* eval.c (evaluate_subexp): Add case MULTI_SUBSCRIPT.
[deliverable/binutils-gdb.git] / gdb / ch-exp.y
1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
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.
28
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.
36
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.
51
52 */
53
54 %{
55
56 #include "defs.h"
57 #include "expression.h"
58 #include "language.h"
59 #include "value.h"
60 #include "parser-defs.h"
61 #include "ch-lang.h"
62
63 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
64 as well as gratuitiously global symbol names, so we can have multiple
65 yacc generated parsers in gdb. Note that these are only the variables
66 produced by yacc. If other parser generators (bison, byacc, etc) produce
67 additional global names that conflict at link time, then those parser
68 generators need to be fixed instead of adding those names to this list. */
69
70 #define yymaxdepth chill_maxdepth
71 #define yyparse chill_parse
72 #define yylex chill_lex
73 #define yyerror chill_error
74 #define yylval chill_lval
75 #define yychar chill_char
76 #define yydebug chill_debug
77 #define yypact chill_pact
78 #define yyr1 chill_r1
79 #define yyr2 chill_r2
80 #define yydef chill_def
81 #define yychk chill_chk
82 #define yypgo chill_pgo
83 #define yyact chill_act
84 #define yyexca chill_exca
85 #define yyerrflag chill_errflag
86 #define yynerrs chill_nerrs
87 #define yyps chill_ps
88 #define yypv chill_pv
89 #define yys chill_s
90 #define yy_yys chill_yys
91 #define yystate chill_state
92 #define yytmp chill_tmp
93 #define yyv chill_v
94 #define yy_yyv chill_yyv
95 #define yyval chill_val
96 #define yylloc chill_lloc
97 #define yyreds chill_reds /* With YYDEBUG defined */
98 #define yytoks chill_toks /* With YYDEBUG defined */
99
100 #ifndef YYDEBUG
101 #define YYDEBUG 0 /* Default to no yydebug support */
102 #endif
103
104 int
105 yyparse PARAMS ((void));
106
107 static int
108 yylex PARAMS ((void));
109
110 void
111 yyerror PARAMS ((char *));
112
113 %}
114
115 /* Although the yacc "value" of an expression is not used,
116 since the result is stored in the structure being created,
117 other node types do have values. */
118
119 %union
120 {
121 LONGEST lval;
122 unsigned LONGEST ulval;
123 struct {
124 LONGEST val;
125 struct type *type;
126 } typed_val;
127 double dval;
128 struct symbol *sym;
129 struct type *tval;
130 struct stoken sval;
131 struct ttype tsym;
132 struct symtoken ssym;
133 int voidval;
134 struct block *bval;
135 enum exp_opcode opcode;
136 struct internalvar *ivar;
137
138 struct type **tvec;
139 int *ivec;
140 }
141
142 %token <voidval> FIXME
143
144 %token <typed_val> INTEGER_LITERAL
145 %token <ulval> BOOLEAN_LITERAL
146 %token <typed_val> CHARACTER_LITERAL
147 %token <ssym> GENERAL_PROCEDURE_NAME
148 %token <ssym> LOCATION_NAME
149 %token <voidval> SET_LITERAL
150 %token <voidval> EMPTINESS_LITERAL
151 %token <voidval> CHARACTER_STRING_LITERAL
152 %token <voidval> BIT_STRING_LITERAL
153
154 %token <voidval> STRING
155 %token <voidval> CONSTANT
156 %token <voidval> '.'
157 %token <voidval> ';'
158 %token <voidval> ':'
159 %token <voidval> CASE
160 %token <voidval> OF
161 %token <voidval> ESAC
162 %token <voidval> LOGIOR
163 %token <voidval> ORIF
164 %token <voidval> LOGXOR
165 %token <voidval> LOGAND
166 %token <voidval> ANDIF
167 %token <voidval> '='
168 %token <voidval> NOTEQUAL
169 %token <voidval> '>'
170 %token <voidval> GTR
171 %token <voidval> '<'
172 %token <voidval> LEQ
173 %token <voidval> IN
174 %token <voidval> '+'
175 %token <voidval> '-'
176 %token <voidval> '*'
177 %token <voidval> '/'
178 %token <voidval> SLASH_SLASH
179 %token <voidval> MOD
180 %token <voidval> REM
181 %token <voidval> NOT
182 %token <voidval> POINTER
183 %token <voidval> RECEIVE
184 %token <voidval> SC
185 %token <voidval> '['
186 %token <voidval> ']'
187 %token <voidval> '('
188 %token <voidval> ')'
189 %token <voidval> UP
190 %token <voidval> IF
191 %token <voidval> THEN
192 %token <voidval> ELSE
193 %token <voidval> FI
194 %token <voidval> ELSIF
195 %token <voidval> ILLEGAL_TOKEN
196
197 /* Tokens which are not Chill tokens used in expressions, but rather GDB
198 specific things that we recognize in the same context as Chill tokens
199 (register names for example). */
200
201 %token <lval> GDB_REGNAME /* Machine register name */
202 %token <lval> GDB_LAST /* Value history */
203 %token <ivar> GDB_VARIABLE /* Convenience variable */
204 %token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
205
206 %type <voidval> location
207 %type <voidval> access_name
208 %type <voidval> primitive_value
209 %type <voidval> location_contents
210 %type <voidval> value_name
211 %type <voidval> literal
212 %type <voidval> tuple
213 %type <voidval> value_string_element
214 %type <voidval> value_string_slice
215 %type <voidval> value_array_element
216 %type <voidval> value_array_slice
217 %type <voidval> value_structure_field
218 %type <voidval> expression_conversion
219 %type <voidval> value_procedure_call
220 %type <voidval> value_built_in_routine_call
221 %type <voidval> start_expression
222 %type <voidval> zero_adic_operator
223 %type <voidval> parenthesised_expression
224 %type <voidval> value
225 %type <voidval> undefined_value
226 %type <voidval> expression
227 %type <voidval> conditional_expression
228 %type <voidval> then_alternative
229 %type <voidval> else_alternative
230 %type <voidval> sub_expression
231 %type <voidval> value_case_alternative
232 %type <voidval> operand_0
233 %type <voidval> operand_1
234 %type <voidval> operand_2
235 %type <voidval> operand_3
236 %type <voidval> operand_4
237 %type <voidval> operand_5
238 %type <voidval> operand_6
239 %type <voidval> integer_literal_expression
240 %type <voidval> synonym_name
241 %type <voidval> value_enumeration_name
242 %type <voidval> value_do_with_name
243 %type <voidval> value_receive_name
244 %type <voidval> string_primitive_value
245 %type <voidval> start_element
246 %type <voidval> left_element
247 %type <voidval> right_element
248 %type <voidval> slice_size
249 %type <voidval> array_primitive_value
250 %type <voidval> expression_list
251 %type <voidval> lower_element
252 %type <voidval> upper_element
253 %type <voidval> first_element
254 %type <voidval> structure_primitive_value
255 %type <voidval> field_name
256 %type <voidval> mode_name
257 %type <voidval> boolean_expression
258 %type <voidval> case_selector_list
259 %type <voidval> subexpression
260 %type <voidval> case_label_specification
261 %type <voidval> buffer_location
262
263 %type <voidval> single_assignment_action
264
265 %%
266
267 /* Z.200, 5.3.1 */
268
269 value : expression
270 {
271 $$ = 0; /* FIXME */
272 }
273 | undefined_value
274 {
275 $$ = 0; /* FIXME */
276 }
277 ;
278
279 undefined_value : FIXME
280 {
281 $$ = 0; /* FIXME */
282 }
283 ;
284
285 /* Z.200, 4.2.1 */
286
287 location : access_name
288 {
289 $$ = 0; /* FIXME */
290 }
291 | FIXME
292 {
293 $$ = 0; /* FIXME */
294 }
295 ;
296
297 /* Z.200, 4.2.2 */
298
299 access_name : LOCATION_NAME
300 {
301 write_exp_elt_opcode (OP_VAR_VALUE);
302 write_exp_elt_sym ($1.sym);
303 write_exp_elt_opcode (OP_VAR_VALUE);
304 }
305 | GDB_LAST /* gdb specific */
306 {
307 write_exp_elt_opcode (OP_LAST);
308 write_exp_elt_longcst ($1);
309 write_exp_elt_opcode (OP_LAST);
310 }
311 | GDB_REGNAME /* gdb specific */
312 {
313 write_exp_elt_opcode (OP_REGISTER);
314 write_exp_elt_longcst ($1);
315 write_exp_elt_opcode (OP_REGISTER);
316 }
317 | GDB_VARIABLE /* gdb specific */
318 {
319 write_exp_elt_opcode (OP_INTERNALVAR);
320 write_exp_elt_intern ($1);
321 write_exp_elt_opcode (OP_INTERNALVAR);
322 }
323 | FIXME
324 {
325 $$ = 0; /* FIXME */
326 }
327 ;
328
329 /* Z.200, 4.2.8 */
330
331 expression_list : expression
332 {
333 arglist_len = 1;
334 }
335 | expression_list ',' expression
336 {
337 arglist_len++;
338 }
339
340 /* Z.200, 5.2.1 */
341
342 primitive_value : location_contents
343 {
344 $$ = 0; /* FIXME */
345 }
346 | value_name
347 {
348 $$ = 0; /* FIXME */
349 }
350 | literal
351 {
352 $$ = 0; /* FIXME */
353 }
354 | tuple
355 {
356 $$ = 0; /* FIXME */
357 }
358 | value_string_element
359 {
360 $$ = 0; /* FIXME */
361 }
362 | value_string_slice
363 {
364 $$ = 0; /* FIXME */
365 }
366 | value_array_element
367 {
368 $$ = 0; /* FIXME */
369 }
370 | value_array_slice
371 {
372 $$ = 0; /* FIXME */
373 }
374 | value_structure_field
375 {
376 $$ = 0; /* FIXME */
377 }
378 | expression_conversion
379 {
380 $$ = 0; /* FIXME */
381 }
382 | value_procedure_call
383 {
384 $$ = 0; /* FIXME */
385 }
386 | value_built_in_routine_call
387 {
388 $$ = 0; /* FIXME */
389 }
390 | start_expression
391 {
392 $$ = 0; /* FIXME */
393 }
394 | zero_adic_operator
395 {
396 $$ = 0; /* FIXME */
397 }
398 | parenthesised_expression
399 {
400 $$ = 0; /* FIXME */
401 }
402 ;
403
404 /* Z.200, 5.2.2 */
405
406 location_contents: location
407 {
408 $$ = 0; /* FIXME */
409 }
410 ;
411
412 /* Z.200, 5.2.3 */
413
414 value_name : synonym_name
415 {
416 $$ = 0; /* FIXME */
417 }
418 | value_enumeration_name
419 {
420 $$ = 0; /* FIXME */
421 }
422 | value_do_with_name
423 {
424 $$ = 0; /* FIXME */
425 }
426 | value_receive_name
427 {
428 $$ = 0; /* FIXME */
429 }
430 | GENERAL_PROCEDURE_NAME
431 {
432 write_exp_elt_opcode (OP_VAR_VALUE);
433 write_exp_elt_sym ($1.sym);
434 write_exp_elt_opcode (OP_VAR_VALUE);
435 }
436 ;
437
438 /* Z.200, 5.2.4.1 */
439
440 literal : INTEGER_LITERAL
441 {
442 write_exp_elt_opcode (OP_LONG);
443 write_exp_elt_type ($1.type);
444 write_exp_elt_longcst ((LONGEST) ($1.val));
445 write_exp_elt_opcode (OP_LONG);
446 }
447 | BOOLEAN_LITERAL
448 {
449 write_exp_elt_opcode (OP_BOOL);
450 write_exp_elt_longcst ((LONGEST) $1);
451 write_exp_elt_opcode (OP_BOOL);
452 }
453 | CHARACTER_LITERAL
454 {
455 write_exp_elt_opcode (OP_LONG);
456 write_exp_elt_type ($1.type);
457 write_exp_elt_longcst ((LONGEST) ($1.val));
458 write_exp_elt_opcode (OP_LONG);
459 }
460 | SET_LITERAL
461 {
462 $$ = 0; /* FIXME */
463 }
464 | EMPTINESS_LITERAL
465 {
466 $$ = 0; /* FIXME */
467 }
468 | CHARACTER_STRING_LITERAL
469 {
470 $$ = 0; /* FIXME */
471 }
472 | BIT_STRING_LITERAL
473 {
474 $$ = 0; /* FIXME */
475 }
476 ;
477
478 /* Z.200, 5.2.5 */
479
480 tuple : FIXME
481 {
482 $$ = 0; /* FIXME */
483 }
484 ;
485
486
487 /* Z.200, 5.2.6 */
488
489 value_string_element: string_primitive_value '(' start_element ')'
490 {
491 $$ = 0; /* FIXME */
492 }
493 ;
494
495 /* Z.200, 5.2.7 */
496
497 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
498 {
499 $$ = 0; /* FIXME */
500 }
501 | string_primitive_value '(' start_element UP slice_size ')'
502 {
503 $$ = 0; /* FIXME */
504 }
505 ;
506
507 /* Z.200, 5.2.8 */
508
509 value_array_element: array_primitive_value '('
510 /* This is to save the value of arglist_len
511 being accumulated for each dimension. */
512 { start_arglist (); }
513 expression_list ')'
514 {
515 write_exp_elt_opcode (MULTI_SUBSCRIPT);
516 write_exp_elt_longcst ((LONGEST) end_arglist ());
517 write_exp_elt_opcode (MULTI_SUBSCRIPT);
518 }
519 ;
520
521 /* Z.200, 5.2.9 */
522
523 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
524 {
525 $$ = 0; /* FIXME */
526 }
527 | array_primitive_value '(' first_element UP slice_size ')'
528 {
529 $$ = 0; /* FIXME */
530 }
531 ;
532
533 /* Z.200, 5.2.10 */
534
535 value_structure_field: structure_primitive_value '.' field_name
536 {
537 $$ = 0; /* FIXME */
538 }
539 ;
540
541 /* Z.200, 5.2.11 */
542
543 expression_conversion: mode_name '(' expression ')'
544 {
545 $$ = 0; /* FIXME */
546 }
547 ;
548
549 /* Z.200, 5.2.12 */
550
551 value_procedure_call: FIXME
552 {
553 $$ = 0; /* FIXME */
554 }
555 ;
556
557 /* Z.200, 5.2.13 */
558
559 value_built_in_routine_call: FIXME
560 {
561 $$ = 0; /* FIXME */
562 }
563 ;
564
565 /* Z.200, 5.2.14 */
566
567 start_expression: FIXME
568 {
569 $$ = 0; /* FIXME */
570 } /* Not in GNU-Chill */
571 ;
572
573 /* Z.200, 5.2.15 */
574
575 zero_adic_operator: FIXME
576 {
577 $$ = 0; /* FIXME */
578 }
579 ;
580
581 /* Z.200, 5.2.16 */
582
583 parenthesised_expression: '(' expression ')'
584 {
585 $$ = 0; /* FIXME */
586 }
587 ;
588
589 /* Z.200, 5.3.2 */
590
591 expression : operand_0
592 {
593 $$ = 0; /* FIXME */
594 }
595 | conditional_expression
596 {
597 $$ = 0; /* FIXME */
598 }
599 ;
600
601 conditional_expression : IF boolean_expression then_alternative else_alternative FI
602 {
603 $$ = 0; /* FIXME */
604 }
605 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
606 {
607 $$ = 0; /* FIXME */
608 }
609 ;
610
611 then_alternative: THEN subexpression
612 {
613 $$ = 0; /* FIXME */
614 }
615 ;
616
617 else_alternative: ELSE subexpression
618 {
619 $$ = 0; /* FIXME */
620 }
621 | ELSIF boolean_expression then_alternative else_alternative
622 {
623 $$ = 0; /* FIXME */
624 }
625 ;
626
627 sub_expression : expression
628 {
629 $$ = 0; /* FIXME */
630 }
631 ;
632
633 value_case_alternative: case_label_specification ':' sub_expression ';'
634 {
635 $$ = 0; /* FIXME */
636 }
637 ;
638
639 /* Z.200, 5.3.3 */
640
641 operand_0 : operand_1
642 {
643 $$ = 0; /* FIXME */
644 }
645 | operand_0 LOGIOR operand_1
646 {
647 write_exp_elt_opcode (BINOP_BITWISE_IOR);
648 }
649 | operand_0 ORIF operand_1
650 {
651 $$ = 0; /* FIXME */
652 }
653 | operand_0 LOGXOR operand_1
654 {
655 write_exp_elt_opcode (BINOP_BITWISE_XOR);
656 }
657 | single_assignment_action
658 {
659 $$ = 0; /* FIXME */
660 }
661 ;
662
663 /* Z.200, 5.3.4 */
664
665 operand_1 : operand_2
666 {
667 $$ = 0; /* FIXME */
668 }
669 | operand_1 LOGAND operand_2
670 {
671 write_exp_elt_opcode (BINOP_BITWISE_AND);
672 }
673 | operand_1 ANDIF operand_2
674 {
675 $$ = 0; /* FIXME */
676 }
677 ;
678
679 /* Z.200, 5.3.5 */
680
681 operand_2 : operand_3
682 {
683 $$ = 0; /* FIXME */
684 }
685 | operand_2 '=' operand_3
686 {
687 write_exp_elt_opcode (BINOP_EQUAL);
688 }
689 | operand_2 NOTEQUAL operand_3
690 {
691 write_exp_elt_opcode (BINOP_NOTEQUAL);
692 }
693 | operand_2 '>' operand_3
694 {
695 write_exp_elt_opcode (BINOP_GTR);
696 }
697 | operand_2 GTR operand_3
698 {
699 write_exp_elt_opcode (BINOP_GEQ);
700 }
701 | operand_2 '<' operand_3
702 {
703 write_exp_elt_opcode (BINOP_LESS);
704 }
705 | operand_2 LEQ operand_3
706 {
707 write_exp_elt_opcode (BINOP_LEQ);
708 }
709 | operand_2 IN operand_3
710 {
711 $$ = 0; /* FIXME */
712 }
713 ;
714
715
716 /* Z.200, 5.3.6 */
717
718 operand_3 : operand_4
719 {
720 $$ = 0; /* FIXME */
721 }
722 | operand_3 '+' operand_4
723 {
724 write_exp_elt_opcode (BINOP_ADD);
725 }
726 | operand_3 '-' operand_4
727 {
728 write_exp_elt_opcode (BINOP_SUB);
729 }
730 | operand_3 SLASH_SLASH operand_4
731 {
732 $$ = 0; /* FIXME */
733 }
734 ;
735
736 /* Z.200, 5.3.7 */
737
738 operand_4 : operand_5
739 {
740 $$ = 0; /* FIXME */
741 }
742 | operand_4 '*' operand_5
743 {
744 write_exp_elt_opcode (BINOP_MUL);
745 }
746 | operand_4 '/' operand_5
747 {
748 write_exp_elt_opcode (BINOP_DIV);
749 }
750 | operand_4 MOD operand_5
751 {
752 $$ = 0; /* FIXME */
753 }
754 | operand_4 REM operand_5
755 {
756 $$ = 0; /* FIXME */
757 }
758 ;
759
760 /* Z.200, 5.3.8 */
761
762 operand_5 : operand_6
763 {
764 $$ = 0; /* FIXME */
765 }
766 | '-' operand_6
767 {
768 write_exp_elt_opcode (UNOP_NEG);
769 }
770 | NOT operand_6
771 {
772 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
773 }
774 | '(' integer_literal_expression ')' operand_6
775 {
776 $$ = 0; /* FIXME */
777 }
778 ;
779
780 /* Z.200, 5.3.9 */
781
782 operand_6 : POINTER location
783 {
784 $$ = 0; /* FIXME */
785 }
786 | RECEIVE buffer_location
787 {
788 $$ = 0; /* FIXME */
789 }
790 | primitive_value
791 {
792 $$ = 0; /* FIXME */
793 }
794 ;
795
796
797 /* Z.200, 6.2 */
798
799 single_assignment_action : location GDB_ASSIGNMENT value
800 {
801 write_exp_elt_opcode (BINOP_ASSIGN);
802 }
803
804 /* Z.200, 12.4.3 */
805 /* FIXME: For now we just accept only a single integer literal. */
806
807 integer_literal_expression:
808 INTEGER_LITERAL
809 {
810 $$ = 0;
811 }
812
813 /* Z.200, 12.4.3 */
814
815 array_primitive_value : primitive_value
816 {
817 $$ = 0;
818 }
819
820
821 /* Things which still need productions... */
822
823 synonym_name : FIXME { $$ = 0; }
824 value_enumeration_name : FIXME { $$ = 0; }
825 value_do_with_name : FIXME { $$ = 0; }
826 value_receive_name : FIXME { $$ = 0; }
827 string_primitive_value : FIXME { $$ = 0; }
828 start_element : FIXME { $$ = 0; }
829 left_element : FIXME { $$ = 0; }
830 right_element : FIXME { $$ = 0; }
831 slice_size : FIXME { $$ = 0; }
832 lower_element : FIXME { $$ = 0; }
833 upper_element : FIXME { $$ = 0; }
834 first_element : FIXME { $$ = 0; }
835 structure_primitive_value: FIXME { $$ = 0; }
836 field_name : FIXME { $$ = 0; }
837 mode_name : FIXME { $$ = 0; }
838 boolean_expression : FIXME { $$ = 0; }
839 case_selector_list : FIXME { $$ = 0; }
840 subexpression : FIXME { $$ = 0; }
841 case_label_specification: FIXME { $$ = 0; }
842 buffer_location : FIXME { $$ = 0; }
843
844 %%
845
846 /* Try to consume a simple name string token. If successful, returns
847 a pointer to a nullbyte terminated copy of the name that can be used
848 in symbol table lookups. If not successful, returns NULL. */
849
850 static char *
851 match_simple_name_string ()
852 {
853 char *tokptr = lexptr;
854
855 if (isalpha (*tokptr))
856 {
857 do {
858 tokptr++;
859 } while (isalpha (*tokptr) || isdigit (*tokptr) || (*tokptr == '_'));
860 yylval.sval.ptr = lexptr;
861 yylval.sval.length = tokptr - lexptr;
862 lexptr = tokptr;
863 return (copy_name (yylval.sval));
864 }
865 return (NULL);
866 }
867
868 /* Start looking for a value composed of valid digits as set by the base
869 in use. Note that '_' characters are valid anywhere, in any quantity,
870 and are simply ignored. Since we must find at least one valid digit,
871 or reject this token as an integer literal, we keep track of how many
872 digits we have encountered. */
873
874 static int
875 decode_integer_value (base, tokptrptr, ivalptr)
876 int base;
877 char **tokptrptr;
878 int *ivalptr;
879 {
880 char *tokptr = *tokptrptr;
881 int temp;
882 int digits = 0;
883
884 while (*tokptr != '\0')
885 {
886 temp = tolower (*tokptr);
887 tokptr++;
888 switch (temp)
889 {
890 case '_':
891 continue;
892 case '0': case '1': case '2': case '3': case '4':
893 case '5': case '6': case '7': case '8': case '9':
894 temp -= '0';
895 break;
896 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
897 temp -= 'a';
898 temp += 10;
899 break;
900 default:
901 temp = base;
902 break;
903 }
904 if (temp < base)
905 {
906 digits++;
907 *ivalptr *= base;
908 *ivalptr += temp;
909 }
910 else
911 {
912 /* Found something not in domain for current base. */
913 tokptr--; /* Unconsume what gave us indigestion. */
914 break;
915 }
916 }
917
918 /* If we didn't find any digits, then we don't have a valid integer
919 value, so reject the entire token. Otherwise, update the lexical
920 scan pointer, and return non-zero for success. */
921
922 if (digits == 0)
923 {
924 return (0);
925 }
926 else
927 {
928 *tokptrptr = tokptr;
929 return (1);
930 }
931 }
932
933 static int
934 decode_integer_literal (valptr, tokptrptr)
935 int *valptr;
936 char **tokptrptr;
937 {
938 char *tokptr = *tokptrptr;
939 int base = 0;
940 int ival = 0;
941 int explicit_base = 0;
942
943 /* Look for an explicit base specifier, which is optional. */
944
945 switch (*tokptr)
946 {
947 case 'd':
948 case 'D':
949 explicit_base++;
950 base = 10;
951 tokptr++;
952 break;
953 case 'b':
954 case 'B':
955 explicit_base++;
956 base = 2;
957 tokptr++;
958 break;
959 case 'h':
960 case 'H':
961 explicit_base++;
962 base = 16;
963 tokptr++;
964 break;
965 case 'o':
966 case 'O':
967 explicit_base++;
968 base = 8;
969 tokptr++;
970 break;
971 default:
972 base = 10;
973 break;
974 }
975
976 /* If we found an explicit base ensure that the character after the
977 explicit base is a single quote. */
978
979 if (explicit_base && (*tokptr++ != '\''))
980 {
981 return (0);
982 }
983
984 /* Attempt to decode whatever follows as an integer value in the
985 indicated base, updating the token pointer in the process and
986 computing the value into ival. Also, if we have an explicit
987 base, then the next character must not be a single quote, or we
988 have a bitstring literal, so reject the entire token in this case.
989 Otherwise, update the lexical scan pointer, and return non-zero
990 for success. */
991
992 if (!decode_integer_value (base, &tokptr, &ival))
993 {
994 return (0);
995 }
996 else if (explicit_base && (*tokptr == '\''))
997 {
998 return (0);
999 }
1000 else
1001 {
1002 *valptr = ival;
1003 *tokptrptr = tokptr;
1004 return (1);
1005 }
1006 }
1007
1008 /* Recognize a character literal. A character literal is single character
1009 or a control sequence, enclosed in single quotes. A control sequence
1010 is a comma separated list of one or more integer literals, enclosed
1011 in parenthesis and introduced with a circumflex character.
1012
1013 EX: 'a' '^(7)' '^(7,8)'
1014
1015 As a GNU chill extension, the syntax C'xx' is also recognized as a
1016 character literal, where xx is a hex value for the character.
1017
1018 Returns CHARACTER_LITERAL if a match is found.
1019 */
1020
1021 static int
1022 match_character_literal ()
1023 {
1024 char *tokptr = lexptr;
1025 int ival = 0;
1026
1027 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
1028 {
1029 /* We have a GNU chill extension form, so skip the leading "C'",
1030 decode the hex value, and then ensure that we have a trailing
1031 single quote character. */
1032 tokptr += 2;
1033 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1034 {
1035 return (0);
1036 }
1037 tokptr++;
1038 }
1039 else if (*tokptr == '\'')
1040 {
1041 tokptr++;
1042
1043 /* Determine which form we have, either a control sequence or the
1044 single character form. */
1045
1046 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1047 {
1048 /* Match and decode a control sequence. Return zero if we don't
1049 find a valid integer literal, or if the next unconsumed character
1050 after the integer literal is not the trailing ')'.
1051 FIXME: We currently don't handle the multiple integer literal
1052 form. */
1053 tokptr += 2;
1054 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1055 {
1056 return (0);
1057 }
1058 }
1059 else
1060 {
1061 ival = *tokptr++;
1062 }
1063
1064 /* The trailing quote has not yet been consumed. If we don't find
1065 it, then we have no match. */
1066
1067 if (*tokptr++ != '\'')
1068 {
1069 return (0);
1070 }
1071 }
1072 else
1073 {
1074 /* Not a character literal. */
1075 return (0);
1076 }
1077 yylval.typed_val.val = ival;
1078 yylval.typed_val.type = builtin_type_chill_char;
1079 lexptr = tokptr;
1080 return (CHARACTER_LITERAL);
1081 }
1082
1083 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1084 Note that according to 5.2.4.2, a single "_" is also a valid integer
1085 literal, however GNU-chill requires there to be at least one "digit"
1086 in any integer literal. */
1087
1088 static int
1089 match_integer_literal ()
1090 {
1091 char *tokptr = lexptr;
1092 int ival;
1093
1094 if (!decode_integer_literal (&ival, &tokptr))
1095 {
1096 return (0);
1097 }
1098 else
1099 {
1100 yylval.typed_val.val = ival;
1101 yylval.typed_val.type = builtin_type_int;
1102 lexptr = tokptr;
1103 return (INTEGER_LITERAL);
1104 }
1105 }
1106
1107 /* Recognize tokens that start with '$'. These include:
1108
1109 $regname A native register name or a "standard
1110 register name".
1111 Return token GDB_REGNAME.
1112
1113 $variable A convenience variable with a name chosen
1114 by the user.
1115 Return token GDB_VARIABLE.
1116
1117 $digits Value history with index <digits>, starting
1118 from the first value which has index 1.
1119 Return GDB_LAST.
1120
1121 $$digits Value history with index <digits> relative
1122 to the last value. I.E. $$0 is the last
1123 value, $$1 is the one previous to that, $$2
1124 is the one previous to $$1, etc.
1125 Return token GDB_LAST.
1126
1127 $ | $0 | $$0 The last value in the value history.
1128 Return token GDB_LAST.
1129
1130 $$ An abbreviation for the second to the last
1131 value in the value history, I.E. $$1
1132 Return token GDB_LAST.
1133
1134 Note that we currently assume that register names and convenience
1135 variables follow the convention of starting with a letter or '_'.
1136
1137 */
1138
1139 static int
1140 match_dollar_tokens ()
1141 {
1142 char *tokptr;
1143 int regno;
1144 int namelength;
1145 int negate;
1146 int ival;
1147
1148 /* We will always have a successful match, even if it is just for
1149 a single '$', the abbreviation for $$0. So advance lexptr. */
1150
1151 tokptr = ++lexptr;
1152
1153 if (*tokptr == '_' || isalpha (*tokptr))
1154 {
1155 /* Look for a match with a native register name, usually something
1156 like "r0" for example. */
1157
1158 for (regno = 0; regno < NUM_REGS; regno++)
1159 {
1160 namelength = strlen (reg_names[regno]);
1161 if (STREQN (tokptr, reg_names[regno], namelength)
1162 && !isalnum (tokptr[namelength]))
1163 {
1164 yylval.lval = regno;
1165 lexptr += namelength + 1;
1166 return (GDB_REGNAME);
1167 }
1168 }
1169
1170 /* Look for a match with a standard register name, usually something
1171 like "pc", which gdb always recognizes as the program counter
1172 regardless of what the native register name is. */
1173
1174 for (regno = 0; regno < num_std_regs; regno++)
1175 {
1176 namelength = strlen (std_regs[regno].name);
1177 if (STREQN (tokptr, std_regs[regno].name, namelength)
1178 && !isalnum (tokptr[namelength]))
1179 {
1180 yylval.lval = std_regs[regno].regnum;
1181 lexptr += namelength;
1182 return (GDB_REGNAME);
1183 }
1184 }
1185
1186 /* Attempt to match against a convenience variable. Note that
1187 this will always succeed, because if no variable of that name
1188 already exists, the lookup_internalvar will create one for us.
1189 Also note that both lexptr and tokptr currently point to the
1190 start of the input string we are trying to match, and that we
1191 have already tested the first character for non-numeric, so we
1192 don't have to treat it specially. */
1193
1194 while (*tokptr == '_' || isalnum (*tokptr))
1195 {
1196 tokptr++;
1197 }
1198 yylval.sval.ptr = lexptr;
1199 yylval.sval.length = tokptr - lexptr;
1200 yylval.ivar = lookup_internalvar (copy_name (yylval.sval));
1201 lexptr = tokptr;
1202 return (GDB_VARIABLE);
1203 }
1204
1205 /* Since we didn't match against a register name or convenience
1206 variable, our only choice left is a history value. */
1207
1208 if (*tokptr == '$')
1209 {
1210 negate = 1;
1211 ival = 1;
1212 tokptr++;
1213 }
1214 else
1215 {
1216 negate = 0;
1217 ival = 0;
1218 }
1219
1220 /* Attempt to decode more characters as an integer value giving
1221 the index in the history list. If successful, the value will
1222 overwrite ival (currently 0 or 1), and if not, ival will be
1223 left alone, which is good since it is currently correct for
1224 the '$' or '$$' case. */
1225
1226 decode_integer_literal (&ival, &tokptr);
1227 yylval.lval = negate ? -ival : ival;
1228 lexptr = tokptr;
1229 return (GDB_LAST);
1230 }
1231
1232 #if 0
1233 static void convert_float ()
1234 {
1235 extern double strtod ();
1236 double d;
1237 char tmp[256];
1238 char *p = yytext, *p1 = tmp;
1239 char c;
1240
1241 while (c = *p++)
1242 {
1243 switch (c)
1244 {
1245 case '_':
1246 break;
1247 case 'E':
1248 case 'd':
1249 case 'D':
1250 *p1++ = 'e';
1251 break;
1252 default:
1253 *p1++ = c;
1254 break;
1255 }
1256 }
1257 *p1 = '\0';
1258 d = strtod (tmp, &p1);
1259 if (*p1)
1260 {
1261 /* add error handling here */
1262 ;
1263 }
1264 yylval.dval = d;
1265 }
1266 #endif
1267
1268 /* Take care of parsing a number (anything that starts with a digit).
1269 Set yylval and return the token type; update lexptr.
1270 LEN is the number of characters in it. */
1271
1272 /*** Needs some error checking for the float case ***/
1273
1274 struct token
1275 {
1276 char *operator;
1277 int token;
1278 };
1279
1280 static const struct token tokentab5[] =
1281 {
1282 { "ANDIF", ANDIF }
1283 };
1284
1285 static const struct token tokentab4[] =
1286 {
1287 { "ORIF", ORIF }
1288 };
1289
1290 static const struct token tokentab3[] =
1291 {
1292 { "NOT", NOT },
1293 { "XOR", LOGXOR },
1294 { "AND", LOGAND }
1295 };
1296
1297 static const struct token tokentab2[] =
1298 {
1299 { ":=", GDB_ASSIGNMENT },
1300 { "//", SLASH_SLASH },
1301 { "/=", NOTEQUAL },
1302 { "<=", LEQ },
1303 { ">=", GTR },
1304 { "IN", IN },
1305 { "OR", LOGIOR }
1306 };
1307
1308 /* Read one token, getting characters through lexptr. */
1309 /* This is where we will check to make sure that the language and the
1310 operators used are compatible. */
1311
1312 static int
1313 yylex ()
1314 {
1315 unsigned int i;
1316 int token;
1317 char *simplename;
1318 struct symbol *sym;
1319
1320 /* Skip over any leading whitespace. */
1321 while (isspace (*lexptr))
1322 {
1323 lexptr++;
1324 }
1325 /* Look for special single character cases which can't be the first
1326 character of some other multicharacter token. */
1327 switch (*lexptr)
1328 {
1329 case '\0':
1330 return (0);
1331 case ',':
1332 case '.':
1333 case '=':
1334 case ';':
1335 case '!':
1336 case '+':
1337 case '-':
1338 case '*':
1339 case '/':
1340 case '(':
1341 case ')':
1342 case '[':
1343 case ']':
1344 return (*lexptr++);
1345 }
1346 /* Look for characters which start a particular kind of multicharacter
1347 token, such as a character literal, register name, convenience
1348 variable name, etc. */
1349 switch (*lexptr)
1350 {
1351 case 'C':
1352 case 'c':
1353 case '\'':
1354 token = match_character_literal ();
1355 if (token != 0)
1356 {
1357 return (token);
1358 }
1359 break;
1360 case '$':
1361 token = match_dollar_tokens ();
1362 if (token != 0)
1363 {
1364 return (token);
1365 }
1366 break;
1367 }
1368 /* See if it is a special token of length 5. */
1369 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1370 {
1371 if (STREQN (lexptr, tokentab5[i].operator, 5))
1372 {
1373 lexptr += 5;
1374 return (tokentab5[i].token);
1375 }
1376 }
1377 /* See if it is a special token of length 4. */
1378 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1379 {
1380 if (STREQN (lexptr, tokentab4[i].operator, 4))
1381 {
1382 lexptr += 4;
1383 return (tokentab4[i].token);
1384 }
1385 }
1386 /* See if it is a special token of length 3. */
1387 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1388 {
1389 if (STREQN (lexptr, tokentab3[i].operator, 3))
1390 {
1391 lexptr += 3;
1392 return (tokentab3[i].token);
1393 }
1394 }
1395 /* See if it is a special token of length 2. */
1396 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1397 {
1398 if (STREQN (lexptr, tokentab2[i].operator, 2))
1399 {
1400 lexptr += 2;
1401 return (tokentab2[i].token);
1402 }
1403 }
1404 /* Look for single character cases which which could be the first
1405 character of some other multicharacter token, but aren't, or we
1406 would already have found it. */
1407 switch (*lexptr)
1408 {
1409 case ':':
1410 case '/':
1411 case '<':
1412 case '>':
1413 return (*lexptr++);
1414 }
1415 /* Look for other special tokens. */
1416 if (STREQN (lexptr, "TRUE", 4)) /* FIXME: What about lowercase? */
1417 {
1418 yylval.ulval = 1;
1419 lexptr += 4;
1420 return (BOOLEAN_LITERAL);
1421 }
1422 if (STREQN (lexptr, "FALSE", 5)) /* FIXME: What about lowercase? */
1423 {
1424 yylval.ulval = 0;
1425 lexptr += 5;
1426 return (BOOLEAN_LITERAL);
1427 }
1428 token = match_integer_literal ();
1429 if (token != 0)
1430 {
1431 return (token);
1432 }
1433
1434 /* Try to match a simple name string, and if a match is found, then
1435 further classify what sort of name it is and return an appropriate
1436 token. Note that attempting to match a simple name string consumes
1437 the token from lexptr, so we can't back out if we later find that
1438 we can't classify what sort of name it is. */
1439
1440 simplename = match_simple_name_string ();
1441 if (simplename != NULL)
1442 {
1443 sym = lookup_symbol (simplename, expression_context_block,
1444 VAR_NAMESPACE, (int *) NULL,
1445 (struct symtab **) NULL);
1446 if (sym != NULL)
1447 {
1448 yylval.ssym.stoken.ptr = NULL;
1449 yylval.ssym.stoken.length = 0;
1450 yylval.ssym.sym = sym;
1451 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1452 switch (SYMBOL_CLASS (sym))
1453 {
1454 case LOC_BLOCK:
1455 /* Found a procedure name. */
1456 return (GENERAL_PROCEDURE_NAME);
1457 case LOC_STATIC:
1458 /* Found a global or local static variable. */
1459 return (LOCATION_NAME);
1460 case LOC_UNDEF:
1461 case LOC_CONST:
1462 case LOC_REGISTER:
1463 case LOC_ARG:
1464 case LOC_REF_ARG:
1465 case LOC_REGPARM:
1466 case LOC_LOCAL:
1467 case LOC_TYPEDEF:
1468 case LOC_LABEL:
1469 case LOC_CONST_BYTES:
1470 case LOC_LOCAL_ARG:
1471 break;
1472 }
1473 }
1474 else if (!have_full_symbols () && !have_partial_symbols ())
1475 {
1476 error ("No symbol table is loaded. Use the \"file\" command.");
1477 }
1478 else
1479 {
1480 error ("No symbol \"%s\" in current context.", simplename);
1481 }
1482 }
1483
1484 return (ILLEGAL_TOKEN);
1485 }
1486
1487 void
1488 yyerror (msg)
1489 char *msg; /* unused */
1490 {
1491 printf ("Parsing: %s\n", lexptr);
1492 if (yychar < 256)
1493 {
1494 error ("Invalid syntax in expression near character '%c'.", yychar);
1495 }
1496 else
1497 {
1498 error ("Invalid syntax in expression");
1499 }
1500 }
This page took 0.060573 seconds and 5 git commands to generate.