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"
41 #include "symfile.h" /* for overlay functions */
43 /* Global variables declared in parser-defs.h (and commented there). */
44 struct expression *expout;
47 struct block *expression_context_block;
48 struct block *innermost_block;
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 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
66 /* Data structure for saving values of arglist_len for function calls whose
67 arguments contain other function calls. */
75 static struct funcall *funcall_chain;
77 /* Assign machine-independent names to certain registers
78 (unless overridden by the REGISTER_NAMES table) */
81 unsigned num_std_regs = 0;
82 struct std_regs std_regs[1];
84 struct std_regs std_regs[] = {
101 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
105 /* The generic method for targets to specify how their registers are named.
106 The mapping can be derived from three sources: reg_names; std_regs; or
107 a target specific alias hook. */
110 target_map_name_to_register (str, len)
116 /* First try target specific aliases. We try these first because on some
117 systems standard names can be context dependent (eg. $pc on a
118 multiprocessor can be could be any of several PCs). */
119 #ifdef REGISTER_NAME_ALIAS_HOOK
120 i = REGISTER_NAME_ALIAS_HOOK (str, len);
125 /* Search architectural register name space. */
126 for (i = 0; i < NUM_REGS; i++)
127 if (reg_names[i] && len == strlen (reg_names[i])
128 && STREQN (str, reg_names[i], len))
133 /* Try standard aliases */
134 for (i = 0; i < num_std_regs; i++)
135 if (std_regs[i].name && len == strlen (std_regs[i].name)
136 && STREQN (str, std_regs[i].name, len))
138 return std_regs[i].regnum;
144 /* Begin counting arguments for a function call,
145 saving the data about any containing call. */
150 register struct funcall *new;
152 new = (struct funcall *) xmalloc (sizeof (struct funcall));
153 new->next = funcall_chain;
154 new->arglist_len = arglist_len;
159 /* Return the number of arguments in a function call just terminated,
160 and restore the data for the containing function call. */
165 register int val = arglist_len;
166 register struct funcall *call = funcall_chain;
167 funcall_chain = call->next;
168 arglist_len = call->arglist_len;
173 /* Free everything in the funcall chain.
174 Used when there is an error inside parsing. */
179 register struct funcall *call, *next;
181 for (call = funcall_chain; call; call = next)
188 /* This page contains the functions for adding data to the struct expression
189 being constructed. */
191 /* Add one element to the end of the expression. */
193 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
194 a register through here */
197 write_exp_elt (expelt)
198 union exp_element expelt;
200 if (expout_ptr >= expout_size)
203 expout = (struct expression *)
204 xrealloc ((char *) expout, sizeof (struct expression)
205 + EXP_ELEM_TO_BYTES (expout_size));
207 expout->elts[expout_ptr++] = expelt;
211 write_exp_elt_opcode (expelt)
212 enum exp_opcode expelt;
214 union exp_element tmp;
222 write_exp_elt_sym (expelt)
223 struct symbol *expelt;
225 union exp_element tmp;
233 write_exp_elt_block (b)
236 union exp_element tmp;
242 write_exp_elt_longcst (expelt)
245 union exp_element tmp;
247 tmp.longconst = expelt;
253 write_exp_elt_dblcst (expelt)
256 union exp_element tmp;
258 tmp.doubleconst = expelt;
264 write_exp_elt_type (expelt)
267 union exp_element tmp;
275 write_exp_elt_intern (expelt)
276 struct internalvar *expelt;
278 union exp_element tmp;
280 tmp.internalvar = expelt;
285 /* Add a string constant to the end of the expression.
287 String constants are stored by first writing an expression element
288 that contains the length of the string, then stuffing the string
289 constant itself into however many expression elements are needed
290 to hold it, and then writing another expression element that contains
291 the length of the string. I.E. an expression element at each end of
292 the string records the string length, so you can skip over the
293 expression elements containing the actual string bytes from either
294 end of the string. Note that this also allows gdb to handle
295 strings with embedded null bytes, as is required for some languages.
297 Don't be fooled by the fact that the string is null byte terminated,
298 this is strictly for the convenience of debugging gdb itself. Gdb
299 Gdb does not depend up the string being null terminated, since the
300 actual length is recorded in expression elements at each end of the
301 string. The null byte is taken into consideration when computing how
302 many expression elements are required to hold the string constant, of
307 write_exp_string (str)
310 register int len = str.length;
312 register char *strdata;
314 /* Compute the number of expression elements required to hold the string
315 (including a null byte terminator), along with one expression element
316 at each end to record the actual string length (not including the
317 null byte terminator). */
319 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
321 /* Ensure that we have enough available expression elements to store
324 if ((expout_ptr + lenelt) >= expout_size)
326 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
327 expout = (struct expression *)
328 xrealloc ((char *) expout, (sizeof (struct expression)
329 + EXP_ELEM_TO_BYTES (expout_size)));
332 /* Write the leading length expression element (which advances the current
333 expression element index), then write the string constant followed by a
334 terminating null byte, and then write the trailing length expression
337 write_exp_elt_longcst ((LONGEST) len);
338 strdata = (char *) &expout->elts[expout_ptr];
339 memcpy (strdata, str.ptr, len);
340 *(strdata + len) = '\0';
341 expout_ptr += lenelt - 2;
342 write_exp_elt_longcst ((LONGEST) len);
345 /* Add a bitstring constant to the end of the expression.
347 Bitstring constants are stored by first writing an expression element
348 that contains the length of the bitstring (in bits), then stuffing the
349 bitstring constant itself into however many expression elements are
350 needed to hold it, and then writing another expression element that
351 contains the length of the bitstring. I.E. an expression element at
352 each end of the bitstring records the bitstring length, so you can skip
353 over the expression elements containing the actual bitstring bytes from
354 either end of the bitstring. */
357 write_exp_bitstring (str)
360 register int bits = str.length; /* length in bits */
361 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
363 register char *strdata;
365 /* Compute the number of expression elements required to hold the bitstring,
366 along with one expression element at each end to record the actual
367 bitstring length in bits. */
369 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
371 /* Ensure that we have enough available expression elements to store
374 if ((expout_ptr + lenelt) >= expout_size)
376 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
377 expout = (struct expression *)
378 xrealloc ((char *) expout, (sizeof (struct expression)
379 + EXP_ELEM_TO_BYTES (expout_size)));
382 /* Write the leading length expression element (which advances the current
383 expression element index), then write the bitstring constant, and then
384 write the trailing length expression element. */
386 write_exp_elt_longcst ((LONGEST) bits);
387 strdata = (char *) &expout->elts[expout_ptr];
388 memcpy (strdata, str.ptr, len);
389 expout_ptr += lenelt - 2;
390 write_exp_elt_longcst ((LONGEST) bits);
393 /* Add the appropriate elements for a minimal symbol to the end of
394 the expression. The rationale behind passing in text_symbol_type and
395 data_symbol_type was so that Modula-2 could pass in WORD for
396 data_symbol_type. Perhaps it still is useful to have those types vary
397 based on the language, but they no longer have names like "int", so
398 the initial rationale is gone. */
400 static struct type *msym_text_symbol_type;
401 static struct type *msym_data_symbol_type;
402 static struct type *msym_unknown_symbol_type;
405 write_exp_msymbol (msymbol, text_symbol_type, data_symbol_type)
406 struct minimal_symbol *msymbol;
407 struct type *text_symbol_type;
408 struct type *data_symbol_type;
412 write_exp_elt_opcode (OP_LONG);
413 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
415 addr = SYMBOL_VALUE_ADDRESS (msymbol);
416 if (overlay_debugging)
417 addr = symbol_overlayed_address (addr, SYMBOL_BFD_SECTION (msymbol));
418 write_exp_elt_longcst ((LONGEST) addr);
420 write_exp_elt_opcode (OP_LONG);
422 write_exp_elt_opcode (UNOP_MEMVAL);
423 switch (msymbol -> type)
427 case mst_solib_trampoline:
428 write_exp_elt_type (msym_text_symbol_type);
435 write_exp_elt_type (msym_data_symbol_type);
439 write_exp_elt_type (msym_unknown_symbol_type);
442 write_exp_elt_opcode (UNOP_MEMVAL);
445 /* Recognize tokens that start with '$'. These include:
447 $regname A native register name or a "standard
450 $variable A convenience variable with a name chosen
453 $digits Value history with index <digits>, starting
454 from the first value which has index 1.
456 $$digits Value history with index <digits> relative
457 to the last value. I.E. $$0 is the last
458 value, $$1 is the one previous to that, $$2
459 is the one previous to $$1, etc.
461 $ | $0 | $$0 The last value in the value history.
463 $$ An abbreviation for the second to the last
464 value in the value history, I.E. $$1
469 write_dollar_variable (str)
472 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
473 and $$digits (equivalent to $<-digits> if you could type that). */
477 /* Double dollar means negate the number and add -1 as well.
478 Thus $$ alone means -1. */
479 if (str.length >= 2 && str.ptr[1] == '$')
486 /* Just dollars (one or two) */
490 /* Is the rest of the token digits? */
491 for (; i < str.length; i++)
492 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
496 i = atoi (str.ptr + 1 + negate);
502 /* Handle tokens that refer to machine registers:
503 $ followed by a register name. */
504 i = target_map_name_to_register( str.ptr + 1, str.length - 1 );
506 goto handle_register;
508 /* Any other names starting in $ are debugger internal variables. */
510 write_exp_elt_opcode (OP_INTERNALVAR);
511 write_exp_elt_intern (lookup_internalvar (copy_name (str) + 1));
512 write_exp_elt_opcode (OP_INTERNALVAR);
515 write_exp_elt_opcode (OP_LAST);
516 write_exp_elt_longcst ((LONGEST) i);
517 write_exp_elt_opcode (OP_LAST);
520 write_exp_elt_opcode (OP_REGISTER);
521 write_exp_elt_longcst (i);
522 write_exp_elt_opcode (OP_REGISTER);
526 /* Return a null-terminated temporary copy of the name
527 of a string token. */
533 memcpy (namecopy, token.ptr, token.length);
534 namecopy[token.length] = 0;
538 /* Reverse an expression from suffix form (in which it is constructed)
539 to prefix form (in which we can conveniently print or execute it). */
542 prefixify_expression (expr)
543 register struct expression *expr;
546 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
547 register struct expression *temp;
548 register int inpos = expr->nelts, outpos = 0;
550 temp = (struct expression *) alloca (len);
552 /* Copy the original expression into temp. */
553 memcpy (temp, expr, len);
555 prefixify_subexp (temp, expr, inpos, outpos);
558 /* Return the number of exp_elements in the subexpression of EXPR
559 whose last exp_element is at index ENDPOS - 1 in EXPR. */
562 length_of_subexp (expr, endpos)
563 register struct expression *expr;
566 register int oplen = 1;
567 register int args = 0;
571 error ("?error in length_of_subexp");
573 i = (int) expr->elts[endpos - 1].opcode;
579 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
580 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
603 case OP_F77_UNDETERMINED_ARGLIST:
605 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
633 case STRUCTOP_STRUCT:
641 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
642 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
646 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
647 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
648 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
653 args = longest_to_int (expr->elts[endpos - 2].longconst);
654 args -= longest_to_int (expr->elts[endpos - 3].longconst);
660 case TERNOP_SLICE_COUNT:
665 case MULTI_SUBSCRIPT:
667 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
670 case BINOP_ASSIGN_MODIFY:
681 args = 1 + (i < (int) BINOP_END);
686 oplen += length_of_subexp (expr, endpos - oplen);
693 /* Copy the subexpression ending just before index INEND in INEXPR
694 into OUTEXPR, starting at index OUTBEG.
695 In the process, convert it from suffix to prefix form. */
698 prefixify_subexp (inexpr, outexpr, inend, outbeg)
699 register struct expression *inexpr;
700 struct expression *outexpr;
704 register int oplen = 1;
705 register int args = 0;
708 enum exp_opcode opcode;
710 /* Compute how long the last operation is (in OPLEN),
711 and also how many preceding subexpressions serve as
712 arguments for it (in ARGS). */
714 opcode = inexpr->elts[inend - 1].opcode;
719 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
720 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
743 case OP_F77_UNDETERMINED_ARGLIST:
745 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
771 case STRUCTOP_STRUCT:
780 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
781 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
785 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
786 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
787 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
792 args = longest_to_int (inexpr->elts[inend - 2].longconst);
793 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
799 case TERNOP_SLICE_COUNT:
803 case BINOP_ASSIGN_MODIFY:
809 case MULTI_SUBSCRIPT:
811 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
820 args = 1 + ((int) opcode < (int) BINOP_END);
823 /* Copy the final operator itself, from the end of the input
824 to the beginning of the output. */
826 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
827 EXP_ELEM_TO_BYTES (oplen));
830 /* Find the lengths of the arg subexpressions. */
831 arglens = (int *) alloca (args * sizeof (int));
832 for (i = args - 1; i >= 0; i--)
834 oplen = length_of_subexp (inexpr, inend);
839 /* Now copy each subexpression, preserving the order of
840 the subexpressions, but prefixifying each one.
841 In this loop, inend starts at the beginning of
842 the expression this level is working on
843 and marches forward over the arguments.
844 outbeg does similarly in the output. */
845 for (i = 0; i < args; i++)
849 prefixify_subexp (inexpr, outexpr, inend, outbeg);
854 /* This page contains the two entry points to this file. */
856 /* Read an expression from the string *STRINGPTR points to,
857 parse it, and return a pointer to a struct expression that we malloc.
858 Use block BLOCK as the lexical context for variable names;
859 if BLOCK is zero, use the block of the selected stack frame.
860 Meanwhile, advance *STRINGPTR to point after the expression,
861 at the first nonwhite character that is not part of the expression
862 (possibly a null character).
864 If COMMA is nonzero, stop if a comma is reached. */
867 parse_exp_1 (stringptr, block, comma)
872 struct cleanup *old_chain;
877 type_stack_depth = 0;
879 comma_terminates = comma;
881 if (lexptr == 0 || *lexptr == 0)
882 error_no_arg ("expression to compute");
884 old_chain = make_cleanup (free_funcalls, 0);
887 expression_context_block = block ? block : get_selected_block ();
889 namecopy = (char *) alloca (strlen (lexptr) + 1);
892 expout = (struct expression *)
893 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
894 expout->language_defn = current_language;
895 make_cleanup (free_current_contents, &expout);
897 if (current_language->la_parser ())
898 current_language->la_error (NULL);
900 discard_cleanups (old_chain);
902 /* Record the actual number of expression elements, and then
903 reallocate the expression memory so that we free up any
906 expout->nelts = expout_ptr;
907 expout = (struct expression *)
908 xrealloc ((char *) expout,
909 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
911 /* Convert expression from postfix form as generated by yacc
912 parser, to a prefix form. */
914 DUMP_EXPRESSION (expout, gdb_stdout, "before conversion to prefix form");
915 prefixify_expression (expout);
916 DUMP_EXPRESSION (expout, gdb_stdout, "after conversion to prefix form");
922 /* Parse STRING as an expression, and complain if this fails
923 to use up all of the contents of STRING. */
926 parse_expression (string)
929 register struct expression *exp;
930 exp = parse_exp_1 (&string, 0, 0);
932 error ("Junk after end of expression.");
936 /* Stuff for maintaining a stack of types. Currently just used by C, but
937 probably useful for any language which declares its types "backwards". */
943 if (type_stack_depth == type_stack_size)
945 type_stack_size *= 2;
946 type_stack = (union type_stack_elt *)
947 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
949 type_stack[type_stack_depth++].piece = tp;
956 if (type_stack_depth == type_stack_size)
958 type_stack_size *= 2;
959 type_stack = (union type_stack_elt *)
960 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
962 type_stack[type_stack_depth++].int_val = n;
968 if (type_stack_depth)
969 return type_stack[--type_stack_depth].piece;
976 if (type_stack_depth)
977 return type_stack[--type_stack_depth].int_val;
978 /* "Can't happen". */
982 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
983 as modified by all the stuff on the stack. */
985 follow_types (follow_type)
986 struct type *follow_type;
990 struct type *range_type;
999 follow_type = lookup_pointer_type (follow_type);
1002 follow_type = lookup_reference_type (follow_type);
1005 array_size = pop_type_int ();
1006 /* FIXME-type-allocation: need a way to free this type when we are
1009 create_range_type ((struct type *) NULL,
1010 builtin_type_int, 0,
1011 array_size >= 0 ? array_size - 1 : 0);
1013 create_array_type ((struct type *) NULL,
1014 follow_type, range_type);
1016 TYPE_ARRAY_UPPER_BOUND_TYPE(follow_type)
1017 = BOUND_CANNOT_BE_DETERMINED;
1020 /* FIXME-type-allocation: need a way to free this type when we are
1022 follow_type = lookup_function_type (follow_type);
1029 _initialize_parse ()
1031 type_stack_size = 80;
1032 type_stack_depth = 0;
1033 type_stack = (union type_stack_elt *)
1034 xmalloc (type_stack_size * sizeof (*type_stack));
1036 msym_text_symbol_type =
1037 init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
1038 TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
1039 msym_data_symbol_type =
1040 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
1041 "<data variable, no debug info>", NULL);
1042 msym_unknown_symbol_type =
1043 init_type (TYPE_CODE_INT, 1, 0,
1044 "<variable (not text or data), no debug info>",