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.
6 This file is part of GDB.
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.
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.
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. */
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. */
36 #include "expression.h"
40 #include "parser-defs.h"
43 prefixify_expression PARAMS ((struct expression *));
46 length_of_subexp PARAMS ((struct expression *, int));
49 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
51 /* Assign machine-independent names to certain registers
52 (unless overridden by the REGISTER_NAMES table) */
54 struct std_regs std_regs[] = {
69 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
72 /* Begin counting arguments for a function call,
73 saving the data about any containing call. */
78 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
80 new->next = funcall_chain;
81 new->arglist_len = arglist_len;
86 /* Return the number of arguments in a function call just terminated,
87 and restore the data for the containing function call. */
92 register int val = arglist_len;
93 register struct funcall *call = funcall_chain;
94 funcall_chain = call->next;
95 arglist_len = call->arglist_len;
100 /* Free everything in the funcall chain.
101 Used when there is an error inside parsing. */
106 register struct funcall *call, *next;
108 for (call = funcall_chain; call; call = next)
115 /* This page contains the functions for adding data to the struct expression
116 being constructed. */
118 /* Add one element to the end of the expression. */
120 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
121 a register through here */
124 write_exp_elt (expelt)
125 union exp_element expelt;
127 if (expout_ptr >= expout_size)
130 expout = (struct expression *) xrealloc ((char *) expout,
131 sizeof (struct expression)
132 + expout_size * sizeof (union exp_element));
134 expout->elts[expout_ptr++] = expelt;
138 write_exp_elt_opcode (expelt)
139 enum exp_opcode expelt;
141 union exp_element tmp;
149 write_exp_elt_sym (expelt)
150 struct symbol *expelt;
152 union exp_element tmp;
160 write_exp_elt_longcst (expelt)
163 union exp_element tmp;
165 tmp.longconst = expelt;
171 write_exp_elt_dblcst (expelt)
174 union exp_element tmp;
176 tmp.doubleconst = expelt;
182 write_exp_elt_type (expelt)
185 union exp_element tmp;
193 write_exp_elt_intern (expelt)
194 struct internalvar *expelt;
196 union exp_element tmp;
198 tmp.internalvar = expelt;
203 /* Add a string constant to the end of the expression.
204 Follow it by its length in bytes, as a separate exp_element. */
207 write_exp_string (str)
210 register int len = str.length;
212 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
214 expout_ptr += lenelt;
216 if (expout_ptr >= expout_size)
218 expout_size = max (expout_size * 2, expout_ptr + 10);
219 expout = (struct expression *)
220 xrealloc ((char *) expout, (sizeof (struct expression)
221 + (expout_size * sizeof (union exp_element))));
223 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
224 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
225 write_exp_elt_longcst ((LONGEST) len);
228 /* Return a null-terminated temporary copy of the name
229 of a string token. */
235 bcopy (token.ptr, namecopy, token.length);
236 namecopy[token.length] = 0;
240 /* Reverse an expression from suffix form (in which it is constructed)
241 to prefix form (in which we can conveniently print or execute it). */
244 prefixify_expression (expr)
245 register struct expression *expr;
247 register int len = sizeof (struct expression) +
248 expr->nelts * sizeof (union exp_element);
249 register struct expression *temp;
250 register int inpos = expr->nelts, outpos = 0;
252 temp = (struct expression *) alloca (len);
254 /* Copy the original expression into temp. */
255 bcopy (expr, temp, len);
257 prefixify_subexp (temp, expr, inpos, outpos);
260 /* Return the number of exp_elements in the subexpression of EXPR
261 whose last exp_element is at index ENDPOS - 1 in EXPR. */
264 length_of_subexp (expr, endpos)
265 register struct expression *expr;
268 register int oplen = 1;
269 register int args = 0;
273 error ("?error in length_of_subexp");
275 i = (int) expr->elts[endpos - 1].opcode;
281 oplen = 4 + ((expr->elts[endpos - 2].longconst
282 + sizeof (union exp_element))
283 / sizeof (union exp_element));
302 args = 1 + expr->elts[endpos - 2].longconst;
330 case STRUCTOP_STRUCT:
335 oplen = 3 + ((expr->elts[endpos - 2].longconst
336 + sizeof (union exp_element))
337 / sizeof (union exp_element));
345 case BINOP_MULTI_SUBSCRIPT:
347 args = 1 + expr->elts[endpos- 2].longconst;
350 case BINOP_ASSIGN_MODIFY:
361 args = 1 + (i < (int) BINOP_END);
366 oplen += length_of_subexp (expr, endpos - oplen);
373 /* Copy the subexpression ending just before index INEND in INEXPR
374 into OUTEXPR, starting at index OUTBEG.
375 In the process, convert it from suffix to prefix form. */
378 prefixify_subexp (inexpr, outexpr, inend, outbeg)
379 register struct expression *inexpr;
380 struct expression *outexpr;
384 register int oplen = 1;
385 register int args = 0;
388 enum exp_opcode opcode;
390 /* Compute how long the last operation is (in OPLEN),
391 and also how many preceding subexpressions serve as
392 arguments for it (in ARGS). */
394 opcode = inexpr->elts[inend - 1].opcode;
399 oplen = 4 + ((inexpr->elts[inend - 2].longconst
400 + sizeof (union exp_element))
401 / sizeof (union exp_element));
420 args = 1 + inexpr->elts[inend - 2].longconst;
447 case STRUCTOP_STRUCT:
452 oplen = 3 + ((inexpr->elts[inend - 2].longconst
453 + sizeof (union exp_element))
454 / sizeof (union exp_element));
462 case BINOP_ASSIGN_MODIFY:
468 case BINOP_MULTI_SUBSCRIPT:
470 args = 1 + inexpr->elts[inend - 2].longconst;
479 args = 1 + ((int) opcode < (int) BINOP_END);
482 /* Copy the final operator itself, from the end of the input
483 to the beginning of the output. */
485 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
486 oplen * sizeof (union exp_element));
489 /* Find the lengths of the arg subexpressions. */
490 arglens = (int *) alloca (args * sizeof (int));
491 for (i = args - 1; i >= 0; i--)
493 oplen = length_of_subexp (inexpr, inend);
498 /* Now copy each subexpression, preserving the order of
499 the subexpressions, but prefixifying each one.
500 In this loop, inend starts at the beginning of
501 the expression this level is working on
502 and marches forward over the arguments.
503 outbeg does similarly in the output. */
504 for (i = 0; i < args; i++)
508 prefixify_subexp (inexpr, outexpr, inend, outbeg);
513 /* This page contains the two entry points to this file. */
515 /* Read an expression from the string *STRINGPTR points to,
516 parse it, and return a pointer to a struct expression that we malloc.
517 Use block BLOCK as the lexical context for variable names;
518 if BLOCK is zero, use the block of the selected stack frame.
519 Meanwhile, advance *STRINGPTR to point after the expression,
520 at the first nonwhite character that is not part of the expression
521 (possibly a null character).
523 If COMMA is nonzero, stop if a comma is reached. */
526 parse_exp_1 (stringptr, block, comma)
531 struct cleanup *old_chain;
536 type_stack_depth = 0;
538 comma_terminates = comma;
540 if (lexptr == 0 || *lexptr == 0)
541 error_no_arg ("expression to compute");
543 old_chain = make_cleanup (free_funcalls, 0);
546 expression_context_block = block ? block : get_selected_block ();
548 namecopy = (char *) alloca (strlen (lexptr) + 1);
551 expout = (struct expression *)
552 xmalloc (sizeof (struct expression)
553 + expout_size * sizeof (union exp_element));
554 expout->language_defn = current_language;
555 make_cleanup (free_current_contents, &expout);
557 if (current_language->la_parser ())
558 current_language->la_error (NULL);
560 discard_cleanups (old_chain);
561 expout->nelts = expout_ptr;
562 expout = (struct expression *)
563 xrealloc ((char *) expout,
564 sizeof (struct expression)
565 + expout_ptr * sizeof (union exp_element));
566 prefixify_expression (expout);
571 /* Parse STRING as an expression, and complain if this fails
572 to use up all of the contents of STRING. */
575 parse_expression (string)
578 register struct expression *exp;
579 exp = parse_exp_1 (&string, 0, 0);
581 error ("Junk after end of expression.");
589 if (type_stack_depth == type_stack_size)
591 type_stack_size *= 2;
592 type_stack = (union type_stack_elt *)
593 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
595 type_stack[type_stack_depth++].piece = tp;
602 if (type_stack_depth == type_stack_size)
604 type_stack_size *= 2;
605 type_stack = (union type_stack_elt *)
606 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
608 type_stack[type_stack_depth++].int_val = n;
614 if (type_stack_depth)
615 return type_stack[--type_stack_depth].piece;
622 if (type_stack_depth)
623 return type_stack[--type_stack_depth].int_val;
624 /* "Can't happen". */
631 type_stack_size = 80;
632 type_stack_depth = 0;
633 type_stack = (union type_stack_elt *)
634 xmalloc (type_stack_size * sizeof (*type_stack));