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 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
65 /* Data structure for saving values of arglist_len for function calls whose
66 arguments contain other function calls. */
74 static struct funcall *funcall_chain;
76 /* Assign machine-independent names to certain registers
77 (unless overridden by the REGISTER_NAMES table) */
80 unsigned num_std_regs = 0;
81 struct std_regs std_regs[1];
83 struct std_regs std_regs[] = {
100 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
104 /* The generic method for targets to specify how their registers are named.
105 The mapping can be derived from three sources: reg_names; std_regs; or
106 a target specific alias hook. */
109 target_map_name_to_register (str, len)
115 /* First search architectural register name space. */
116 for (i = 0; i < NUM_REGS; i++)
117 if (reg_names[i] && len == strlen (reg_names[i])
118 && STREQN (str, reg_names[i], len))
123 /* Try standard aliases */
124 for (i = 0; i < num_std_regs; i++)
125 if (std_regs[i].name && len == strlen (std_regs[i].name)
126 && STREQN (str, std_regs[i].name, len))
128 return std_regs[i].regnum;
131 /* Try target specific aliases */
132 #ifdef REGISTER_NAME_ALIAS_HOOK
133 return REGISTER_NAME_ALIAS_HOOK (str, len);
139 /* Begin counting arguments for a function call,
140 saving the data about any containing call. */
145 register struct funcall *new;
147 new = (struct funcall *) xmalloc (sizeof (struct funcall));
148 new->next = funcall_chain;
149 new->arglist_len = arglist_len;
154 /* Return the number of arguments in a function call just terminated,
155 and restore the data for the containing function call. */
160 register int val = arglist_len;
161 register struct funcall *call = funcall_chain;
162 funcall_chain = call->next;
163 arglist_len = call->arglist_len;
168 /* Free everything in the funcall chain.
169 Used when there is an error inside parsing. */
174 register struct funcall *call, *next;
176 for (call = funcall_chain; call; call = next)
183 /* This page contains the functions for adding data to the struct expression
184 being constructed. */
186 /* Add one element to the end of the expression. */
188 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
189 a register through here */
192 write_exp_elt (expelt)
193 union exp_element expelt;
195 if (expout_ptr >= expout_size)
198 expout = (struct expression *)
199 xrealloc ((char *) expout, sizeof (struct expression)
200 + EXP_ELEM_TO_BYTES (expout_size));
202 expout->elts[expout_ptr++] = expelt;
206 write_exp_elt_opcode (expelt)
207 enum exp_opcode expelt;
209 union exp_element tmp;
217 write_exp_elt_sym (expelt)
218 struct symbol *expelt;
220 union exp_element tmp;
228 write_exp_elt_block (b)
231 union exp_element tmp;
237 write_exp_elt_longcst (expelt)
240 union exp_element tmp;
242 tmp.longconst = expelt;
248 write_exp_elt_dblcst (expelt)
251 union exp_element tmp;
253 tmp.doubleconst = expelt;
259 write_exp_elt_type (expelt)
262 union exp_element tmp;
270 write_exp_elt_intern (expelt)
271 struct internalvar *expelt;
273 union exp_element tmp;
275 tmp.internalvar = expelt;
280 /* Add a string constant to the end of the expression.
282 String constants are stored by first writing an expression element
283 that contains the length of the string, then stuffing the string
284 constant itself into however many expression elements are needed
285 to hold it, and then writing another expression element that contains
286 the length of the string. I.E. an expression element at each end of
287 the string records the string length, so you can skip over the
288 expression elements containing the actual string bytes from either
289 end of the string. Note that this also allows gdb to handle
290 strings with embedded null bytes, as is required for some languages.
292 Don't be fooled by the fact that the string is null byte terminated,
293 this is strictly for the convenience of debugging gdb itself. Gdb
294 Gdb does not depend up the string being null terminated, since the
295 actual length is recorded in expression elements at each end of the
296 string. The null byte is taken into consideration when computing how
297 many expression elements are required to hold the string constant, of
302 write_exp_string (str)
305 register int len = str.length;
307 register char *strdata;
309 /* Compute the number of expression elements required to hold the string
310 (including a null byte terminator), along with one expression element
311 at each end to record the actual string length (not including the
312 null byte terminator). */
314 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
316 /* Ensure that we have enough available expression elements to store
319 if ((expout_ptr + lenelt) >= expout_size)
321 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
322 expout = (struct expression *)
323 xrealloc ((char *) expout, (sizeof (struct expression)
324 + EXP_ELEM_TO_BYTES (expout_size)));
327 /* Write the leading length expression element (which advances the current
328 expression element index), then write the string constant followed by a
329 terminating null byte, and then write the trailing length expression
332 write_exp_elt_longcst ((LONGEST) len);
333 strdata = (char *) &expout->elts[expout_ptr];
334 memcpy (strdata, str.ptr, len);
335 *(strdata + len) = '\0';
336 expout_ptr += lenelt - 2;
337 write_exp_elt_longcst ((LONGEST) len);
340 /* Add a bitstring constant to the end of the expression.
342 Bitstring constants are stored by first writing an expression element
343 that contains the length of the bitstring (in bits), then stuffing the
344 bitstring constant itself into however many expression elements are
345 needed to hold it, and then writing another expression element that
346 contains the length of the bitstring. I.E. an expression element at
347 each end of the bitstring records the bitstring length, so you can skip
348 over the expression elements containing the actual bitstring bytes from
349 either end of the bitstring. */
352 write_exp_bitstring (str)
355 register int bits = str.length; /* length in bits */
356 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
358 register char *strdata;
360 /* Compute the number of expression elements required to hold the bitstring,
361 along with one expression element at each end to record the actual
362 bitstring length in bits. */
364 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
366 /* Ensure that we have enough available expression elements to store
369 if ((expout_ptr + lenelt) >= expout_size)
371 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
372 expout = (struct expression *)
373 xrealloc ((char *) expout, (sizeof (struct expression)
374 + EXP_ELEM_TO_BYTES (expout_size)));
377 /* Write the leading length expression element (which advances the current
378 expression element index), then write the bitstring constant, and then
379 write the trailing length expression element. */
381 write_exp_elt_longcst ((LONGEST) bits);
382 strdata = (char *) &expout->elts[expout_ptr];
383 memcpy (strdata, str.ptr, len);
384 expout_ptr += lenelt - 2;
385 write_exp_elt_longcst ((LONGEST) bits);
388 /* Add the appropriate elements for a minimal symbol to the end of
389 the expression. The rationale behind passing in text_symbol_type and
390 data_symbol_type was so that Modula-2 could pass in WORD for
391 data_symbol_type. Perhaps it still is useful to have those types vary
392 based on the language, but they no longer have names like "int", so
393 the initial rationale is gone. */
395 static struct type *msym_text_symbol_type;
396 static struct type *msym_data_symbol_type;
397 static struct type *msym_unknown_symbol_type;
400 write_exp_msymbol (msymbol, text_symbol_type, data_symbol_type)
401 struct minimal_symbol *msymbol;
402 struct type *text_symbol_type;
403 struct type *data_symbol_type;
405 write_exp_elt_opcode (OP_LONG);
406 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
407 write_exp_elt_longcst ((LONGEST) SYMBOL_VALUE_ADDRESS (msymbol));
408 write_exp_elt_opcode (OP_LONG);
410 write_exp_elt_opcode (UNOP_MEMVAL);
411 switch (msymbol -> type)
415 case mst_solib_trampoline:
416 write_exp_elt_type (msym_text_symbol_type);
423 write_exp_elt_type (msym_data_symbol_type);
427 write_exp_elt_type (msym_unknown_symbol_type);
430 write_exp_elt_opcode (UNOP_MEMVAL);
433 /* Recognize tokens that start with '$'. These include:
435 $regname A native register name or a "standard
438 $variable A convenience variable with a name chosen
441 $digits Value history with index <digits>, starting
442 from the first value which has index 1.
444 $$digits Value history with index <digits> relative
445 to the last value. I.E. $$0 is the last
446 value, $$1 is the one previous to that, $$2
447 is the one previous to $$1, etc.
449 $ | $0 | $$0 The last value in the value history.
451 $$ An abbreviation for the second to the last
452 value in the value history, I.E. $$1
457 write_dollar_variable (str)
460 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
461 and $$digits (equivalent to $<-digits> if you could type that). */
465 /* Double dollar means negate the number and add -1 as well.
466 Thus $$ alone means -1. */
467 if (str.length >= 2 && str.ptr[1] == '$')
474 /* Just dollars (one or two) */
478 /* Is the rest of the token digits? */
479 for (; i < str.length; i++)
480 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
484 i = atoi (str.ptr + 1 + negate);
490 /* Handle tokens that refer to machine registers:
491 $ followed by a register name. */
492 i = target_map_name_to_register( str.ptr + 1, str.length - 1 );
494 goto handle_register;
496 /* Any other names starting in $ are debugger internal variables. */
498 write_exp_elt_opcode (OP_INTERNALVAR);
499 write_exp_elt_intern (lookup_internalvar (copy_name (str) + 1));
500 write_exp_elt_opcode (OP_INTERNALVAR);
503 write_exp_elt_opcode (OP_LAST);
504 write_exp_elt_longcst ((LONGEST) i);
505 write_exp_elt_opcode (OP_LAST);
508 write_exp_elt_opcode (OP_REGISTER);
509 write_exp_elt_longcst (i);
510 write_exp_elt_opcode (OP_REGISTER);
514 /* Return a null-terminated temporary copy of the name
515 of a string token. */
521 memcpy (namecopy, token.ptr, token.length);
522 namecopy[token.length] = 0;
526 /* Reverse an expression from suffix form (in which it is constructed)
527 to prefix form (in which we can conveniently print or execute it). */
530 prefixify_expression (expr)
531 register struct expression *expr;
534 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
535 register struct expression *temp;
536 register int inpos = expr->nelts, outpos = 0;
538 temp = (struct expression *) alloca (len);
540 /* Copy the original expression into temp. */
541 memcpy (temp, expr, len);
543 prefixify_subexp (temp, expr, inpos, outpos);
546 /* Return the number of exp_elements in the subexpression of EXPR
547 whose last exp_element is at index ENDPOS - 1 in EXPR. */
550 length_of_subexp (expr, endpos)
551 register struct expression *expr;
554 register int oplen = 1;
555 register int args = 0;
559 error ("?error in length_of_subexp");
561 i = (int) expr->elts[endpos - 1].opcode;
567 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
568 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
591 case OP_F77_UNDETERMINED_ARGLIST:
593 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
621 case STRUCTOP_STRUCT:
629 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
630 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
634 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
635 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
636 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
641 args = longest_to_int (expr->elts[endpos - 2].longconst);
642 args -= longest_to_int (expr->elts[endpos - 3].longconst);
648 case TERNOP_SLICE_COUNT:
653 case MULTI_SUBSCRIPT:
655 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
658 case BINOP_ASSIGN_MODIFY:
669 args = 1 + (i < (int) BINOP_END);
674 oplen += length_of_subexp (expr, endpos - oplen);
681 /* Copy the subexpression ending just before index INEND in INEXPR
682 into OUTEXPR, starting at index OUTBEG.
683 In the process, convert it from suffix to prefix form. */
686 prefixify_subexp (inexpr, outexpr, inend, outbeg)
687 register struct expression *inexpr;
688 struct expression *outexpr;
692 register int oplen = 1;
693 register int args = 0;
696 enum exp_opcode opcode;
698 /* Compute how long the last operation is (in OPLEN),
699 and also how many preceding subexpressions serve as
700 arguments for it (in ARGS). */
702 opcode = inexpr->elts[inend - 1].opcode;
707 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
708 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
731 case OP_F77_UNDETERMINED_ARGLIST:
733 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
759 case STRUCTOP_STRUCT:
768 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
769 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
773 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
774 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
775 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
780 args = longest_to_int (inexpr->elts[inend - 2].longconst);
781 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
787 case TERNOP_SLICE_COUNT:
791 case BINOP_ASSIGN_MODIFY:
797 case MULTI_SUBSCRIPT:
799 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
808 args = 1 + ((int) opcode < (int) BINOP_END);
811 /* Copy the final operator itself, from the end of the input
812 to the beginning of the output. */
814 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
815 EXP_ELEM_TO_BYTES (oplen));
818 /* Find the lengths of the arg subexpressions. */
819 arglens = (int *) alloca (args * sizeof (int));
820 for (i = args - 1; i >= 0; i--)
822 oplen = length_of_subexp (inexpr, inend);
827 /* Now copy each subexpression, preserving the order of
828 the subexpressions, but prefixifying each one.
829 In this loop, inend starts at the beginning of
830 the expression this level is working on
831 and marches forward over the arguments.
832 outbeg does similarly in the output. */
833 for (i = 0; i < args; i++)
837 prefixify_subexp (inexpr, outexpr, inend, outbeg);
842 /* This page contains the two entry points to this file. */
844 /* Read an expression from the string *STRINGPTR points to,
845 parse it, and return a pointer to a struct expression that we malloc.
846 Use block BLOCK as the lexical context for variable names;
847 if BLOCK is zero, use the block of the selected stack frame.
848 Meanwhile, advance *STRINGPTR to point after the expression,
849 at the first nonwhite character that is not part of the expression
850 (possibly a null character).
852 If COMMA is nonzero, stop if a comma is reached. */
855 parse_exp_1 (stringptr, block, comma)
860 struct cleanup *old_chain;
865 type_stack_depth = 0;
867 comma_terminates = comma;
869 if (lexptr == 0 || *lexptr == 0)
870 error_no_arg ("expression to compute");
872 old_chain = make_cleanup (free_funcalls, 0);
875 expression_context_block = block ? block : get_selected_block ();
877 namecopy = (char *) alloca (strlen (lexptr) + 1);
880 expout = (struct expression *)
881 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
882 expout->language_defn = current_language;
883 make_cleanup (free_current_contents, &expout);
885 if (current_language->la_parser ())
886 current_language->la_error (NULL);
888 discard_cleanups (old_chain);
890 /* Record the actual number of expression elements, and then
891 reallocate the expression memory so that we free up any
894 expout->nelts = expout_ptr;
895 expout = (struct expression *)
896 xrealloc ((char *) expout,
897 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
899 /* Convert expression from postfix form as generated by yacc
900 parser, to a prefix form. */
902 DUMP_EXPRESSION (expout, gdb_stdout, "before conversion to prefix form");
903 prefixify_expression (expout);
904 DUMP_EXPRESSION (expout, gdb_stdout, "after conversion to prefix form");
910 /* Parse STRING as an expression, and complain if this fails
911 to use up all of the contents of STRING. */
914 parse_expression (string)
917 register struct expression *exp;
918 exp = parse_exp_1 (&string, 0, 0);
920 error ("Junk after end of expression.");
924 /* Stuff for maintaining a stack of types. Currently just used by C, but
925 probably useful for any language which declares its types "backwards". */
931 if (type_stack_depth == type_stack_size)
933 type_stack_size *= 2;
934 type_stack = (union type_stack_elt *)
935 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
937 type_stack[type_stack_depth++].piece = tp;
944 if (type_stack_depth == type_stack_size)
946 type_stack_size *= 2;
947 type_stack = (union type_stack_elt *)
948 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
950 type_stack[type_stack_depth++].int_val = n;
956 if (type_stack_depth)
957 return type_stack[--type_stack_depth].piece;
964 if (type_stack_depth)
965 return type_stack[--type_stack_depth].int_val;
966 /* "Can't happen". */
970 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
971 as modified by all the stuff on the stack. */
973 follow_types (follow_type)
974 struct type *follow_type;
978 struct type *range_type;
987 follow_type = lookup_pointer_type (follow_type);
990 follow_type = lookup_reference_type (follow_type);
993 array_size = pop_type_int ();
994 /* FIXME-type-allocation: need a way to free this type when we are
997 create_range_type ((struct type *) NULL,
999 array_size >= 0 ? array_size - 1 : 0);
1001 create_array_type ((struct type *) NULL,
1002 follow_type, range_type);
1004 TYPE_ARRAY_UPPER_BOUND_TYPE(follow_type)
1005 = BOUND_CANNOT_BE_DETERMINED;
1008 /* FIXME-type-allocation: need a way to free this type when we are
1010 follow_type = lookup_function_type (follow_type);
1017 _initialize_parse ()
1019 type_stack_size = 80;
1020 type_stack_depth = 0;
1021 type_stack = (union type_stack_elt *)
1022 xmalloc (type_stack_size * sizeof (*type_stack));
1024 msym_text_symbol_type =
1025 init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
1026 TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
1027 msym_data_symbol_type =
1028 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
1029 "<data variable, no debug info>", NULL);
1030 msym_unknown_symbol_type =
1031 init_type (TYPE_CODE_INT, 1, 0,
1032 "<variable (not text or data), no debug info>",