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"
42 /* Assign machine-independent names to certain registers
43 (unless overridden by the REGISTER_NAMES table) */
45 struct std_regs std_regs[] = {
60 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
63 /* Begin counting arguments for a function call,
64 saving the data about any containing call. */
69 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
71 new->next = funcall_chain;
72 new->arglist_len = arglist_len;
77 /* Return the number of arguments in a function call just terminated,
78 and restore the data for the containing function call. */
83 register int val = arglist_len;
84 register struct funcall *call = funcall_chain;
85 funcall_chain = call->next;
86 arglist_len = call->arglist_len;
91 /* Free everything in the funcall chain.
92 Used when there is an error inside parsing. */
97 register struct funcall *call, *next;
99 for (call = funcall_chain; call; call = next)
106 /* This page contains the functions for adding data to the struct expression
107 being constructed. */
109 /* Add one element to the end of the expression. */
111 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
112 a register through here */
115 write_exp_elt (expelt)
116 union exp_element expelt;
118 if (expout_ptr >= expout_size)
121 expout = (struct expression *) xrealloc (expout,
122 sizeof (struct expression)
123 + expout_size * sizeof (union exp_element));
125 expout->elts[expout_ptr++] = expelt;
129 write_exp_elt_opcode (expelt)
130 enum exp_opcode expelt;
132 union exp_element tmp;
140 write_exp_elt_sym (expelt)
141 struct symbol *expelt;
143 union exp_element tmp;
151 write_exp_elt_longcst (expelt)
154 union exp_element tmp;
156 tmp.longconst = expelt;
162 write_exp_elt_dblcst (expelt)
165 union exp_element tmp;
167 tmp.doubleconst = expelt;
173 write_exp_elt_type (expelt)
176 union exp_element tmp;
184 write_exp_elt_intern (expelt)
185 struct internalvar *expelt;
187 union exp_element tmp;
189 tmp.internalvar = expelt;
194 /* Add a string constant to the end of the expression.
195 Follow it by its length in bytes, as a separate exp_element. */
198 write_exp_string (str)
201 register int len = str.length;
203 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
205 expout_ptr += lenelt;
207 if (expout_ptr >= expout_size)
209 expout_size = max (expout_size * 2, expout_ptr + 10);
210 expout = (struct expression *)
211 xrealloc (expout, (sizeof (struct expression)
212 + (expout_size * sizeof (union exp_element))));
214 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
215 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
216 write_exp_elt_longcst ((LONGEST) len);
219 /* Return a null-terminated temporary copy of the name
220 of a string token. */
226 bcopy (token.ptr, namecopy, token.length);
227 namecopy[token.length] = 0;
231 /* Reverse an expression from suffix form (in which it is constructed)
232 to prefix form (in which we can conveniently print or execute it). */
234 static void prefixify_subexp ();
237 prefixify_expression (expr)
238 register struct expression *expr;
240 register int len = sizeof (struct expression) +
241 expr->nelts * sizeof (union exp_element);
242 register struct expression *temp;
243 register int inpos = expr->nelts, outpos = 0;
245 temp = (struct expression *) alloca (len);
247 /* Copy the original expression into temp. */
248 bcopy (expr, temp, len);
250 prefixify_subexp (temp, expr, inpos, outpos);
253 /* Return the number of exp_elements in the subexpression of EXPR
254 whose last exp_element is at index ENDPOS - 1 in EXPR. */
257 length_of_subexp (expr, endpos)
258 register struct expression *expr;
261 register int oplen = 1;
262 register int args = 0;
266 error ("?error in length_of_subexp");
268 i = (int) expr->elts[endpos - 1].opcode;
274 oplen = 4 + ((expr->elts[endpos - 2].longconst
275 + sizeof (union exp_element))
276 / sizeof (union exp_element));
295 args = 1 + expr->elts[endpos - 2].longconst;
323 case STRUCTOP_STRUCT:
328 oplen = 3 + ((expr->elts[endpos - 2].longconst
329 + sizeof (union exp_element))
330 / sizeof (union exp_element));
338 case BINOP_MULTI_SUBSCRIPT:
340 args = 1 + expr->elts[endpos- 2].longconst;
343 case BINOP_ASSIGN_MODIFY:
354 args = 1 + (i < (int) BINOP_END);
359 oplen += length_of_subexp (expr, endpos - oplen);
366 /* Copy the subexpression ending just before index INEND in INEXPR
367 into OUTEXPR, starting at index OUTBEG.
368 In the process, convert it from suffix to prefix form. */
371 prefixify_subexp (inexpr, outexpr, inend, outbeg)
372 register struct expression *inexpr;
373 struct expression *outexpr;
377 register int oplen = 1;
378 register int args = 0;
381 enum exp_opcode opcode;
383 /* Compute how long the last operation is (in OPLEN),
384 and also how many preceding subexpressions serve as
385 arguments for it (in ARGS). */
387 opcode = inexpr->elts[inend - 1].opcode;
392 oplen = 4 + ((inexpr->elts[inend - 2].longconst
393 + sizeof (union exp_element))
394 / sizeof (union exp_element));
413 args = 1 + inexpr->elts[inend - 2].longconst;
440 case STRUCTOP_STRUCT:
445 oplen = 3 + ((inexpr->elts[inend - 2].longconst
446 + sizeof (union exp_element))
447 / sizeof (union exp_element));
455 case BINOP_ASSIGN_MODIFY:
461 case BINOP_MULTI_SUBSCRIPT:
463 args = 1 + inexpr->elts[inend - 2].longconst;
472 args = 1 + ((int) opcode < (int) BINOP_END);
475 /* Copy the final operator itself, from the end of the input
476 to the beginning of the output. */
478 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
479 oplen * sizeof (union exp_element));
482 /* Find the lengths of the arg subexpressions. */
483 arglens = (int *) alloca (args * sizeof (int));
484 for (i = args - 1; i >= 0; i--)
486 oplen = length_of_subexp (inexpr, inend);
491 /* Now copy each subexpression, preserving the order of
492 the subexpressions, but prefixifying each one.
493 In this loop, inend starts at the beginning of
494 the expression this level is working on
495 and marches forward over the arguments.
496 outbeg does similarly in the output. */
497 for (i = 0; i < args; i++)
501 prefixify_subexp (inexpr, outexpr, inend, outbeg);
506 /* This page contains the two entry points to this file. */
508 /* Read an expression from the string *STRINGPTR points to,
509 parse it, and return a pointer to a struct expression that we malloc.
510 Use block BLOCK as the lexical context for variable names;
511 if BLOCK is zero, use the block of the selected stack frame.
512 Meanwhile, advance *STRINGPTR to point after the expression,
513 at the first nonwhite character that is not part of the expression
514 (possibly a null character).
516 If COMMA is nonzero, stop if a comma is reached. */
519 parse_exp_1 (stringptr, block, comma)
524 struct cleanup *old_chain;
529 type_stack_depth = 0;
531 comma_terminates = comma;
533 if (lexptr == 0 || *lexptr == 0)
534 error_no_arg ("expression to compute");
536 old_chain = make_cleanup (free_funcalls, 0);
539 expression_context_block = block ? block : get_selected_block ();
541 namecopy = (char *) alloca (strlen (lexptr) + 1);
544 expout = (struct expression *)
545 xmalloc (sizeof (struct expression)
546 + expout_size * sizeof (union exp_element));
547 expout->language_defn = current_language;
548 make_cleanup (free_current_contents, &expout);
550 if (current_language->la_parser ())
551 current_language->la_error (NULL);
553 discard_cleanups (old_chain);
554 expout->nelts = expout_ptr;
555 expout = (struct expression *)
557 sizeof (struct expression)
558 + expout_ptr * sizeof (union exp_element));
559 prefixify_expression (expout);
564 /* Parse STRING as an expression, and complain if this fails
565 to use up all of the contents of STRING. */
568 parse_expression (string)
571 register struct expression *exp;
572 exp = parse_exp_1 (&string, 0, 0);
574 error ("Junk after end of expression.");
582 if (type_stack_depth == type_stack_size)
584 type_stack_size *= 2;
585 type_stack = (union type_stack_elt *)
586 xrealloc (type_stack, type_stack_size * sizeof (*type_stack));
588 type_stack[type_stack_depth++].piece = tp;
595 if (type_stack_depth == type_stack_size)
597 type_stack_size *= 2;
598 type_stack = (union type_stack_elt *)
599 xrealloc (type_stack, type_stack_size * sizeof (*type_stack));
601 type_stack[type_stack_depth++].int_val = n;
607 if (type_stack_depth)
608 return type_stack[--type_stack_depth].piece;
615 if (type_stack_depth)
616 return type_stack[--type_stack_depth].int_val;
617 /* "Can't happen". */
624 type_stack_size = 80;
625 type_stack_depth = 0;
626 type_stack = (union type_stack_elt *)
627 xmalloc (type_stack_size * sizeof (*type_stack));