Tue Sep 28 09:45:38 1993 Peter Schauer (pes@regent.e-technik.tu-muenchen.de)
[deliverable/binutils-gdb.git] / gdb / parse.c
1 /* Parse expressions for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
3 Modified from expread.y by the Department of Computer Science at the
4 State University of New York at Buffalo, 1991.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 /* Parse an expression from text in a string,
23 and return the result as a struct expression pointer.
24 That structure contains arithmetic operations in reverse polish,
25 with constants represented by operations that are followed by special data.
26 See expression.h for the details of the format.
27 What is important here is that it can be built up sequentially
28 during the process of parsing; the lower levels of the tree always
29 come first in the result. */
30
31 #include "defs.h"
32 #include "symtab.h"
33 #include "gdbtypes.h"
34 #include "frame.h"
35 #include "expression.h"
36 #include "value.h"
37 #include "command.h"
38 #include "language.h"
39 #include "parser-defs.h"
40
41 static void
42 free_funcalls PARAMS ((void));
43
44 static void
45 prefixify_expression PARAMS ((struct expression *));
46
47 static int
48 length_of_subexp PARAMS ((struct expression *, int));
49
50 static void
51 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
52
53 /* Data structure for saving values of arglist_len for function calls whose
54 arguments contain other function calls. */
55
56 struct funcall
57 {
58 struct funcall *next;
59 int arglist_len;
60 };
61
62 static struct funcall *funcall_chain;
63
64 /* Assign machine-independent names to certain registers
65 (unless overridden by the REGISTER_NAMES table) */
66
67 #ifdef NO_STD_REGS
68 unsigned num_std_regs = 0;
69 struct std_regs std_regs[1];
70 #else
71 struct std_regs std_regs[] = {
72
73 #ifdef PC_REGNUM
74 { "pc", PC_REGNUM },
75 #endif
76 #ifdef FP_REGNUM
77 { "fp", FP_REGNUM },
78 #endif
79 #ifdef SP_REGNUM
80 { "sp", SP_REGNUM },
81 #endif
82 #ifdef PS_REGNUM
83 { "ps", PS_REGNUM },
84 #endif
85
86 };
87
88 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
89
90 #endif
91
92
93 /* Begin counting arguments for a function call,
94 saving the data about any containing call. */
95
96 void
97 start_arglist ()
98 {
99 register struct funcall *new;
100
101 new = (struct funcall *) xmalloc (sizeof (struct funcall));
102 new->next = funcall_chain;
103 new->arglist_len = arglist_len;
104 arglist_len = 0;
105 funcall_chain = new;
106 }
107
108 /* Return the number of arguments in a function call just terminated,
109 and restore the data for the containing function call. */
110
111 int
112 end_arglist ()
113 {
114 register int val = arglist_len;
115 register struct funcall *call = funcall_chain;
116 funcall_chain = call->next;
117 arglist_len = call->arglist_len;
118 free ((PTR)call);
119 return val;
120 }
121
122 /* Free everything in the funcall chain.
123 Used when there is an error inside parsing. */
124
125 static void
126 free_funcalls ()
127 {
128 register struct funcall *call, *next;
129
130 for (call = funcall_chain; call; call = next)
131 {
132 next = call->next;
133 free ((PTR)call);
134 }
135 }
136 \f
137 /* This page contains the functions for adding data to the struct expression
138 being constructed. */
139
140 /* Add one element to the end of the expression. */
141
142 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
143 a register through here */
144
145 void
146 write_exp_elt (expelt)
147 union exp_element expelt;
148 {
149 if (expout_ptr >= expout_size)
150 {
151 expout_size *= 2;
152 expout = (struct expression *)
153 xrealloc ((char *) expout, sizeof (struct expression)
154 + EXP_ELEM_TO_BYTES (expout_size));
155 }
156 expout->elts[expout_ptr++] = expelt;
157 }
158
159 void
160 write_exp_elt_opcode (expelt)
161 enum exp_opcode expelt;
162 {
163 union exp_element tmp;
164
165 tmp.opcode = expelt;
166
167 write_exp_elt (tmp);
168 }
169
170 void
171 write_exp_elt_sym (expelt)
172 struct symbol *expelt;
173 {
174 union exp_element tmp;
175
176 tmp.symbol = expelt;
177
178 write_exp_elt (tmp);
179 }
180
181 void
182 write_exp_elt_block (b)
183 struct block *b;
184 {
185 union exp_element tmp;
186 tmp.block = b;
187 write_exp_elt (tmp);
188 }
189
190 void
191 write_exp_elt_longcst (expelt)
192 LONGEST expelt;
193 {
194 union exp_element tmp;
195
196 tmp.longconst = expelt;
197
198 write_exp_elt (tmp);
199 }
200
201 void
202 write_exp_elt_dblcst (expelt)
203 double expelt;
204 {
205 union exp_element tmp;
206
207 tmp.doubleconst = expelt;
208
209 write_exp_elt (tmp);
210 }
211
212 void
213 write_exp_elt_type (expelt)
214 struct type *expelt;
215 {
216 union exp_element tmp;
217
218 tmp.type = expelt;
219
220 write_exp_elt (tmp);
221 }
222
223 void
224 write_exp_elt_intern (expelt)
225 struct internalvar *expelt;
226 {
227 union exp_element tmp;
228
229 tmp.internalvar = expelt;
230
231 write_exp_elt (tmp);
232 }
233
234 /* Add a string constant to the end of the expression.
235
236 String constants are stored by first writing an expression element
237 that contains the length of the string, then stuffing the string
238 constant itself into however many expression elements are needed
239 to hold it, and then writing another expression element that contains
240 the length of the string. I.E. an expression element at each end of
241 the string records the string length, so you can skip over the
242 expression elements containing the actual string bytes from either
243 end of the string. Note that this also allows gdb to handle
244 strings with embedded null bytes, as is required for some languages.
245
246 Don't be fooled by the fact that the string is null byte terminated,
247 this is strictly for the convenience of debugging gdb itself. Gdb
248 Gdb does not depend up the string being null terminated, since the
249 actual length is recorded in expression elements at each end of the
250 string. The null byte is taken into consideration when computing how
251 many expression elements are required to hold the string constant, of
252 course. */
253
254
255 void
256 write_exp_string (str)
257 struct stoken str;
258 {
259 register int len = str.length;
260 register int lenelt;
261 register char *strdata;
262
263 /* Compute the number of expression elements required to hold the string
264 (including a null byte terminator), along with one expression element
265 at each end to record the actual string length (not including the
266 null byte terminator). */
267
268 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
269
270 /* Ensure that we have enough available expression elements to store
271 everything. */
272
273 if ((expout_ptr + lenelt) >= expout_size)
274 {
275 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
276 expout = (struct expression *)
277 xrealloc ((char *) expout, (sizeof (struct expression)
278 + EXP_ELEM_TO_BYTES (expout_size)));
279 }
280
281 /* Write the leading length expression element (which advances the current
282 expression element index), then write the string constant followed by a
283 terminating null byte, and then write the trailing length expression
284 element. */
285
286 write_exp_elt_longcst ((LONGEST) len);
287 strdata = (char *) &expout->elts[expout_ptr];
288 memcpy (strdata, str.ptr, len);
289 *(strdata + len) = '\0';
290 expout_ptr += lenelt - 2;
291 write_exp_elt_longcst ((LONGEST) len);
292 }
293
294 /* Add a bitstring constant to the end of the expression.
295
296 Bitstring constants are stored by first writing an expression element
297 that contains the length of the bitstring (in bits), then stuffing the
298 bitstring constant itself into however many expression elements are
299 needed to hold it, and then writing another expression element that
300 contains the length of the bitstring. I.E. an expression element at
301 each end of the bitstring records the bitstring length, so you can skip
302 over the expression elements containing the actual bitstring bytes from
303 either end of the bitstring. */
304
305 void
306 write_exp_bitstring (str)
307 struct stoken str;
308 {
309 register int bits = str.length; /* length in bits */
310 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
311 register int lenelt;
312 register char *strdata;
313
314 /* Compute the number of expression elements required to hold the bitstring,
315 along with one expression element at each end to record the actual
316 bitstring length in bits. */
317
318 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
319
320 /* Ensure that we have enough available expression elements to store
321 everything. */
322
323 if ((expout_ptr + lenelt) >= expout_size)
324 {
325 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
326 expout = (struct expression *)
327 xrealloc ((char *) expout, (sizeof (struct expression)
328 + EXP_ELEM_TO_BYTES (expout_size)));
329 }
330
331 /* Write the leading length expression element (which advances the current
332 expression element index), then write the bitstring constant, and then
333 write the trailing length expression element. */
334
335 write_exp_elt_longcst ((LONGEST) bits);
336 strdata = (char *) &expout->elts[expout_ptr];
337 memcpy (strdata, str.ptr, len);
338 expout_ptr += lenelt - 2;
339 write_exp_elt_longcst ((LONGEST) bits);
340 }
341 \f
342 /* Return a null-terminated temporary copy of the name
343 of a string token. */
344
345 char *
346 copy_name (token)
347 struct stoken token;
348 {
349 memcpy (namecopy, token.ptr, token.length);
350 namecopy[token.length] = 0;
351 return namecopy;
352 }
353 \f
354 /* Reverse an expression from suffix form (in which it is constructed)
355 to prefix form (in which we can conveniently print or execute it). */
356
357 static void
358 prefixify_expression (expr)
359 register struct expression *expr;
360 {
361 register int len =
362 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
363 register struct expression *temp;
364 register int inpos = expr->nelts, outpos = 0;
365
366 temp = (struct expression *) alloca (len);
367
368 /* Copy the original expression into temp. */
369 memcpy (temp, expr, len);
370
371 prefixify_subexp (temp, expr, inpos, outpos);
372 }
373
374 /* Return the number of exp_elements in the subexpression of EXPR
375 whose last exp_element is at index ENDPOS - 1 in EXPR. */
376
377 static int
378 length_of_subexp (expr, endpos)
379 register struct expression *expr;
380 register int endpos;
381 {
382 register int oplen = 1;
383 register int args = 0;
384 register int i;
385
386 if (endpos < 1)
387 error ("?error in length_of_subexp");
388
389 i = (int) expr->elts[endpos - 1].opcode;
390
391 switch (i)
392 {
393 /* C++ */
394 case OP_SCOPE:
395 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
396 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
397 break;
398
399 case OP_LONG:
400 case OP_DOUBLE:
401 case OP_VAR_VALUE:
402 oplen = 4;
403 break;
404
405 case OP_TYPE:
406 case OP_BOOL:
407 case OP_LAST:
408 case OP_REGISTER:
409 case OP_INTERNALVAR:
410 oplen = 3;
411 break;
412
413 case OP_FUNCALL:
414 oplen = 3;
415 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
416 break;
417
418 case UNOP_MAX:
419 case UNOP_MIN:
420 oplen = 3;
421 break;
422
423 case BINOP_VAL:
424 case UNOP_CAST:
425 case UNOP_MEMVAL:
426 oplen = 3;
427 args = 1;
428 break;
429
430 case UNOP_ABS:
431 case UNOP_CAP:
432 case UNOP_CHR:
433 case UNOP_FLOAT:
434 case UNOP_HIGH:
435 case UNOP_ODD:
436 case UNOP_ORD:
437 case UNOP_TRUNC:
438 oplen = 1;
439 args = 1;
440 break;
441
442 case STRUCTOP_STRUCT:
443 case STRUCTOP_PTR:
444 args = 1;
445 /* fall through */
446 case OP_M2_STRING:
447 case OP_STRING:
448 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
449 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
450 break;
451
452 case OP_BITSTRING:
453 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
454 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
455 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
456 break;
457
458 case OP_ARRAY:
459 oplen = 4;
460 args = longest_to_int (expr->elts[endpos - 2].longconst);
461 args -= longest_to_int (expr->elts[endpos - 3].longconst);
462 args += 1;
463 break;
464
465 case TERNOP_COND:
466 args = 3;
467 break;
468
469 /* Modula-2 */
470 case MULTI_SUBSCRIPT:
471 oplen=3;
472 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
473 break;
474
475 case BINOP_ASSIGN_MODIFY:
476 oplen = 3;
477 args = 2;
478 break;
479
480 /* C++ */
481 case OP_THIS:
482 oplen = 2;
483 break;
484
485 default:
486 args = 1 + (i < (int) BINOP_END);
487 }
488
489 while (args > 0)
490 {
491 oplen += length_of_subexp (expr, endpos - oplen);
492 args--;
493 }
494
495 return oplen;
496 }
497
498 /* Copy the subexpression ending just before index INEND in INEXPR
499 into OUTEXPR, starting at index OUTBEG.
500 In the process, convert it from suffix to prefix form. */
501
502 static void
503 prefixify_subexp (inexpr, outexpr, inend, outbeg)
504 register struct expression *inexpr;
505 struct expression *outexpr;
506 register int inend;
507 int outbeg;
508 {
509 register int oplen = 1;
510 register int args = 0;
511 register int i;
512 int *arglens;
513 enum exp_opcode opcode;
514
515 /* Compute how long the last operation is (in OPLEN),
516 and also how many preceding subexpressions serve as
517 arguments for it (in ARGS). */
518
519 opcode = inexpr->elts[inend - 1].opcode;
520 switch (opcode)
521 {
522 /* C++ */
523 case OP_SCOPE:
524 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
525 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
526 break;
527
528 case OP_LONG:
529 case OP_DOUBLE:
530 case OP_VAR_VALUE:
531 oplen = 4;
532 break;
533
534 case OP_TYPE:
535 case OP_BOOL:
536 case OP_LAST:
537 case OP_REGISTER:
538 case OP_INTERNALVAR:
539 oplen = 3;
540 break;
541
542 case OP_FUNCALL:
543 oplen = 3;
544 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
545 break;
546
547 case UNOP_MIN:
548 case UNOP_MAX:
549 oplen = 3;
550 break;
551
552 case UNOP_CAST:
553 case UNOP_MEMVAL:
554 oplen = 3;
555 args = 1;
556 break;
557
558 case UNOP_ABS:
559 case UNOP_CAP:
560 case UNOP_CHR:
561 case UNOP_FLOAT:
562 case UNOP_HIGH:
563 case UNOP_ODD:
564 case UNOP_ORD:
565 case UNOP_TRUNC:
566 oplen=1;
567 args=1;
568 break;
569
570 case STRUCTOP_STRUCT:
571 case STRUCTOP_PTR:
572 args = 1;
573 /* fall through */
574 case OP_M2_STRING:
575 case OP_STRING:
576 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
577 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
578 break;
579
580 case OP_BITSTRING:
581 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
582 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
583 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
584 break;
585
586 case OP_ARRAY:
587 oplen = 4;
588 args = longest_to_int (inexpr->elts[inend - 2].longconst);
589 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
590 args += 1;
591 break;
592
593 case TERNOP_COND:
594 args = 3;
595 break;
596
597 case BINOP_ASSIGN_MODIFY:
598 oplen = 3;
599 args = 2;
600 break;
601
602 /* Modula-2 */
603 case MULTI_SUBSCRIPT:
604 oplen=3;
605 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
606 break;
607
608 /* C++ */
609 case OP_THIS:
610 oplen = 2;
611 break;
612
613 default:
614 args = 1 + ((int) opcode < (int) BINOP_END);
615 }
616
617 /* Copy the final operator itself, from the end of the input
618 to the beginning of the output. */
619 inend -= oplen;
620 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
621 EXP_ELEM_TO_BYTES (oplen));
622 outbeg += oplen;
623
624 /* Find the lengths of the arg subexpressions. */
625 arglens = (int *) alloca (args * sizeof (int));
626 for (i = args - 1; i >= 0; i--)
627 {
628 oplen = length_of_subexp (inexpr, inend);
629 arglens[i] = oplen;
630 inend -= oplen;
631 }
632
633 /* Now copy each subexpression, preserving the order of
634 the subexpressions, but prefixifying each one.
635 In this loop, inend starts at the beginning of
636 the expression this level is working on
637 and marches forward over the arguments.
638 outbeg does similarly in the output. */
639 for (i = 0; i < args; i++)
640 {
641 oplen = arglens[i];
642 inend += oplen;
643 prefixify_subexp (inexpr, outexpr, inend, outbeg);
644 outbeg += oplen;
645 }
646 }
647 \f
648 /* This page contains the two entry points to this file. */
649
650 /* Read an expression from the string *STRINGPTR points to,
651 parse it, and return a pointer to a struct expression that we malloc.
652 Use block BLOCK as the lexical context for variable names;
653 if BLOCK is zero, use the block of the selected stack frame.
654 Meanwhile, advance *STRINGPTR to point after the expression,
655 at the first nonwhite character that is not part of the expression
656 (possibly a null character).
657
658 If COMMA is nonzero, stop if a comma is reached. */
659
660 struct expression *
661 parse_exp_1 (stringptr, block, comma)
662 char **stringptr;
663 struct block *block;
664 int comma;
665 {
666 struct cleanup *old_chain;
667
668 lexptr = *stringptr;
669
670 paren_depth = 0;
671 type_stack_depth = 0;
672
673 comma_terminates = comma;
674
675 if (lexptr == 0 || *lexptr == 0)
676 error_no_arg ("expression to compute");
677
678 old_chain = make_cleanup (free_funcalls, 0);
679 funcall_chain = 0;
680
681 expression_context_block = block ? block : get_selected_block ();
682
683 namecopy = (char *) alloca (strlen (lexptr) + 1);
684 expout_size = 10;
685 expout_ptr = 0;
686 expout = (struct expression *)
687 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
688 expout->language_defn = current_language;
689 make_cleanup (free_current_contents, &expout);
690
691 if (current_language->la_parser ())
692 current_language->la_error (NULL);
693
694 discard_cleanups (old_chain);
695
696 /* Record the actual number of expression elements, and then
697 reallocate the expression memory so that we free up any
698 excess elements. */
699
700 expout->nelts = expout_ptr;
701 expout = (struct expression *)
702 xrealloc ((char *) expout,
703 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
704
705 /* Convert expression from postfix form as generated by yacc
706 parser, to a prefix form. */
707
708 DUMP_EXPRESSION (expout, stdout, "before conversion to prefix form");
709 prefixify_expression (expout);
710 DUMP_EXPRESSION (expout, stdout, "after conversion to prefix form");
711
712 *stringptr = lexptr;
713 return expout;
714 }
715
716 /* Parse STRING as an expression, and complain if this fails
717 to use up all of the contents of STRING. */
718
719 struct expression *
720 parse_expression (string)
721 char *string;
722 {
723 register struct expression *exp;
724 exp = parse_exp_1 (&string, 0, 0);
725 if (*string)
726 error ("Junk after end of expression.");
727 return exp;
728 }
729
730 void
731 push_type (tp)
732 enum type_pieces tp;
733 {
734 if (type_stack_depth == type_stack_size)
735 {
736 type_stack_size *= 2;
737 type_stack = (union type_stack_elt *)
738 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
739 }
740 type_stack[type_stack_depth++].piece = tp;
741 }
742
743 void
744 push_type_int (n)
745 int n;
746 {
747 if (type_stack_depth == type_stack_size)
748 {
749 type_stack_size *= 2;
750 type_stack = (union type_stack_elt *)
751 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
752 }
753 type_stack[type_stack_depth++].int_val = n;
754 }
755
756 enum type_pieces
757 pop_type ()
758 {
759 if (type_stack_depth)
760 return type_stack[--type_stack_depth].piece;
761 return tp_end;
762 }
763
764 int
765 pop_type_int ()
766 {
767 if (type_stack_depth)
768 return type_stack[--type_stack_depth].int_val;
769 /* "Can't happen". */
770 return 0;
771 }
772
773 void
774 _initialize_parse ()
775 {
776 type_stack_size = 80;
777 type_stack_depth = 0;
778 type_stack = (union type_stack_elt *)
779 xmalloc (type_stack_size * sizeof (*type_stack));
780 }
This page took 0.061216 seconds and 4 git commands to generate.