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