1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Parse a Chill 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.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator.
37 Also note that the language accepted by this parser is more liberal
38 than the one accepted by an actual Chill compiler. For example, the
39 language rule that a simple name string can not be one of the reserved
40 simple name strings is not enforced (e.g "case" is not treated as a
41 reserved name). Another example is that Chill is a strongly typed
42 language, and certain expressions that violate the type constraints
43 may still be evaluated if gdb can do so in a meaningful manner, while
44 such expressions would be rejected by the compiler. The reason for
45 this more liberal behavior is the philosophy that the debugger
46 is intended to be a tool that is used by the programmer when things
47 go wrong, and as such, it should provide as few artificial barriers
48 to it's use as possible. If it can do something meaningful, even
49 something that violates language contraints that are enforced by the
50 compiler, it should do so without complaint.
59 #include "expression.h"
62 #include "parser-defs.h"
64 #include "bfd.h" /* Required by objfiles.h. */
65 #include "symfile.h" /* Required by objfiles.h. */
66 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
68 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
69 as well as gratuitiously global symbol names, so we can have multiple
70 yacc generated parsers in gdb. Note that these are only the variables
71 produced by yacc. If other parser generators (bison, byacc, etc) produce
72 additional global names that conflict at link time, then those parser
73 generators need to be fixed instead of adding those names to this list. */
75 #define yymaxdepth chill_maxdepth
76 #define yyparse chill_parse
77 #define yylex chill_lex
78 #define yyerror chill_error
79 #define yylval chill_lval
80 #define yychar chill_char
81 #define yydebug chill_debug
82 #define yypact chill_pact
85 #define yydef chill_def
86 #define yychk chill_chk
87 #define yypgo chill_pgo
88 #define yyact chill_act
89 #define yyexca chill_exca
90 #define yyerrflag chill_errflag
91 #define yynerrs chill_nerrs
95 #define yy_yys chill_yys
96 #define yystate chill_state
97 #define yytmp chill_tmp
99 #define yy_yyv chill_yyv
100 #define yyval chill_val
101 #define yylloc chill_lloc
102 #define yyreds chill_reds /* With YYDEBUG defined */
103 #define yytoks chill_toks /* With YYDEBUG defined */
104 #define yylhs chill_yylhs
105 #define yylen chill_yylen
106 #define yydefred chill_yydefred
107 #define yydgoto chill_yydgoto
108 #define yysindex chill_yysindex
109 #define yyrindex chill_yyrindex
110 #define yygindex chill_yygindex
111 #define yytable chill_yytable
112 #define yycheck chill_yycheck
115 #define YYDEBUG 0 /* Default to no yydebug support */
119 yyparse PARAMS ((void));
122 yylex PARAMS ((void));
125 yyerror PARAMS ((char *));
129 /* Although the yacc "value" of an expression is not used,
130 since the result is stored in the structure being created,
131 other node types do have values. */
136 unsigned LONGEST ulval;
146 struct symtoken ssym;
149 enum exp_opcode opcode;
150 struct internalvar *ivar;
156 %token <typed_val> INTEGER_LITERAL
157 %token <ulval> BOOLEAN_LITERAL
158 %token <typed_val> CHARACTER_LITERAL
159 %token <dval> FLOAT_LITERAL
160 %token <ssym> GENERAL_PROCEDURE_NAME
161 %token <ssym> LOCATION_NAME
162 %token <voidval> EMPTINESS_LITERAL
163 %token <sval> CHARACTER_STRING_LITERAL
164 %token <sval> BIT_STRING_LITERAL
165 %token <tsym> TYPENAME
166 %token <sval> FIELD_NAME
171 %token <voidval> CASE
173 %token <voidval> ESAC
174 %token <voidval> LOGIOR
175 %token <voidval> ORIF
176 %token <voidval> LOGXOR
177 %token <voidval> LOGAND
178 %token <voidval> ANDIF
180 %token <voidval> NOTEQUAL
190 %token <voidval> SLASH_SLASH
194 %token <voidval> POINTER
195 %token <voidval> RECEIVE
202 %token <voidval> THEN
203 %token <voidval> ELSE
205 %token <voidval> ELSIF
206 %token <voidval> ILLEGAL_TOKEN
208 %token <voidval> PRED
209 %token <voidval> SUCC
211 %token <voidval> CARD
212 %token <voidval> MAX_TOKEN
213 %token <voidval> MIN_TOKEN
214 %token <voidval> ADDR_TOKEN
215 %token <voidval> SIZE
216 %token <voidval> UPPER
217 %token <voidval> LOWER
218 %token <voidval> LENGTH
219 %token <voidval> ARRAY
221 /* Tokens which are not Chill tokens used in expressions, but rather GDB
222 specific things that we recognize in the same context as Chill tokens
223 (register names for example). */
225 %token <lval> GDB_REGNAME /* Machine register name */
226 %token <lval> GDB_LAST /* Value history */
227 %token <ivar> GDB_VARIABLE /* Convenience variable */
228 %token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
230 %type <voidval> access_name
231 %type <voidval> primitive_value
232 %type <voidval> value_name
233 %type <voidval> literal
234 %type <voidval> tuple
235 %type <voidval> slice
236 %type <voidval> expression_conversion
237 %type <voidval> value_built_in_routine_call
238 %type <voidval> parenthesised_expression
239 %type <voidval> value
240 %type <voidval> expression
241 %type <voidval> conditional_expression
242 %type <voidval> then_alternative
243 %type <voidval> else_alternative
244 %type <voidval> operand_0
245 %type <voidval> operand_1
246 %type <voidval> operand_2
247 %type <voidval> operand_3
248 %type <voidval> operand_4
249 %type <voidval> operand_5
250 %type <voidval> operand_6
251 %type <voidval> expression_list
252 %type <tval> mode_argument
253 %type <voidval> single_assignment_action
254 %type <tsym> mode_name
258 %type <voidval> undefined_value
259 %type <voidval> array_mode_name
260 %type <voidval> string_mode_name
261 %type <voidval> variant_structure_mode_name
270 { write_exp_elt_opcode(OP_TYPE);
271 write_exp_elt_type($1.type);
272 write_exp_elt_opcode(OP_TYPE);}
284 access_name : LOCATION_NAME
286 write_exp_elt_opcode (OP_VAR_VALUE);
287 write_exp_elt_block (NULL);
288 write_exp_elt_sym ($1.sym);
289 write_exp_elt_opcode (OP_VAR_VALUE);
291 | GDB_LAST /* gdb specific */
293 write_exp_elt_opcode (OP_LAST);
294 write_exp_elt_longcst ($1);
295 write_exp_elt_opcode (OP_LAST);
297 | GDB_REGNAME /* gdb specific */
299 write_exp_elt_opcode (OP_REGISTER);
300 write_exp_elt_longcst ($1);
301 write_exp_elt_opcode (OP_REGISTER);
303 | GDB_VARIABLE /* gdb specific */
305 write_exp_elt_opcode (OP_INTERNALVAR);
306 write_exp_elt_intern ($1);
307 write_exp_elt_opcode (OP_INTERNALVAR);
313 expression_list : expression
317 | expression_list ',' expression
323 maybe_expression_list: /* EMPTY */
333 primitive_value_lparen: primitive_value '('
334 /* This is to save the value of arglist_len
335 being accumulated for each dimension. */
336 { start_arglist (); }
340 { $$ = end_arglist (); }
345 | primitive_value_lparen maybe_expression_list rparen
347 write_exp_elt_opcode (MULTI_SUBSCRIPT);
348 write_exp_elt_longcst ($3);
349 write_exp_elt_opcode (MULTI_SUBSCRIPT);
351 | primitive_value FIELD_NAME
352 { write_exp_elt_opcode (STRUCTOP_STRUCT);
353 write_exp_string ($2);
354 write_exp_elt_opcode (STRUCTOP_STRUCT);
356 | primitive_value POINTER
358 write_exp_elt_opcode (UNOP_IND);
360 | primitive_value POINTER mode_name
362 write_exp_elt_opcode (UNOP_CAST);
363 write_exp_elt_type (lookup_pointer_type ($3.type));
364 write_exp_elt_opcode (UNOP_CAST);
365 write_exp_elt_opcode (UNOP_IND);
371 | expression_conversion
372 | value_built_in_routine_call
379 | parenthesised_expression
384 value_name : GENERAL_PROCEDURE_NAME
386 write_exp_elt_opcode (OP_VAR_VALUE);
387 write_exp_elt_block (NULL);
388 write_exp_elt_sym ($1.sym);
389 write_exp_elt_opcode (OP_VAR_VALUE);
395 literal : INTEGER_LITERAL
397 write_exp_elt_opcode (OP_LONG);
398 write_exp_elt_type ($1.type);
399 write_exp_elt_longcst ((LONGEST) ($1.val));
400 write_exp_elt_opcode (OP_LONG);
404 write_exp_elt_opcode (OP_BOOL);
405 write_exp_elt_longcst ((LONGEST) $1);
406 write_exp_elt_opcode (OP_BOOL);
410 write_exp_elt_opcode (OP_LONG);
411 write_exp_elt_type ($1.type);
412 write_exp_elt_longcst ((LONGEST) ($1.val));
413 write_exp_elt_opcode (OP_LONG);
417 write_exp_elt_opcode (OP_DOUBLE);
418 write_exp_elt_type (builtin_type_double);
419 write_exp_elt_dblcst ($1);
420 write_exp_elt_opcode (OP_DOUBLE);
424 struct type *void_ptr_type
425 = lookup_pointer_type (builtin_type_void);
426 write_exp_elt_opcode (OP_LONG);
427 write_exp_elt_type (void_ptr_type);
428 write_exp_elt_longcst (0);
429 write_exp_elt_opcode (OP_LONG);
431 | CHARACTER_STRING_LITERAL
433 write_exp_elt_opcode (OP_STRING);
434 write_exp_string ($1);
435 write_exp_elt_opcode (OP_STRING);
439 write_exp_elt_opcode (OP_BITSTRING);
440 write_exp_bitstring ($1);
441 write_exp_elt_opcode (OP_BITSTRING);
447 tuple_element : expression
448 | named_record_element
451 named_record_element: FIELD_NAME ',' named_record_element
452 { write_exp_elt_opcode (OP_LABELED);
453 write_exp_string ($1);
454 write_exp_elt_opcode (OP_LABELED);
456 | FIELD_NAME ':' expression
457 { write_exp_elt_opcode (OP_LABELED);
458 write_exp_string ($1);
459 write_exp_elt_opcode (OP_LABELED);
463 tuple_elements : tuple_element
467 | tuple_elements ',' tuple_element
473 maybe_tuple_elements : tuple_elements
478 { start_arglist (); }
479 maybe_tuple_elements ']'
481 write_exp_elt_opcode (OP_ARRAY);
482 write_exp_elt_longcst ((LONGEST) 0);
483 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
484 write_exp_elt_opcode (OP_ARRAY);
488 { start_arglist (); }
489 maybe_tuple_elements ']'
491 write_exp_elt_opcode (OP_ARRAY);
492 write_exp_elt_longcst ((LONGEST) 0);
493 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
494 write_exp_elt_opcode (OP_ARRAY);
496 write_exp_elt_opcode (UNOP_CAST);
497 write_exp_elt_type ($1.type);
498 write_exp_elt_opcode (UNOP_CAST);
506 slice: primitive_value_lparen expression ':' expression rparen
508 write_exp_elt_opcode (TERNOP_SLICE);
510 | primitive_value_lparen expression UP expression rparen
512 write_exp_elt_opcode (TERNOP_SLICE_COUNT);
518 expression_conversion: mode_name parenthesised_expression
520 write_exp_elt_opcode (UNOP_CAST);
521 write_exp_elt_type ($1.type);
522 write_exp_elt_opcode (UNOP_CAST);
524 | ARRAY '(' ')' mode_name parenthesised_expression
525 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
526 which casts to an artificial array. */
528 struct type *range_type
529 = create_range_type ((struct type *) NULL,
530 builtin_type_int, 0, 0);
531 struct type *array_type
532 = create_array_type ((struct type *) NULL,
533 $4.type, range_type);
534 TYPE_ARRAY_UPPER_BOUND_TYPE(array_type)
535 = BOUND_CANNOT_BE_DETERMINED;
536 write_exp_elt_opcode (UNOP_CAST);
537 write_exp_elt_type (array_type);
538 write_exp_elt_opcode (UNOP_CAST);
544 parenthesised_expression: '(' expression ')'
549 expression : operand_0
550 | single_assignment_action
551 | conditional_expression
554 conditional_expression : IF expression then_alternative else_alternative FI
555 { write_exp_elt_opcode (TERNOP_COND); }
557 | CASE case_selector_list OF value_case_alternative ELSE expression ESAC
558 { error ("not implemented: CASE expression" }
562 then_alternative: THEN expression
565 else_alternative: ELSE expression
566 | ELSIF expression then_alternative else_alternative
567 { write_exp_elt_opcode (TERNOP_COND); }
572 operand_0 : operand_1
573 | operand_0 LOGIOR operand_1
575 write_exp_elt_opcode (BINOP_BITWISE_IOR);
577 | operand_0 ORIF operand_1
579 write_exp_elt_opcode (BINOP_LOGICAL_OR);
581 | operand_0 LOGXOR operand_1
583 write_exp_elt_opcode (BINOP_BITWISE_XOR);
589 operand_1 : operand_2
590 | operand_1 LOGAND operand_2
592 write_exp_elt_opcode (BINOP_BITWISE_AND);
594 | operand_1 ANDIF operand_2
596 write_exp_elt_opcode (BINOP_LOGICAL_AND);
602 operand_2 : operand_3
603 | operand_2 '=' operand_3
605 write_exp_elt_opcode (BINOP_EQUAL);
607 | operand_2 NOTEQUAL operand_3
609 write_exp_elt_opcode (BINOP_NOTEQUAL);
611 | operand_2 '>' operand_3
613 write_exp_elt_opcode (BINOP_GTR);
615 | operand_2 GTR operand_3
617 write_exp_elt_opcode (BINOP_GEQ);
619 | operand_2 '<' operand_3
621 write_exp_elt_opcode (BINOP_LESS);
623 | operand_2 LEQ operand_3
625 write_exp_elt_opcode (BINOP_LEQ);
627 | operand_2 IN operand_3
629 write_exp_elt_opcode (BINOP_IN);
636 operand_3 : operand_4
637 | operand_3 '+' operand_4
639 write_exp_elt_opcode (BINOP_ADD);
641 | operand_3 '-' operand_4
643 write_exp_elt_opcode (BINOP_SUB);
645 | operand_3 SLASH_SLASH operand_4
647 write_exp_elt_opcode (BINOP_CONCAT);
653 operand_4 : operand_5
654 | operand_4 '*' operand_5
656 write_exp_elt_opcode (BINOP_MUL);
658 | operand_4 '/' operand_5
660 write_exp_elt_opcode (BINOP_DIV);
662 | operand_4 MOD operand_5
664 write_exp_elt_opcode (BINOP_MOD);
666 | operand_4 REM operand_5
668 write_exp_elt_opcode (BINOP_REM);
674 operand_5 : operand_6
677 write_exp_elt_opcode (UNOP_NEG);
681 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
683 | parenthesised_expression literal
684 /* We require the string operand to be a literal, to avoid some
685 nasty parsing ambiguities. */
687 write_exp_elt_opcode (BINOP_CONCAT);
693 operand_6 : POINTER primitive_value
695 write_exp_elt_opcode (UNOP_ADDR);
698 { error ("not implemented: RECEIVE expression"); }
705 single_assignment_action :
706 primitive_value GDB_ASSIGNMENT value
708 write_exp_elt_opcode (BINOP_ASSIGN);
714 value_built_in_routine_call :
715 NUM '(' expression ')'
717 write_exp_elt_opcode (UNOP_CAST);
718 write_exp_elt_type (builtin_type_int);
719 write_exp_elt_opcode (UNOP_CAST);
721 | PRED '(' expression ')'
722 { error ("not implemented: PRED builtin function"); }
723 | SUCC '(' expression ')'
724 { error ("not implemented: SUCC builtin function"); }
725 | ADDR_TOKEN '(' expression ')'
726 { write_exp_elt_opcode (UNOP_ADDR); }
727 | ABS '(' expression ')'
728 { error ("not implemented: ABS builtin function"); }
729 | CARD '(' expression ')'
730 { error ("not implemented: CARD builtin function"); }
731 | MAX_TOKEN '(' expression ')'
732 { error ("not implemented: MAX builtin function"); }
733 | MIN_TOKEN '(' expression ')'
734 { error ("not implemented: MIN builtin function"); }
735 | SIZE '(' expression ')'
736 { write_exp_elt_opcode (UNOP_SIZEOF); }
737 | SIZE '(' mode_argument ')'
738 { write_exp_elt_opcode (OP_LONG);
739 write_exp_elt_type (builtin_type_int);
740 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
741 write_exp_elt_opcode (OP_LONG); }
742 | LOWER '(' mode_argument ')'
743 { write_lower_upper_value (UNOP_LOWER, $3); }
744 | UPPER '(' mode_argument ')'
745 { write_lower_upper_value (UNOP_UPPER, $3); }
746 | LOWER '(' expression ')'
747 { write_exp_elt_opcode (UNOP_LOWER); }
748 | UPPER '(' expression ')'
749 { write_exp_elt_opcode (UNOP_UPPER); }
750 | LENGTH '(' expression ')'
751 { write_exp_elt_opcode (UNOP_LENGTH); }
754 mode_argument : mode_name
759 | array_mode_name '(' expression ')'
761 | string_mode_name '(' expression ')'
763 | variant_structure_mode_name '(' expression_list ')'
773 /* Implementation of a dynamically expandable buffer for processing input
774 characters acquired through lexptr and building a value to return in
777 static char *tempbuf; /* Current buffer contents */
778 static int tempbufsize; /* Size of allocated buffer */
779 static int tempbufindex; /* Current index into buffer */
781 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
783 #define CHECKBUF(size) \
785 if (tempbufindex + (size) >= tempbufsize) \
787 growbuf_by_size (size); \
791 /* Grow the static temp buffer if necessary, including allocating the first one
795 growbuf_by_size (count)
800 growby = max (count, GROWBY_MIN_SIZE);
801 tempbufsize += growby;
804 tempbuf = (char *) malloc (tempbufsize);
808 tempbuf = (char *) realloc (tempbuf, tempbufsize);
812 /* Try to consume a simple name string token. If successful, returns
813 a pointer to a nullbyte terminated copy of the name that can be used
814 in symbol table lookups. If not successful, returns NULL. */
817 match_simple_name_string ()
819 char *tokptr = lexptr;
821 if (isalpha (*tokptr) || *tokptr == '_')
826 } while (isalnum (*tokptr) || (*tokptr == '_'));
827 yylval.sval.ptr = lexptr;
828 yylval.sval.length = tokptr - lexptr;
830 result = copy_name (yylval.sval);
836 /* Start looking for a value composed of valid digits as set by the base
837 in use. Note that '_' characters are valid anywhere, in any quantity,
838 and are simply ignored. Since we must find at least one valid digit,
839 or reject this token as an integer literal, we keep track of how many
840 digits we have encountered. */
843 decode_integer_value (base, tokptrptr, ivalptr)
848 char *tokptr = *tokptrptr;
852 while (*tokptr != '\0')
856 temp = tolower (temp);
862 case '0': case '1': case '2': case '3': case '4':
863 case '5': case '6': case '7': case '8': case '9':
866 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
882 /* Found something not in domain for current base. */
883 tokptr--; /* Unconsume what gave us indigestion. */
888 /* If we didn't find any digits, then we don't have a valid integer
889 value, so reject the entire token. Otherwise, update the lexical
890 scan pointer, and return non-zero for success. */
904 decode_integer_literal (valptr, tokptrptr)
908 char *tokptr = *tokptrptr;
911 int explicit_base = 0;
913 /* Look for an explicit base specifier, which is optional. */
946 /* If we found an explicit base ensure that the character after the
947 explicit base is a single quote. */
949 if (explicit_base && (*tokptr++ != '\''))
954 /* Attempt to decode whatever follows as an integer value in the
955 indicated base, updating the token pointer in the process and
956 computing the value into ival. Also, if we have an explicit
957 base, then the next character must not be a single quote, or we
958 have a bitstring literal, so reject the entire token in this case.
959 Otherwise, update the lexical scan pointer, and return non-zero
962 if (!decode_integer_value (base, &tokptr, &ival))
966 else if (explicit_base && (*tokptr == '\''))
978 /* If it wasn't for the fact that floating point values can contain '_'
979 characters, we could just let strtod do all the hard work by letting it
980 try to consume as much of the current token buffer as possible and
981 find a legal conversion. Unfortunately we need to filter out the '_'
982 characters before calling strtod, which we do by copying the other
983 legal chars to a local buffer to be converted. However since we also
984 need to keep track of where the last unconsumed character in the input
985 buffer is, we have transfer only as many characters as may compose a
986 legal floating point value. */
989 match_float_literal ()
991 char *tokptr = lexptr;
995 extern double strtod ();
997 /* Make local buffer in which to build the string to convert. This is
998 required because underscores are valid in chill floating point numbers
999 but not in the string passed to strtod to convert. The string will be
1000 no longer than our input string. */
1002 copy = buf = (char *) alloca (strlen (tokptr) + 1);
1004 /* Transfer all leading digits to the conversion buffer, discarding any
1007 while (isdigit (*tokptr) || *tokptr == '_')
1016 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1017 of whether we found any leading digits, and we simply accept it and
1018 continue on to look for the fractional part and/or exponent. One of
1019 [eEdD] is legal only if we have seen digits, and means that there
1020 is no fractional part. If we find neither of these, then this is
1021 not a floating point number, so return failure. */
1026 /* Accept and then look for fractional part and/or exponent. */
1039 goto collect_exponent;
1047 /* We found a '.', copy any fractional digits to the conversion buffer, up
1048 to the first nondigit, non-underscore character. */
1050 while (isdigit (*tokptr) || *tokptr == '_')
1059 /* Look for an exponent, which must start with one of [eEdD]. If none
1060 is found, jump directly to trying to convert what we have collected
1077 /* Accept an optional '-' or '+' following one of [eEdD]. */
1080 if (*tokptr == '+' || *tokptr == '-')
1082 *copy++ = *tokptr++;
1085 /* Now copy an exponent into the conversion buffer. Note that at the
1086 moment underscores are *not* allowed in exponents. */
1088 while (isdigit (*tokptr))
1090 *copy++ = *tokptr++;
1093 /* If we transfered any chars to the conversion buffer, try to interpret its
1094 contents as a floating point value. If any characters remain, then we
1095 must not have a valid floating point string. */
1101 dval = strtod (buf, ©);
1106 return (FLOAT_LITERAL);
1112 /* Recognize a string literal. A string literal is a sequence
1113 of characters enclosed in matching single or double quotes, except that
1114 a single character inside single quotes is a character literal, which
1115 we reject as a string literal. To embed the terminator character inside
1116 a string, it is simply doubled (I.E. "this""is""one""string") */
1119 match_string_literal ()
1121 char *tokptr = lexptr;
1123 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1126 if (*tokptr == *lexptr)
1128 if (*(tokptr + 1) == *lexptr)
1137 tempbuf[tempbufindex++] = *tokptr;
1139 if (*tokptr == '\0' /* no terminator */
1140 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1146 tempbuf[tempbufindex] = '\0';
1147 yylval.sval.ptr = tempbuf;
1148 yylval.sval.length = tempbufindex;
1150 return (CHARACTER_STRING_LITERAL);
1154 /* Recognize a character literal. A character literal is single character
1155 or a control sequence, enclosed in single quotes. A control sequence
1156 is a comma separated list of one or more integer literals, enclosed
1157 in parenthesis and introduced with a circumflex character.
1159 EX: 'a' '^(7)' '^(7,8)'
1161 As a GNU chill extension, the syntax C'xx' is also recognized as a
1162 character literal, where xx is a hex value for the character.
1164 Note that more than a single character, enclosed in single quotes, is
1167 Also note that the control sequence form is not in GNU Chill since it
1168 is ambiguous with the string literal form using single quotes. I.E.
1169 is '^(7)' a character literal or a string literal. In theory it it
1170 possible to tell by context, but GNU Chill doesn't accept the control
1171 sequence form, so neither do we (for now the code is disabled).
1173 Returns CHARACTER_LITERAL if a match is found.
1177 match_character_literal ()
1179 char *tokptr = lexptr;
1182 if ((*tokptr == 'c' || *tokptr == 'C') && (*(tokptr + 1) == '\''))
1184 /* We have a GNU chill extension form, so skip the leading "C'",
1185 decode the hex value, and then ensure that we have a trailing
1186 single quote character. */
1188 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1194 else if (*tokptr == '\'')
1198 /* Determine which form we have, either a control sequence or the
1199 single character form. */
1201 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1203 #if 0 /* Disable, see note above. -fnf */
1204 /* Match and decode a control sequence. Return zero if we don't
1205 find a valid integer literal, or if the next unconsumed character
1206 after the integer literal is not the trailing ')'.
1207 FIXME: We currently don't handle the multiple integer literal
1210 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1223 /* The trailing quote has not yet been consumed. If we don't find
1224 it, then we have no match. */
1226 if (*tokptr++ != '\'')
1233 /* Not a character literal. */
1236 yylval.typed_val.val = ival;
1237 yylval.typed_val.type = builtin_type_chill_char;
1239 return (CHARACTER_LITERAL);
1242 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1243 Note that according to 5.2.4.2, a single "_" is also a valid integer
1244 literal, however GNU-chill requires there to be at least one "digit"
1245 in any integer literal. */
1248 match_integer_literal ()
1250 char *tokptr = lexptr;
1253 if (!decode_integer_literal (&ival, &tokptr))
1259 yylval.typed_val.val = ival;
1260 yylval.typed_val.type = builtin_type_int;
1262 return (INTEGER_LITERAL);
1266 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1267 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1268 literal, however GNU-chill requires there to be at least one "digit"
1269 in any bit-string literal. */
1272 match_bitstring_literal ()
1274 register char *tokptr = lexptr;
1284 /* Look for the required explicit base specifier. */
1305 /* Ensure that the character after the explicit base is a single quote. */
1307 if (*tokptr++ != '\'')
1312 while (*tokptr != '\0' && *tokptr != '\'')
1315 if (isupper (digit))
1316 digit = tolower (digit);
1322 case '0': case '1': case '2': case '3': case '4':
1323 case '5': case '6': case '7': case '8': case '9':
1326 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1334 if (digit >= 1 << bits_per_char)
1336 /* Found something not in domain for current base. */
1341 /* Extract bits from digit, packing them into the bitstring byte. */
1342 int k = TARGET_BYTE_ORDER == BIG_ENDIAN ? bits_per_char - 1 : 0;
1343 for (; TARGET_BYTE_ORDER == BIG_ENDIAN ? k >= 0 : k < bits_per_char;
1344 TARGET_BYTE_ORDER == BIG_ENDIAN ? k-- : k++)
1347 if (digit & (1 << k))
1349 tempbuf[tempbufindex] |=
1350 (TARGET_BYTE_ORDER == BIG_ENDIAN)
1351 ? (1 << (HOST_CHAR_BIT - 1 - bitoffset))
1355 if (bitoffset == HOST_CHAR_BIT)
1360 tempbuf[tempbufindex] = 0;
1366 /* Verify that we consumed everything up to the trailing single quote,
1367 and that we found some bits (IE not just underbars). */
1369 if (*tokptr++ != '\'')
1375 yylval.sval.ptr = tempbuf;
1376 yylval.sval.length = bitcount;
1378 return (BIT_STRING_LITERAL);
1382 /* Recognize tokens that start with '$'. These include:
1384 $regname A native register name or a "standard
1386 Return token GDB_REGNAME.
1388 $variable A convenience variable with a name chosen
1390 Return token GDB_VARIABLE.
1392 $digits Value history with index <digits>, starting
1393 from the first value which has index 1.
1396 $$digits Value history with index <digits> relative
1397 to the last value. I.E. $$0 is the last
1398 value, $$1 is the one previous to that, $$2
1399 is the one previous to $$1, etc.
1400 Return token GDB_LAST.
1402 $ | $0 | $$0 The last value in the value history.
1403 Return token GDB_LAST.
1405 $$ An abbreviation for the second to the last
1406 value in the value history, I.E. $$1
1407 Return token GDB_LAST.
1409 Note that we currently assume that register names and convenience
1410 variables follow the convention of starting with a letter or '_'.
1415 match_dollar_tokens ()
1423 /* We will always have a successful match, even if it is just for
1424 a single '$', the abbreviation for $$0. So advance lexptr. */
1428 if (*tokptr == '_' || isalpha (*tokptr))
1430 /* Look for a match with a native register name, usually something
1431 like "r0" for example. */
1433 for (regno = 0; regno < NUM_REGS; regno++)
1435 namelength = strlen (reg_names[regno]);
1436 if (STREQN (tokptr, reg_names[regno], namelength)
1437 && !isalnum (tokptr[namelength]))
1439 yylval.lval = regno;
1440 lexptr += namelength;
1441 return (GDB_REGNAME);
1445 /* Look for a match with a standard register name, usually something
1446 like "pc", which gdb always recognizes as the program counter
1447 regardless of what the native register name is. */
1449 for (regno = 0; regno < num_std_regs; regno++)
1451 namelength = strlen (std_regs[regno].name);
1452 if (STREQN (tokptr, std_regs[regno].name, namelength)
1453 && !isalnum (tokptr[namelength]))
1455 yylval.lval = std_regs[regno].regnum;
1456 lexptr += namelength;
1457 return (GDB_REGNAME);
1461 /* Attempt to match against a convenience variable. Note that
1462 this will always succeed, because if no variable of that name
1463 already exists, the lookup_internalvar will create one for us.
1464 Also note that both lexptr and tokptr currently point to the
1465 start of the input string we are trying to match, and that we
1466 have already tested the first character for non-numeric, so we
1467 don't have to treat it specially. */
1469 while (*tokptr == '_' || isalnum (*tokptr))
1473 yylval.sval.ptr = lexptr;
1474 yylval.sval.length = tokptr - lexptr;
1475 yylval.ivar = lookup_internalvar (copy_name (yylval.sval));
1477 return (GDB_VARIABLE);
1480 /* Since we didn't match against a register name or convenience
1481 variable, our only choice left is a history value. */
1495 /* Attempt to decode more characters as an integer value giving
1496 the index in the history list. If successful, the value will
1497 overwrite ival (currently 0 or 1), and if not, ival will be
1498 left alone, which is good since it is currently correct for
1499 the '$' or '$$' case. */
1501 decode_integer_literal (&ival, &tokptr);
1502 yylval.lval = negate ? -ival : ival;
1513 static const struct token idtokentab[] =
1516 { "length", LENGTH },
1527 { "max", MAX_TOKEN },
1528 { "min", MIN_TOKEN },
1537 { "addr", ADDR_TOKEN },
1538 { "null", EMPTINESS_LITERAL }
1541 static const struct token tokentab2[] =
1543 { ":=", GDB_ASSIGNMENT },
1544 { "//", SLASH_SLASH },
1551 /* Read one token, getting characters through lexptr. */
1552 /* This is where we will check to make sure that the language and the
1553 operators used are compatible. */
1563 /* Skip over any leading whitespace. */
1564 while (isspace (*lexptr))
1568 /* Look for special single character cases which can't be the first
1569 character of some other multicharacter token. */
1586 /* Look for characters which start a particular kind of multicharacter
1587 token, such as a character literal, register name, convenience
1588 variable name, string literal, etc. */
1593 /* First try to match a string literal, which is any
1594 sequence of characters enclosed in matching single or double
1595 quotes, except that a single character inside single quotes
1596 is a character literal, so we have to catch that case also. */
1597 token = match_string_literal ();
1602 if (*lexptr == '\'')
1604 token = match_character_literal ();
1613 token = match_character_literal ();
1620 token = match_dollar_tokens ();
1627 /* See if it is a special token of length 2. */
1628 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1630 if (STREQN (lexptr, tokentab2[i].operator, 2))
1633 return (tokentab2[i].token);
1636 /* Look for single character cases which which could be the first
1637 character of some other multicharacter token, but aren't, or we
1638 would already have found it. */
1648 /* Look for a float literal before looking for an integer literal, so
1649 we match as much of the input stream as possible. */
1650 token = match_float_literal ();
1655 token = match_bitstring_literal ();
1660 token = match_integer_literal ();
1666 /* Try to match a simple name string, and if a match is found, then
1667 further classify what sort of name it is and return an appropriate
1668 token. Note that attempting to match a simple name string consumes
1669 the token from lexptr, so we can't back out if we later find that
1670 we can't classify what sort of name it is. */
1672 inputname = match_simple_name_string ();
1674 if (inputname != NULL)
1676 char *simplename = (char*) alloca (strlen (inputname) + 1);
1678 char *dptr = simplename, *sptr = inputname;
1679 for (; *sptr; sptr++)
1680 *dptr++ = isupper (*sptr) ? tolower(*sptr) : *sptr;
1683 /* See if it is a reserved identifier. */
1684 for (i = 0; i < sizeof (idtokentab) / sizeof (idtokentab[0]); i++)
1686 if (STREQ (simplename, idtokentab[i].operator))
1688 return (idtokentab[i].token);
1692 /* Look for other special tokens. */
1693 if (STREQ (simplename, "true"))
1696 return (BOOLEAN_LITERAL);
1698 if (STREQ (simplename, "false"))
1701 return (BOOLEAN_LITERAL);
1704 sym = lookup_symbol (inputname, expression_context_block,
1705 VAR_NAMESPACE, (int *) NULL,
1706 (struct symtab **) NULL);
1707 if (sym == NULL && strcmp (inputname, simplename) != 0)
1709 sym = lookup_symbol (simplename, expression_context_block,
1710 VAR_NAMESPACE, (int *) NULL,
1711 (struct symtab **) NULL);
1715 yylval.ssym.stoken.ptr = NULL;
1716 yylval.ssym.stoken.length = 0;
1717 yylval.ssym.sym = sym;
1718 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1719 switch (SYMBOL_CLASS (sym))
1722 /* Found a procedure name. */
1723 return (GENERAL_PROCEDURE_NAME);
1725 /* Found a global or local static variable. */
1726 return (LOCATION_NAME);
1731 case LOC_REGPARM_ADDR:
1735 case LOC_BASEREG_ARG:
1736 if (innermost_block == NULL
1737 || contained_in (block_found, innermost_block))
1739 innermost_block = block_found;
1741 return (LOCATION_NAME);
1745 return (LOCATION_NAME);
1748 yylval.tsym.type = SYMBOL_TYPE (sym);
1751 case LOC_CONST_BYTES:
1752 case LOC_OPTIMIZED_OUT:
1753 error ("Symbol \"%s\" names no location.", inputname);
1757 else if (!have_full_symbols () && !have_partial_symbols ())
1759 error ("No symbol table is loaded. Use the \"file\" command.");
1763 error ("No symbol \"%s\" in current context.", inputname);
1767 /* Catch single character tokens which are not part of some
1772 case '.': /* Not float for example. */
1774 while (isspace (*lexptr)) lexptr++;
1775 inputname = match_simple_name_string ();
1781 return (ILLEGAL_TOKEN);
1785 write_lower_upper_value (opcode, type)
1786 enum exp_opcode opcode; /* Either UNOP_LOWER or UNOP_UPPER */
1789 extern LONGEST type_lower_upper ();
1790 struct type *result_type;
1791 LONGEST val = type_lower_upper (opcode, type, &result_type);
1792 write_exp_elt_opcode (OP_LONG);
1793 write_exp_elt_type (result_type);
1794 write_exp_elt_longcst (val);
1795 write_exp_elt_opcode (OP_LONG);
1802 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);