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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. */
32 #include "gdb_string.h"
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;
49 union type_stack_elt *type_stack;
50 int type_stack_depth, type_stack_size;
57 free_funcalls PARAMS ((void));
60 prefixify_expression PARAMS ((struct expression *));
63 length_of_subexp PARAMS ((struct expression *, int));
66 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
68 /* Data structure for saving values of arglist_len for function calls whose
69 arguments contain other function calls. */
77 static struct funcall *funcall_chain;
79 /* Assign machine-independent names to certain registers
80 (unless overridden by the REGISTER_NAMES table) */
83 unsigned num_std_regs = 0;
84 struct std_regs std_regs[1];
86 struct std_regs std_regs[] = {
103 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
108 /* Begin counting arguments for a function call,
109 saving the data about any containing call. */
114 register struct funcall *new;
116 new = (struct funcall *) xmalloc (sizeof (struct funcall));
117 new->next = funcall_chain;
118 new->arglist_len = arglist_len;
123 /* Return the number of arguments in a function call just terminated,
124 and restore the data for the containing function call. */
129 register int val = arglist_len;
130 register struct funcall *call = funcall_chain;
131 funcall_chain = call->next;
132 arglist_len = call->arglist_len;
137 /* Free everything in the funcall chain.
138 Used when there is an error inside parsing. */
143 register struct funcall *call, *next;
145 for (call = funcall_chain; call; call = next)
152 /* This page contains the functions for adding data to the struct expression
153 being constructed. */
155 /* Add one element to the end of the expression. */
157 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
158 a register through here */
161 write_exp_elt (expelt)
162 union exp_element expelt;
164 if (expout_ptr >= expout_size)
167 expout = (struct expression *)
168 xrealloc ((char *) expout, sizeof (struct expression)
169 + EXP_ELEM_TO_BYTES (expout_size));
171 expout->elts[expout_ptr++] = expelt;
175 write_exp_elt_opcode (expelt)
176 enum exp_opcode expelt;
178 union exp_element tmp;
186 write_exp_elt_sym (expelt)
187 struct symbol *expelt;
189 union exp_element tmp;
197 write_exp_elt_block (b)
200 union exp_element tmp;
206 write_exp_elt_longcst (expelt)
209 union exp_element tmp;
211 tmp.longconst = expelt;
217 write_exp_elt_dblcst (expelt)
220 union exp_element tmp;
222 tmp.doubleconst = expelt;
228 write_exp_elt_type (expelt)
231 union exp_element tmp;
239 write_exp_elt_intern (expelt)
240 struct internalvar *expelt;
242 union exp_element tmp;
244 tmp.internalvar = expelt;
249 /* Add a string constant to the end of the expression.
251 String constants are stored by first writing an expression element
252 that contains the length of the string, then stuffing the string
253 constant itself into however many expression elements are needed
254 to hold it, and then writing another expression element that contains
255 the length of the string. I.E. an expression element at each end of
256 the string records the string length, so you can skip over the
257 expression elements containing the actual string bytes from either
258 end of the string. Note that this also allows gdb to handle
259 strings with embedded null bytes, as is required for some languages.
261 Don't be fooled by the fact that the string is null byte terminated,
262 this is strictly for the convenience of debugging gdb itself. Gdb
263 Gdb does not depend up the string being null terminated, since the
264 actual length is recorded in expression elements at each end of the
265 string. The null byte is taken into consideration when computing how
266 many expression elements are required to hold the string constant, of
271 write_exp_string (str)
274 register int len = str.length;
276 register char *strdata;
278 /* Compute the number of expression elements required to hold the string
279 (including a null byte terminator), along with one expression element
280 at each end to record the actual string length (not including the
281 null byte terminator). */
283 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
285 /* Ensure that we have enough available expression elements to store
288 if ((expout_ptr + lenelt) >= expout_size)
290 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
291 expout = (struct expression *)
292 xrealloc ((char *) expout, (sizeof (struct expression)
293 + EXP_ELEM_TO_BYTES (expout_size)));
296 /* Write the leading length expression element (which advances the current
297 expression element index), then write the string constant followed by a
298 terminating null byte, and then write the trailing length expression
301 write_exp_elt_longcst ((LONGEST) len);
302 strdata = (char *) &expout->elts[expout_ptr];
303 memcpy (strdata, str.ptr, len);
304 *(strdata + len) = '\0';
305 expout_ptr += lenelt - 2;
306 write_exp_elt_longcst ((LONGEST) len);
309 /* Add a bitstring constant to the end of the expression.
311 Bitstring constants are stored by first writing an expression element
312 that contains the length of the bitstring (in bits), then stuffing the
313 bitstring constant itself into however many expression elements are
314 needed to hold it, and then writing another expression element that
315 contains the length of the bitstring. I.E. an expression element at
316 each end of the bitstring records the bitstring length, so you can skip
317 over the expression elements containing the actual bitstring bytes from
318 either end of the bitstring. */
321 write_exp_bitstring (str)
324 register int bits = str.length; /* length in bits */
325 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
327 register char *strdata;
329 /* Compute the number of expression elements required to hold the bitstring,
330 along with one expression element at each end to record the actual
331 bitstring length in bits. */
333 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
335 /* Ensure that we have enough available expression elements to store
338 if ((expout_ptr + lenelt) >= expout_size)
340 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
341 expout = (struct expression *)
342 xrealloc ((char *) expout, (sizeof (struct expression)
343 + EXP_ELEM_TO_BYTES (expout_size)));
346 /* Write the leading length expression element (which advances the current
347 expression element index), then write the bitstring constant, and then
348 write the trailing length expression element. */
350 write_exp_elt_longcst ((LONGEST) bits);
351 strdata = (char *) &expout->elts[expout_ptr];
352 memcpy (strdata, str.ptr, len);
353 expout_ptr += lenelt - 2;
354 write_exp_elt_longcst ((LONGEST) bits);
357 /* Add the appropriate elements for a minimal symbol to the end of
358 the expression. The rationale behind passing in text_symbol_type and
359 data_symbol_type was so that Modula-2 could pass in WORD for
360 data_symbol_type. Perhaps it still is useful to have those types vary
361 based on the language, but they no longer have names like "int", so
362 the initial rationale is gone. */
364 static struct type *msym_text_symbol_type;
365 static struct type *msym_data_symbol_type;
366 static struct type *msym_unknown_symbol_type;
369 write_exp_msymbol (msymbol, text_symbol_type, data_symbol_type)
370 struct minimal_symbol *msymbol;
371 struct type *text_symbol_type;
372 struct type *data_symbol_type;
374 write_exp_elt_opcode (OP_LONG);
375 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
376 write_exp_elt_longcst ((LONGEST) SYMBOL_VALUE_ADDRESS (msymbol));
377 write_exp_elt_opcode (OP_LONG);
379 write_exp_elt_opcode (UNOP_MEMVAL);
380 switch (msymbol -> type)
384 case mst_solib_trampoline:
385 write_exp_elt_type (msym_text_symbol_type);
392 write_exp_elt_type (msym_data_symbol_type);
396 write_exp_elt_type (msym_unknown_symbol_type);
399 write_exp_elt_opcode (UNOP_MEMVAL);
402 /* Return a null-terminated temporary copy of the name
403 of a string token. */
409 memcpy (namecopy, token.ptr, token.length);
410 namecopy[token.length] = 0;
414 /* Reverse an expression from suffix form (in which it is constructed)
415 to prefix form (in which we can conveniently print or execute it). */
418 prefixify_expression (expr)
419 register struct expression *expr;
422 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
423 register struct expression *temp;
424 register int inpos = expr->nelts, outpos = 0;
426 temp = (struct expression *) alloca (len);
428 /* Copy the original expression into temp. */
429 memcpy (temp, expr, len);
431 prefixify_subexp (temp, expr, inpos, outpos);
434 /* Return the number of exp_elements in the subexpression of EXPR
435 whose last exp_element is at index ENDPOS - 1 in EXPR. */
438 length_of_subexp (expr, endpos)
439 register struct expression *expr;
442 register int oplen = 1;
443 register int args = 0;
447 error ("?error in length_of_subexp");
449 i = (int) expr->elts[endpos - 1].opcode;
455 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
456 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
479 case OP_F77_UNDETERMINED_ARGLIST:
481 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
509 case STRUCTOP_STRUCT:
515 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
516 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
520 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
521 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
522 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
527 args = longest_to_int (expr->elts[endpos - 2].longconst);
528 args -= longest_to_int (expr->elts[endpos - 3].longconst);
534 case TERNOP_SLICE_COUNT:
539 case MULTI_SUBSCRIPT:
541 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
544 case BINOP_ASSIGN_MODIFY:
555 args = 1 + (i < (int) BINOP_END);
560 oplen += length_of_subexp (expr, endpos - oplen);
567 /* Copy the subexpression ending just before index INEND in INEXPR
568 into OUTEXPR, starting at index OUTBEG.
569 In the process, convert it from suffix to prefix form. */
572 prefixify_subexp (inexpr, outexpr, inend, outbeg)
573 register struct expression *inexpr;
574 struct expression *outexpr;
578 register int oplen = 1;
579 register int args = 0;
582 enum exp_opcode opcode;
584 /* Compute how long the last operation is (in OPLEN),
585 and also how many preceding subexpressions serve as
586 arguments for it (in ARGS). */
588 opcode = inexpr->elts[inend - 1].opcode;
593 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
594 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
617 case OP_F77_UNDETERMINED_ARGLIST:
619 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
645 case STRUCTOP_STRUCT:
652 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
653 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
657 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
658 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
659 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
664 args = longest_to_int (inexpr->elts[inend - 2].longconst);
665 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
671 case TERNOP_SLICE_COUNT:
675 case BINOP_ASSIGN_MODIFY:
681 case MULTI_SUBSCRIPT:
683 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
692 args = 1 + ((int) opcode < (int) BINOP_END);
695 /* Copy the final operator itself, from the end of the input
696 to the beginning of the output. */
698 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
699 EXP_ELEM_TO_BYTES (oplen));
702 /* Find the lengths of the arg subexpressions. */
703 arglens = (int *) alloca (args * sizeof (int));
704 for (i = args - 1; i >= 0; i--)
706 oplen = length_of_subexp (inexpr, inend);
711 /* Now copy each subexpression, preserving the order of
712 the subexpressions, but prefixifying each one.
713 In this loop, inend starts at the beginning of
714 the expression this level is working on
715 and marches forward over the arguments.
716 outbeg does similarly in the output. */
717 for (i = 0; i < args; i++)
721 prefixify_subexp (inexpr, outexpr, inend, outbeg);
726 /* This page contains the two entry points to this file. */
728 /* Read an expression from the string *STRINGPTR points to,
729 parse it, and return a pointer to a struct expression that we malloc.
730 Use block BLOCK as the lexical context for variable names;
731 if BLOCK is zero, use the block of the selected stack frame.
732 Meanwhile, advance *STRINGPTR to point after the expression,
733 at the first nonwhite character that is not part of the expression
734 (possibly a null character).
736 If COMMA is nonzero, stop if a comma is reached. */
739 parse_exp_1 (stringptr, block, comma)
744 struct cleanup *old_chain;
749 type_stack_depth = 0;
751 comma_terminates = comma;
753 if (lexptr == 0 || *lexptr == 0)
754 error_no_arg ("expression to compute");
756 old_chain = make_cleanup (free_funcalls, 0);
759 expression_context_block = block ? block : get_selected_block ();
761 namecopy = (char *) alloca (strlen (lexptr) + 1);
764 expout = (struct expression *)
765 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
766 expout->language_defn = current_language;
767 make_cleanup (free_current_contents, &expout);
769 if (current_language->la_parser ())
770 current_language->la_error (NULL);
772 discard_cleanups (old_chain);
774 /* Record the actual number of expression elements, and then
775 reallocate the expression memory so that we free up any
778 expout->nelts = expout_ptr;
779 expout = (struct expression *)
780 xrealloc ((char *) expout,
781 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
783 /* Convert expression from postfix form as generated by yacc
784 parser, to a prefix form. */
786 DUMP_EXPRESSION (expout, gdb_stdout, "before conversion to prefix form");
787 prefixify_expression (expout);
788 DUMP_EXPRESSION (expout, gdb_stdout, "after conversion to prefix form");
794 /* Parse STRING as an expression, and complain if this fails
795 to use up all of the contents of STRING. */
798 parse_expression (string)
801 register struct expression *exp;
802 exp = parse_exp_1 (&string, 0, 0);
804 error ("Junk after end of expression.");
808 /* Stuff for maintaining a stack of types. Currently just used by C, but
809 probably useful for any language which declares its types "backwards". */
815 if (type_stack_depth == type_stack_size)
817 type_stack_size *= 2;
818 type_stack = (union type_stack_elt *)
819 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
821 type_stack[type_stack_depth++].piece = tp;
828 if (type_stack_depth == type_stack_size)
830 type_stack_size *= 2;
831 type_stack = (union type_stack_elt *)
832 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
834 type_stack[type_stack_depth++].int_val = n;
840 if (type_stack_depth)
841 return type_stack[--type_stack_depth].piece;
848 if (type_stack_depth)
849 return type_stack[--type_stack_depth].int_val;
850 /* "Can't happen". */
854 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
855 as modified by all the stuff on the stack. */
857 follow_types (follow_type)
858 struct type *follow_type;
862 struct type *range_type;
871 follow_type = lookup_pointer_type (follow_type);
874 follow_type = lookup_reference_type (follow_type);
877 array_size = pop_type_int ();
878 /* FIXME-type-allocation: need a way to free this type when we are
881 create_range_type ((struct type *) NULL,
883 array_size >= 0 ? array_size - 1 : 0);
885 create_array_type ((struct type *) NULL,
886 follow_type, range_type);
888 TYPE_ARRAY_UPPER_BOUND_TYPE(follow_type)
889 = BOUND_CANNOT_BE_DETERMINED;
892 /* FIXME-type-allocation: need a way to free this type when we are
894 follow_type = lookup_function_type (follow_type);
903 type_stack_size = 80;
904 type_stack_depth = 0;
905 type_stack = (union type_stack_elt *)
906 xmalloc (type_stack_size * sizeof (*type_stack));
908 msym_text_symbol_type =
909 init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
910 TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
911 msym_data_symbol_type =
912 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
913 "<data variable, no debug info>", NULL);
914 msym_unknown_symbol_type =
915 init_type (TYPE_CODE_INT, 1, 0,
916 "<variable (not text or data), no debug info>",