1 /* Parse expressions for GDB.
2 Copyright (C) 1986, 89, 90, 91, 94, 1998 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 #include "symfile.h" /* for overlay functions */
44 /* Global variables declared in parser-defs.h (and commented there). */
45 struct expression *expout;
48 struct block *expression_context_block;
49 struct block *innermost_block;
51 union type_stack_elt *type_stack;
52 int type_stack_depth, type_stack_size;
58 #ifdef MAINTENANCE_CMDS
59 static int expressiondebug = 0;
63 free_funcalls PARAMS ((void));
66 prefixify_expression PARAMS ((struct expression *));
69 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
71 /* Data structure for saving values of arglist_len for function calls whose
72 arguments contain other function calls. */
80 static struct funcall *funcall_chain;
82 /* Assign machine-independent names to certain registers
83 (unless overridden by the REGISTER_NAMES table) */
86 unsigned num_std_regs = 0;
87 struct std_regs std_regs[1];
89 struct std_regs std_regs[] = {
106 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
110 /* The generic method for targets to specify how their registers are named.
111 The mapping can be derived from three sources: reg_names; std_regs; or
112 a target specific alias hook. */
115 target_map_name_to_register (str, len)
121 /* First try target specific aliases. We try these first because on some
122 systems standard names can be context dependent (eg. $pc on a
123 multiprocessor can be could be any of several PCs). */
124 #ifdef REGISTER_NAME_ALIAS_HOOK
125 i = REGISTER_NAME_ALIAS_HOOK (str, len);
130 /* Search architectural register name space. */
131 for (i = 0; i < NUM_REGS; i++)
132 if (reg_names[i] && len == strlen (reg_names[i])
133 && STREQN (str, reg_names[i], len))
138 /* Try standard aliases */
139 for (i = 0; i < num_std_regs; i++)
140 if (std_regs[i].name && len == strlen (std_regs[i].name)
141 && STREQN (str, std_regs[i].name, len))
143 return std_regs[i].regnum;
149 /* Begin counting arguments for a function call,
150 saving the data about any containing call. */
155 register struct funcall *new;
157 new = (struct funcall *) xmalloc (sizeof (struct funcall));
158 new->next = funcall_chain;
159 new->arglist_len = arglist_len;
164 /* Return the number of arguments in a function call just terminated,
165 and restore the data for the containing function call. */
170 register int val = arglist_len;
171 register struct funcall *call = funcall_chain;
172 funcall_chain = call->next;
173 arglist_len = call->arglist_len;
178 /* Free everything in the funcall chain.
179 Used when there is an error inside parsing. */
184 register struct funcall *call, *next;
186 for (call = funcall_chain; call; call = next)
193 /* This page contains the functions for adding data to the struct expression
194 being constructed. */
196 /* Add one element to the end of the expression. */
198 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
199 a register through here */
202 write_exp_elt (expelt)
203 union exp_element expelt;
205 if (expout_ptr >= expout_size)
208 expout = (struct expression *)
209 xrealloc ((char *) expout, sizeof (struct expression)
210 + EXP_ELEM_TO_BYTES (expout_size));
212 expout->elts[expout_ptr++] = expelt;
216 write_exp_elt_opcode (expelt)
217 enum exp_opcode expelt;
219 union exp_element tmp;
227 write_exp_elt_sym (expelt)
228 struct symbol *expelt;
230 union exp_element tmp;
238 write_exp_elt_block (b)
241 union exp_element tmp;
247 write_exp_elt_longcst (expelt)
250 union exp_element tmp;
252 tmp.longconst = expelt;
258 write_exp_elt_dblcst (expelt)
261 union exp_element tmp;
263 tmp.doubleconst = expelt;
269 write_exp_elt_type (expelt)
272 union exp_element tmp;
280 write_exp_elt_intern (expelt)
281 struct internalvar *expelt;
283 union exp_element tmp;
285 tmp.internalvar = expelt;
290 /* Add a string constant to the end of the expression.
292 String constants are stored by first writing an expression element
293 that contains the length of the string, then stuffing the string
294 constant itself into however many expression elements are needed
295 to hold it, and then writing another expression element that contains
296 the length of the string. I.E. an expression element at each end of
297 the string records the string length, so you can skip over the
298 expression elements containing the actual string bytes from either
299 end of the string. Note that this also allows gdb to handle
300 strings with embedded null bytes, as is required for some languages.
302 Don't be fooled by the fact that the string is null byte terminated,
303 this is strictly for the convenience of debugging gdb itself. Gdb
304 Gdb does not depend up the string being null terminated, since the
305 actual length is recorded in expression elements at each end of the
306 string. The null byte is taken into consideration when computing how
307 many expression elements are required to hold the string constant, of
312 write_exp_string (str)
315 register int len = str.length;
317 register char *strdata;
319 /* Compute the number of expression elements required to hold the string
320 (including a null byte terminator), along with one expression element
321 at each end to record the actual string length (not including the
322 null byte terminator). */
324 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
326 /* Ensure that we have enough available expression elements to store
329 if ((expout_ptr + lenelt) >= expout_size)
331 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
332 expout = (struct expression *)
333 xrealloc ((char *) expout, (sizeof (struct expression)
334 + EXP_ELEM_TO_BYTES (expout_size)));
337 /* Write the leading length expression element (which advances the current
338 expression element index), then write the string constant followed by a
339 terminating null byte, and then write the trailing length expression
342 write_exp_elt_longcst ((LONGEST) len);
343 strdata = (char *) &expout->elts[expout_ptr];
344 memcpy (strdata, str.ptr, len);
345 *(strdata + len) = '\0';
346 expout_ptr += lenelt - 2;
347 write_exp_elt_longcst ((LONGEST) len);
350 /* Add a bitstring constant to the end of the expression.
352 Bitstring constants are stored by first writing an expression element
353 that contains the length of the bitstring (in bits), then stuffing the
354 bitstring constant itself into however many expression elements are
355 needed to hold it, and then writing another expression element that
356 contains the length of the bitstring. I.E. an expression element at
357 each end of the bitstring records the bitstring length, so you can skip
358 over the expression elements containing the actual bitstring bytes from
359 either end of the bitstring. */
362 write_exp_bitstring (str)
365 register int bits = str.length; /* length in bits */
366 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
368 register char *strdata;
370 /* Compute the number of expression elements required to hold the bitstring,
371 along with one expression element at each end to record the actual
372 bitstring length in bits. */
374 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
376 /* Ensure that we have enough available expression elements to store
379 if ((expout_ptr + lenelt) >= expout_size)
381 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
382 expout = (struct expression *)
383 xrealloc ((char *) expout, (sizeof (struct expression)
384 + EXP_ELEM_TO_BYTES (expout_size)));
387 /* Write the leading length expression element (which advances the current
388 expression element index), then write the bitstring constant, and then
389 write the trailing length expression element. */
391 write_exp_elt_longcst ((LONGEST) bits);
392 strdata = (char *) &expout->elts[expout_ptr];
393 memcpy (strdata, str.ptr, len);
394 expout_ptr += lenelt - 2;
395 write_exp_elt_longcst ((LONGEST) bits);
398 /* Add the appropriate elements for a minimal symbol to the end of
399 the expression. The rationale behind passing in text_symbol_type and
400 data_symbol_type was so that Modula-2 could pass in WORD for
401 data_symbol_type. Perhaps it still is useful to have those types vary
402 based on the language, but they no longer have names like "int", so
403 the initial rationale is gone. */
405 static struct type *msym_text_symbol_type;
406 static struct type *msym_data_symbol_type;
407 static struct type *msym_unknown_symbol_type;
410 write_exp_msymbol (msymbol, text_symbol_type, data_symbol_type)
411 struct minimal_symbol *msymbol;
412 struct type *text_symbol_type;
413 struct type *data_symbol_type;
417 write_exp_elt_opcode (OP_LONG);
418 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
420 addr = SYMBOL_VALUE_ADDRESS (msymbol);
421 if (overlay_debugging)
422 addr = symbol_overlayed_address (addr, SYMBOL_BFD_SECTION (msymbol));
423 write_exp_elt_longcst ((LONGEST) addr);
425 write_exp_elt_opcode (OP_LONG);
427 write_exp_elt_opcode (UNOP_MEMVAL);
428 switch (msymbol -> type)
432 case mst_solib_trampoline:
433 write_exp_elt_type (msym_text_symbol_type);
440 write_exp_elt_type (msym_data_symbol_type);
444 write_exp_elt_type (msym_unknown_symbol_type);
447 write_exp_elt_opcode (UNOP_MEMVAL);
450 /* Recognize tokens that start with '$'. These include:
452 $regname A native register name or a "standard
455 $variable A convenience variable with a name chosen
458 $digits Value history with index <digits>, starting
459 from the first value which has index 1.
461 $$digits Value history with index <digits> relative
462 to the last value. I.E. $$0 is the last
463 value, $$1 is the one previous to that, $$2
464 is the one previous to $$1, etc.
466 $ | $0 | $$0 The last value in the value history.
468 $$ An abbreviation for the second to the last
469 value in the value history, I.E. $$1
474 write_dollar_variable (str)
477 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
478 and $$digits (equivalent to $<-digits> if you could type that). */
482 /* Double dollar means negate the number and add -1 as well.
483 Thus $$ alone means -1. */
484 if (str.length >= 2 && str.ptr[1] == '$')
491 /* Just dollars (one or two) */
495 /* Is the rest of the token digits? */
496 for (; i < str.length; i++)
497 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
501 i = atoi (str.ptr + 1 + negate);
507 /* Handle tokens that refer to machine registers:
508 $ followed by a register name. */
509 i = target_map_name_to_register( str.ptr + 1, str.length - 1 );
511 goto handle_register;
513 /* Any other names starting in $ are debugger internal variables. */
515 write_exp_elt_opcode (OP_INTERNALVAR);
516 write_exp_elt_intern (lookup_internalvar (copy_name (str) + 1));
517 write_exp_elt_opcode (OP_INTERNALVAR);
520 write_exp_elt_opcode (OP_LAST);
521 write_exp_elt_longcst ((LONGEST) i);
522 write_exp_elt_opcode (OP_LAST);
525 write_exp_elt_opcode (OP_REGISTER);
526 write_exp_elt_longcst (i);
527 write_exp_elt_opcode (OP_REGISTER);
531 /* Return a null-terminated temporary copy of the name
532 of a string token. */
538 memcpy (namecopy, token.ptr, token.length);
539 namecopy[token.length] = 0;
543 /* Reverse an expression from suffix form (in which it is constructed)
544 to prefix form (in which we can conveniently print or execute it). */
547 prefixify_expression (expr)
548 register struct expression *expr;
551 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
552 register struct expression *temp;
553 register int inpos = expr->nelts, outpos = 0;
555 temp = (struct expression *) alloca (len);
557 /* Copy the original expression into temp. */
558 memcpy (temp, expr, len);
560 prefixify_subexp (temp, expr, inpos, outpos);
563 /* Return the number of exp_elements in the subexpression of EXPR
564 whose last exp_element is at index ENDPOS - 1 in EXPR. */
567 length_of_subexp (expr, endpos)
568 register struct expression *expr;
571 register int oplen = 1;
572 register int args = 0;
576 error ("?error in length_of_subexp");
578 i = (int) expr->elts[endpos - 1].opcode;
584 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
585 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
608 case OP_F77_UNDETERMINED_ARGLIST:
610 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
638 case STRUCTOP_STRUCT:
646 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
647 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
651 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
652 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
653 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
658 args = longest_to_int (expr->elts[endpos - 2].longconst);
659 args -= longest_to_int (expr->elts[endpos - 3].longconst);
665 case TERNOP_SLICE_COUNT:
670 case MULTI_SUBSCRIPT:
672 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
675 case BINOP_ASSIGN_MODIFY:
686 args = 1 + (i < (int) BINOP_END);
691 oplen += length_of_subexp (expr, endpos - oplen);
698 /* Copy the subexpression ending just before index INEND in INEXPR
699 into OUTEXPR, starting at index OUTBEG.
700 In the process, convert it from suffix to prefix form. */
703 prefixify_subexp (inexpr, outexpr, inend, outbeg)
704 register struct expression *inexpr;
705 struct expression *outexpr;
709 register int oplen = 1;
710 register int args = 0;
713 enum exp_opcode opcode;
715 /* Compute how long the last operation is (in OPLEN),
716 and also how many preceding subexpressions serve as
717 arguments for it (in ARGS). */
719 opcode = inexpr->elts[inend - 1].opcode;
724 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
725 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
748 case OP_F77_UNDETERMINED_ARGLIST:
750 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
776 case STRUCTOP_STRUCT:
785 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
786 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
790 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
791 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
792 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
797 args = longest_to_int (inexpr->elts[inend - 2].longconst);
798 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
804 case TERNOP_SLICE_COUNT:
808 case BINOP_ASSIGN_MODIFY:
814 case MULTI_SUBSCRIPT:
816 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
825 args = 1 + ((int) opcode < (int) BINOP_END);
828 /* Copy the final operator itself, from the end of the input
829 to the beginning of the output. */
831 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
832 EXP_ELEM_TO_BYTES (oplen));
835 /* Find the lengths of the arg subexpressions. */
836 arglens = (int *) alloca (args * sizeof (int));
837 for (i = args - 1; i >= 0; i--)
839 oplen = length_of_subexp (inexpr, inend);
844 /* Now copy each subexpression, preserving the order of
845 the subexpressions, but prefixifying each one.
846 In this loop, inend starts at the beginning of
847 the expression this level is working on
848 and marches forward over the arguments.
849 outbeg does similarly in the output. */
850 for (i = 0; i < args; i++)
854 prefixify_subexp (inexpr, outexpr, inend, outbeg);
859 /* This page contains the two entry points to this file. */
861 /* Read an expression from the string *STRINGPTR points to,
862 parse it, and return a pointer to a struct expression that we malloc.
863 Use block BLOCK as the lexical context for variable names;
864 if BLOCK is zero, use the block of the selected stack frame.
865 Meanwhile, advance *STRINGPTR to point after the expression,
866 at the first nonwhite character that is not part of the expression
867 (possibly a null character).
869 If COMMA is nonzero, stop if a comma is reached. */
872 parse_exp_1 (stringptr, block, comma)
877 struct cleanup *old_chain;
882 type_stack_depth = 0;
884 comma_terminates = comma;
886 if (lexptr == 0 || *lexptr == 0)
887 error_no_arg ("expression to compute");
889 old_chain = make_cleanup ((make_cleanup_func) free_funcalls, 0);
892 expression_context_block = block ? block : get_selected_block ();
894 namecopy = (char *) alloca (strlen (lexptr) + 1);
897 expout = (struct expression *)
898 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
899 expout->language_defn = current_language;
900 make_cleanup ((make_cleanup_func) free_current_contents, &expout);
902 if (current_language->la_parser ())
903 current_language->la_error (NULL);
905 discard_cleanups (old_chain);
907 /* Record the actual number of expression elements, and then
908 reallocate the expression memory so that we free up any
911 expout->nelts = expout_ptr;
912 expout = (struct expression *)
913 xrealloc ((char *) expout,
914 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
916 /* Convert expression from postfix form as generated by yacc
917 parser, to a prefix form. */
919 #ifdef MAINTENANCE_CMDS
921 dump_prefix_expression (expout, gdb_stdout,
922 "before conversion to prefix form");
923 #endif /* MAINTENANCE_CMDS */
925 prefixify_expression (expout);
927 #ifdef MAINTENANCE_CMDS
929 dump_postfix_expression (expout, gdb_stdout,
930 "after conversion to prefix form");
931 #endif /* MAINTENANCE_CMDS */
937 /* Parse STRING as an expression, and complain if this fails
938 to use up all of the contents of STRING. */
941 parse_expression (string)
944 register struct expression *exp;
945 exp = parse_exp_1 (&string, 0, 0);
947 error ("Junk after end of expression.");
951 /* Stuff for maintaining a stack of types. Currently just used by C, but
952 probably useful for any language which declares its types "backwards". */
958 if (type_stack_depth == type_stack_size)
960 type_stack_size *= 2;
961 type_stack = (union type_stack_elt *)
962 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
964 type_stack[type_stack_depth++].piece = tp;
971 if (type_stack_depth == type_stack_size)
973 type_stack_size *= 2;
974 type_stack = (union type_stack_elt *)
975 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
977 type_stack[type_stack_depth++].int_val = n;
983 if (type_stack_depth)
984 return type_stack[--type_stack_depth].piece;
991 if (type_stack_depth)
992 return type_stack[--type_stack_depth].int_val;
993 /* "Can't happen". */
997 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
998 as modified by all the stuff on the stack. */
1000 follow_types (follow_type)
1001 struct type *follow_type;
1005 struct type *range_type;
1008 switch (pop_type ())
1014 follow_type = lookup_pointer_type (follow_type);
1017 follow_type = lookup_reference_type (follow_type);
1020 array_size = pop_type_int ();
1021 /* FIXME-type-allocation: need a way to free this type when we are
1024 create_range_type ((struct type *) NULL,
1025 builtin_type_int, 0,
1026 array_size >= 0 ? array_size - 1 : 0);
1028 create_array_type ((struct type *) NULL,
1029 follow_type, range_type);
1031 TYPE_ARRAY_UPPER_BOUND_TYPE(follow_type)
1032 = BOUND_CANNOT_BE_DETERMINED;
1035 /* FIXME-type-allocation: need a way to free this type when we are
1037 follow_type = lookup_function_type (follow_type);
1044 _initialize_parse ()
1046 type_stack_size = 80;
1047 type_stack_depth = 0;
1048 type_stack = (union type_stack_elt *)
1049 xmalloc (type_stack_size * sizeof (*type_stack));
1051 msym_text_symbol_type =
1052 init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
1053 TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
1054 msym_data_symbol_type =
1055 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
1056 "<data variable, no debug info>", NULL);
1057 msym_unknown_symbol_type =
1058 init_type (TYPE_CODE_INT, 1, 0,
1059 "<variable (not text or data), no debug info>",
1062 #ifdef MAINTENANCE_CMDS
1064 add_set_cmd ("expressiondebug", class_maintenance, var_zinteger,
1065 (char *)&expressiondebug,
1066 "Set expression debugging.\n\
1067 When non-zero, the internal representation of expressions will be printed.",