1 /* Parse expressions for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1994 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 /* Global variables declared in parser-defs.h (and commented there). */
43 struct expression *expout;
46 struct block *expression_context_block;
47 struct block *innermost_block;
48 struct block *block_found;
50 union type_stack_elt *type_stack;
51 int type_stack_depth, type_stack_size;
58 free_funcalls PARAMS ((void));
61 prefixify_expression PARAMS ((struct expression *));
64 length_of_subexp PARAMS ((struct expression *, int));
67 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
69 /* Data structure for saving values of arglist_len for function calls whose
70 arguments contain other function calls. */
78 static struct funcall *funcall_chain;
80 /* Assign machine-independent names to certain registers
81 (unless overridden by the REGISTER_NAMES table) */
84 unsigned num_std_regs = 0;
85 struct std_regs std_regs[1];
87 struct std_regs std_regs[] = {
104 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
109 /* Begin counting arguments for a function call,
110 saving the data about any containing call. */
115 register struct funcall *new;
117 new = (struct funcall *) xmalloc (sizeof (struct funcall));
118 new->next = funcall_chain;
119 new->arglist_len = arglist_len;
124 /* Return the number of arguments in a function call just terminated,
125 and restore the data for the containing function call. */
130 register int val = arglist_len;
131 register struct funcall *call = funcall_chain;
132 funcall_chain = call->next;
133 arglist_len = call->arglist_len;
138 /* Free everything in the funcall chain.
139 Used when there is an error inside parsing. */
144 register struct funcall *call, *next;
146 for (call = funcall_chain; call; call = next)
153 /* This page contains the functions for adding data to the struct expression
154 being constructed. */
156 /* Add one element to the end of the expression. */
158 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
159 a register through here */
162 write_exp_elt (expelt)
163 union exp_element expelt;
165 if (expout_ptr >= expout_size)
168 expout = (struct expression *)
169 xrealloc ((char *) expout, sizeof (struct expression)
170 + EXP_ELEM_TO_BYTES (expout_size));
172 expout->elts[expout_ptr++] = expelt;
176 write_exp_elt_opcode (expelt)
177 enum exp_opcode expelt;
179 union exp_element tmp;
187 write_exp_elt_sym (expelt)
188 struct symbol *expelt;
190 union exp_element tmp;
198 write_exp_elt_block (b)
201 union exp_element tmp;
207 write_exp_elt_longcst (expelt)
210 union exp_element tmp;
212 tmp.longconst = expelt;
218 write_exp_elt_dblcst (expelt)
221 union exp_element tmp;
223 tmp.doubleconst = expelt;
229 write_exp_elt_type (expelt)
232 union exp_element tmp;
240 write_exp_elt_intern (expelt)
241 struct internalvar *expelt;
243 union exp_element tmp;
245 tmp.internalvar = expelt;
250 /* Add a string constant to the end of the expression.
252 String constants are stored by first writing an expression element
253 that contains the length of the string, then stuffing the string
254 constant itself into however many expression elements are needed
255 to hold it, and then writing another expression element that contains
256 the length of the string. I.E. an expression element at each end of
257 the string records the string length, so you can skip over the
258 expression elements containing the actual string bytes from either
259 end of the string. Note that this also allows gdb to handle
260 strings with embedded null bytes, as is required for some languages.
262 Don't be fooled by the fact that the string is null byte terminated,
263 this is strictly for the convenience of debugging gdb itself. Gdb
264 Gdb does not depend up the string being null terminated, since the
265 actual length is recorded in expression elements at each end of the
266 string. The null byte is taken into consideration when computing how
267 many expression elements are required to hold the string constant, of
272 write_exp_string (str)
275 register int len = str.length;
277 register char *strdata;
279 /* Compute the number of expression elements required to hold the string
280 (including a null byte terminator), along with one expression element
281 at each end to record the actual string length (not including the
282 null byte terminator). */
284 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
286 /* Ensure that we have enough available expression elements to store
289 if ((expout_ptr + lenelt) >= expout_size)
291 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
292 expout = (struct expression *)
293 xrealloc ((char *) expout, (sizeof (struct expression)
294 + EXP_ELEM_TO_BYTES (expout_size)));
297 /* Write the leading length expression element (which advances the current
298 expression element index), then write the string constant followed by a
299 terminating null byte, and then write the trailing length expression
302 write_exp_elt_longcst ((LONGEST) len);
303 strdata = (char *) &expout->elts[expout_ptr];
304 memcpy (strdata, str.ptr, len);
305 *(strdata + len) = '\0';
306 expout_ptr += lenelt - 2;
307 write_exp_elt_longcst ((LONGEST) len);
310 /* Add a bitstring constant to the end of the expression.
312 Bitstring constants are stored by first writing an expression element
313 that contains the length of the bitstring (in bits), then stuffing the
314 bitstring constant itself into however many expression elements are
315 needed to hold it, and then writing another expression element that
316 contains the length of the bitstring. I.E. an expression element at
317 each end of the bitstring records the bitstring length, so you can skip
318 over the expression elements containing the actual bitstring bytes from
319 either end of the bitstring. */
322 write_exp_bitstring (str)
325 register int bits = str.length; /* length in bits */
326 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
328 register char *strdata;
330 /* Compute the number of expression elements required to hold the bitstring,
331 along with one expression element at each end to record the actual
332 bitstring length in bits. */
334 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
336 /* Ensure that we have enough available expression elements to store
339 if ((expout_ptr + lenelt) >= expout_size)
341 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
342 expout = (struct expression *)
343 xrealloc ((char *) expout, (sizeof (struct expression)
344 + EXP_ELEM_TO_BYTES (expout_size)));
347 /* Write the leading length expression element (which advances the current
348 expression element index), then write the bitstring constant, and then
349 write the trailing length expression element. */
351 write_exp_elt_longcst ((LONGEST) bits);
352 strdata = (char *) &expout->elts[expout_ptr];
353 memcpy (strdata, str.ptr, len);
354 expout_ptr += lenelt - 2;
355 write_exp_elt_longcst ((LONGEST) bits);
358 /* Type that corresponds to the address given in a minimal symbol. */
360 static struct type *msymbol_addr_type;
362 /* Add the appropriate elements for a minimal symbol to the end of
366 write_exp_msymbol (msymbol, text_symbol_type, data_symbol_type)
367 struct minimal_symbol *msymbol;
368 struct type *text_symbol_type;
369 struct type *data_symbol_type;
371 write_exp_elt_opcode (OP_LONG);
372 write_exp_elt_type (msymbol_addr_type);
373 write_exp_elt_longcst ((LONGEST) SYMBOL_VALUE_ADDRESS (msymbol));
374 write_exp_elt_opcode (OP_LONG);
376 write_exp_elt_opcode (UNOP_MEMVAL);
377 switch (msymbol -> type)
381 case mst_solib_trampoline:
382 write_exp_elt_type (text_symbol_type);
389 write_exp_elt_type (data_symbol_type);
393 write_exp_elt_type (builtin_type_char);
396 write_exp_elt_opcode (UNOP_MEMVAL);
399 /* Return a null-terminated temporary copy of the name
400 of a string token. */
406 memcpy (namecopy, token.ptr, token.length);
407 namecopy[token.length] = 0;
411 /* Reverse an expression from suffix form (in which it is constructed)
412 to prefix form (in which we can conveniently print or execute it). */
415 prefixify_expression (expr)
416 register struct expression *expr;
419 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
420 register struct expression *temp;
421 register int inpos = expr->nelts, outpos = 0;
423 temp = (struct expression *) alloca (len);
425 /* Copy the original expression into temp. */
426 memcpy (temp, expr, len);
428 prefixify_subexp (temp, expr, inpos, outpos);
431 /* Return the number of exp_elements in the subexpression of EXPR
432 whose last exp_element is at index ENDPOS - 1 in EXPR. */
435 length_of_subexp (expr, endpos)
436 register struct expression *expr;
439 register int oplen = 1;
440 register int args = 0;
444 error ("?error in length_of_subexp");
446 i = (int) expr->elts[endpos - 1].opcode;
452 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
453 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
472 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
499 case STRUCTOP_STRUCT:
505 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
506 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
510 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
511 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
512 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
517 args = longest_to_int (expr->elts[endpos - 2].longconst);
518 args -= longest_to_int (expr->elts[endpos - 3].longconst);
527 case MULTI_SUBSCRIPT:
529 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
532 case BINOP_ASSIGN_MODIFY:
543 args = 1 + (i < (int) BINOP_END);
548 oplen += length_of_subexp (expr, endpos - oplen);
555 /* Copy the subexpression ending just before index INEND in INEXPR
556 into OUTEXPR, starting at index OUTBEG.
557 In the process, convert it from suffix to prefix form. */
560 prefixify_subexp (inexpr, outexpr, inend, outbeg)
561 register struct expression *inexpr;
562 struct expression *outexpr;
566 register int oplen = 1;
567 register int args = 0;
570 enum exp_opcode opcode;
572 /* Compute how long the last operation is (in OPLEN),
573 and also how many preceding subexpressions serve as
574 arguments for it (in ARGS). */
576 opcode = inexpr->elts[inend - 1].opcode;
581 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
582 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
601 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
627 case STRUCTOP_STRUCT:
633 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
634 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
638 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
639 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
640 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
645 args = longest_to_int (inexpr->elts[inend - 2].longconst);
646 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
654 case BINOP_ASSIGN_MODIFY:
660 case MULTI_SUBSCRIPT:
662 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
671 args = 1 + ((int) opcode < (int) BINOP_END);
674 /* Copy the final operator itself, from the end of the input
675 to the beginning of the output. */
677 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
678 EXP_ELEM_TO_BYTES (oplen));
681 /* Find the lengths of the arg subexpressions. */
682 arglens = (int *) alloca (args * sizeof (int));
683 for (i = args - 1; i >= 0; i--)
685 oplen = length_of_subexp (inexpr, inend);
690 /* Now copy each subexpression, preserving the order of
691 the subexpressions, but prefixifying each one.
692 In this loop, inend starts at the beginning of
693 the expression this level is working on
694 and marches forward over the arguments.
695 outbeg does similarly in the output. */
696 for (i = 0; i < args; i++)
700 prefixify_subexp (inexpr, outexpr, inend, outbeg);
705 /* This page contains the two entry points to this file. */
707 /* Read an expression from the string *STRINGPTR points to,
708 parse it, and return a pointer to a struct expression that we malloc.
709 Use block BLOCK as the lexical context for variable names;
710 if BLOCK is zero, use the block of the selected stack frame.
711 Meanwhile, advance *STRINGPTR to point after the expression,
712 at the first nonwhite character that is not part of the expression
713 (possibly a null character).
715 If COMMA is nonzero, stop if a comma is reached. */
718 parse_exp_1 (stringptr, block, comma)
723 struct cleanup *old_chain;
728 type_stack_depth = 0;
730 comma_terminates = comma;
732 if (lexptr == 0 || *lexptr == 0)
733 error_no_arg ("expression to compute");
735 old_chain = make_cleanup (free_funcalls, 0);
738 expression_context_block = block ? block : get_selected_block ();
740 namecopy = (char *) alloca (strlen (lexptr) + 1);
743 expout = (struct expression *)
744 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
745 expout->language_defn = current_language;
746 make_cleanup (free_current_contents, &expout);
748 if (current_language->la_parser ())
749 current_language->la_error (NULL);
751 discard_cleanups (old_chain);
753 /* Record the actual number of expression elements, and then
754 reallocate the expression memory so that we free up any
757 expout->nelts = expout_ptr;
758 expout = (struct expression *)
759 xrealloc ((char *) expout,
760 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
762 /* Convert expression from postfix form as generated by yacc
763 parser, to a prefix form. */
765 DUMP_EXPRESSION (expout, gdb_stdout, "before conversion to prefix form");
766 prefixify_expression (expout);
767 DUMP_EXPRESSION (expout, gdb_stdout, "after conversion to prefix form");
773 /* Parse STRING as an expression, and complain if this fails
774 to use up all of the contents of STRING. */
777 parse_expression (string)
780 register struct expression *exp;
781 exp = parse_exp_1 (&string, 0, 0);
783 error ("Junk after end of expression.");
787 /* Stuff for maintaining a stack of types. Currently just used by C, but
788 probably useful for any language which declares its types "backwards". */
794 if (type_stack_depth == type_stack_size)
796 type_stack_size *= 2;
797 type_stack = (union type_stack_elt *)
798 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
800 type_stack[type_stack_depth++].piece = tp;
807 if (type_stack_depth == type_stack_size)
809 type_stack_size *= 2;
810 type_stack = (union type_stack_elt *)
811 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
813 type_stack[type_stack_depth++].int_val = n;
819 if (type_stack_depth)
820 return type_stack[--type_stack_depth].piece;
827 if (type_stack_depth)
828 return type_stack[--type_stack_depth].int_val;
829 /* "Can't happen". */
833 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
834 as modified by all the stuff on the stack. */
836 follow_types (follow_type)
837 struct type *follow_type;
841 struct type *range_type;
850 follow_type = lookup_pointer_type (follow_type);
853 follow_type = lookup_reference_type (follow_type);
856 array_size = pop_type_int ();
857 if (array_size != -1)
860 create_range_type ((struct type *) NULL,
864 create_array_type ((struct type *) NULL,
865 follow_type, range_type);
868 follow_type = lookup_pointer_type (follow_type);
871 follow_type = lookup_function_type (follow_type);
880 type_stack_size = 80;
881 type_stack_depth = 0;
882 type_stack = (union type_stack_elt *)
883 xmalloc (type_stack_size * sizeof (*type_stack));
885 /* We don't worry too much about what the name of this type is
886 because the name should rarely appear in output to the user. */
889 init_type (TYPE_CODE_PTR, TARGET_PTR_BIT / HOST_CHAR_BIT, 0,