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]);
105 /* Begin counting arguments for a function call,
106 saving the data about any containing call. */
111 register struct funcall
*new;
113 new = (struct funcall
*) xmalloc (sizeof (struct funcall
));
114 new->next
= funcall_chain
;
115 new->arglist_len
= arglist_len
;
120 /* Return the number of arguments in a function call just terminated,
121 and restore the data for the containing function call. */
126 register int val
= arglist_len
;
127 register struct funcall
*call
= funcall_chain
;
128 funcall_chain
= call
->next
;
129 arglist_len
= call
->arglist_len
;
134 /* Free everything in the funcall chain.
135 Used when there is an error inside parsing. */
140 register struct funcall
*call
, *next
;
142 for (call
= funcall_chain
; call
; call
= next
)
149 /* This page contains the functions for adding data to the struct expression
150 being constructed. */
152 /* Add one element to the end of the expression. */
154 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
155 a register through here */
158 write_exp_elt (expelt
)
159 union exp_element expelt
;
161 if (expout_ptr
>= expout_size
)
164 expout
= (struct expression
*)
165 xrealloc ((char *) expout
, sizeof (struct expression
)
166 + EXP_ELEM_TO_BYTES (expout_size
));
168 expout
->elts
[expout_ptr
++] = expelt
;
172 write_exp_elt_opcode (expelt
)
173 enum exp_opcode expelt
;
175 union exp_element tmp
;
183 write_exp_elt_sym (expelt
)
184 struct symbol
*expelt
;
186 union exp_element tmp
;
194 write_exp_elt_block (b
)
197 union exp_element tmp
;
203 write_exp_elt_longcst (expelt
)
206 union exp_element tmp
;
208 tmp
.longconst
= expelt
;
214 write_exp_elt_dblcst (expelt
)
217 union exp_element tmp
;
219 tmp
.doubleconst
= expelt
;
225 write_exp_elt_type (expelt
)
228 union exp_element tmp
;
236 write_exp_elt_intern (expelt
)
237 struct internalvar
*expelt
;
239 union exp_element tmp
;
241 tmp
.internalvar
= expelt
;
246 /* Add a string constant to the end of the expression.
248 String constants are stored by first writing an expression element
249 that contains the length of the string, then stuffing the string
250 constant itself into however many expression elements are needed
251 to hold it, and then writing another expression element that contains
252 the length of the string. I.E. an expression element at each end of
253 the string records the string length, so you can skip over the
254 expression elements containing the actual string bytes from either
255 end of the string. Note that this also allows gdb to handle
256 strings with embedded null bytes, as is required for some languages.
258 Don't be fooled by the fact that the string is null byte terminated,
259 this is strictly for the convenience of debugging gdb itself. Gdb
260 Gdb does not depend up the string being null terminated, since the
261 actual length is recorded in expression elements at each end of the
262 string. The null byte is taken into consideration when computing how
263 many expression elements are required to hold the string constant, of
268 write_exp_string (str
)
271 register int len
= str
.length
;
273 register char *strdata
;
275 /* Compute the number of expression elements required to hold the string
276 (including a null byte terminator), along with one expression element
277 at each end to record the actual string length (not including the
278 null byte terminator). */
280 lenelt
= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
282 /* Ensure that we have enough available expression elements to store
285 if ((expout_ptr
+ lenelt
) >= expout_size
)
287 expout_size
= max (expout_size
* 2, expout_ptr
+ lenelt
+ 10);
288 expout
= (struct expression
*)
289 xrealloc ((char *) expout
, (sizeof (struct expression
)
290 + EXP_ELEM_TO_BYTES (expout_size
)));
293 /* Write the leading length expression element (which advances the current
294 expression element index), then write the string constant followed by a
295 terminating null byte, and then write the trailing length expression
298 write_exp_elt_longcst ((LONGEST
) len
);
299 strdata
= (char *) &expout
->elts
[expout_ptr
];
300 memcpy (strdata
, str
.ptr
, len
);
301 *(strdata
+ len
) = '\0';
302 expout_ptr
+= lenelt
- 2;
303 write_exp_elt_longcst ((LONGEST
) len
);
306 /* Add a bitstring constant to the end of the expression.
308 Bitstring constants are stored by first writing an expression element
309 that contains the length of the bitstring (in bits), then stuffing the
310 bitstring constant itself into however many expression elements are
311 needed to hold it, and then writing another expression element that
312 contains the length of the bitstring. I.E. an expression element at
313 each end of the bitstring records the bitstring length, so you can skip
314 over the expression elements containing the actual bitstring bytes from
315 either end of the bitstring. */
318 write_exp_bitstring (str
)
321 register int bits
= str
.length
; /* length in bits */
322 register int len
= (bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
324 register char *strdata
;
326 /* Compute the number of expression elements required to hold the bitstring,
327 along with one expression element at each end to record the actual
328 bitstring length in bits. */
330 lenelt
= 2 + BYTES_TO_EXP_ELEM (len
);
332 /* Ensure that we have enough available expression elements to store
335 if ((expout_ptr
+ lenelt
) >= expout_size
)
337 expout_size
= max (expout_size
* 2, expout_ptr
+ lenelt
+ 10);
338 expout
= (struct expression
*)
339 xrealloc ((char *) expout
, (sizeof (struct expression
)
340 + EXP_ELEM_TO_BYTES (expout_size
)));
343 /* Write the leading length expression element (which advances the current
344 expression element index), then write the bitstring constant, and then
345 write the trailing length expression element. */
347 write_exp_elt_longcst ((LONGEST
) bits
);
348 strdata
= (char *) &expout
->elts
[expout_ptr
];
349 memcpy (strdata
, str
.ptr
, len
);
350 expout_ptr
+= lenelt
- 2;
351 write_exp_elt_longcst ((LONGEST
) bits
);
354 /* Add the appropriate elements for a minimal symbol to the end of
355 the expression. The rationale behind passing in text_symbol_type and
356 data_symbol_type was so that Modula-2 could pass in WORD for
357 data_symbol_type. Perhaps it still is useful to have those types vary
358 based on the language, but they no longer have names like "int", so
359 the initial rationale is gone. */
361 static struct type
*msym_text_symbol_type
;
362 static struct type
*msym_data_symbol_type
;
363 static struct type
*msym_unknown_symbol_type
;
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 (lookup_pointer_type (builtin_type_void
));
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 (msym_text_symbol_type
);
389 write_exp_elt_type (msym_data_symbol_type
);
393 write_exp_elt_type (msym_unknown_symbol_type
);
396 write_exp_elt_opcode (UNOP_MEMVAL
);
399 /* Recognize tokens that start with '$'. These include:
401 $regname A native register name or a "standard
404 $variable A convenience variable with a name chosen
407 $digits Value history with index <digits>, starting
408 from the first value which has index 1.
410 $$digits Value history with index <digits> relative
411 to the last value. I.E. $$0 is the last
412 value, $$1 is the one previous to that, $$2
413 is the one previous to $$1, etc.
415 $ | $0 | $$0 The last value in the value history.
417 $$ An abbreviation for the second to the last
418 value in the value history, I.E. $$1
423 write_dollar_variable (str
)
426 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
427 and $$digits (equivalent to $<-digits> if you could type that). */
431 /* Double dollar means negate the number and add -1 as well.
432 Thus $$ alone means -1. */
433 if (str
.length
>= 2 && str
.ptr
[1] == '$')
440 /* Just dollars (one or two) */
444 /* Is the rest of the token digits? */
445 for (; i
< str
.length
; i
++)
446 if (!(str
.ptr
[i
] >= '0' && str
.ptr
[i
] <= '9'))
450 i
= atoi (str
.ptr
+ 1 + negate
);
456 /* Handle tokens that refer to machine registers:
457 $ followed by a register name. */
458 for (i
= 0; i
< NUM_REGS
; i
++)
459 if (reg_names
[i
] && str
.length
- 1 == strlen (reg_names
[i
])
460 && STREQN (str
.ptr
+ 1, reg_names
[i
], str
.length
- 1))
462 goto handle_register
;
464 for (i
= 0; i
< num_std_regs
; i
++)
465 if (std_regs
[i
].name
&& str
.length
- 1 == strlen (std_regs
[i
].name
)
466 && STREQN (str
.ptr
+ 1, std_regs
[i
].name
, str
.length
- 1))
468 i
= std_regs
[i
].regnum
;
469 goto handle_register
;
472 /* Any other names starting in $ are debugger internal variables. */
474 write_exp_elt_opcode (OP_INTERNALVAR
);
475 write_exp_elt_intern (lookup_internalvar (copy_name (str
) + 1));
476 write_exp_elt_opcode (OP_INTERNALVAR
);
479 write_exp_elt_opcode (OP_LAST
);
480 write_exp_elt_longcst ((LONGEST
) i
);
481 write_exp_elt_opcode (OP_LAST
);
484 write_exp_elt_opcode (OP_REGISTER
);
485 write_exp_elt_longcst (i
);
486 write_exp_elt_opcode (OP_REGISTER
);
490 /* Return a null-terminated temporary copy of the name
491 of a string token. */
497 memcpy (namecopy
, token
.ptr
, token
.length
);
498 namecopy
[token
.length
] = 0;
502 /* Reverse an expression from suffix form (in which it is constructed)
503 to prefix form (in which we can conveniently print or execute it). */
506 prefixify_expression (expr
)
507 register struct expression
*expr
;
510 sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expr
->nelts
);
511 register struct expression
*temp
;
512 register int inpos
= expr
->nelts
, outpos
= 0;
514 temp
= (struct expression
*) alloca (len
);
516 /* Copy the original expression into temp. */
517 memcpy (temp
, expr
, len
);
519 prefixify_subexp (temp
, expr
, inpos
, outpos
);
522 /* Return the number of exp_elements in the subexpression of EXPR
523 whose last exp_element is at index ENDPOS - 1 in EXPR. */
526 length_of_subexp (expr
, endpos
)
527 register struct expression
*expr
;
530 register int oplen
= 1;
531 register int args
= 0;
535 error ("?error in length_of_subexp");
537 i
= (int) expr
->elts
[endpos
- 1].opcode
;
543 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
544 oplen
= 5 + BYTES_TO_EXP_ELEM (oplen
+ 1);
567 case OP_F77_UNDETERMINED_ARGLIST
:
569 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
597 case STRUCTOP_STRUCT
:
599 /* start-sanitize-gm */
602 #endif /* GENERAL_MAGIC */
603 /* end-sanitize-gm */
610 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
611 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
615 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
616 oplen
= (oplen
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
617 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
);
622 args
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
623 args
-= longest_to_int (expr
->elts
[endpos
- 3].longconst
);
629 case TERNOP_SLICE_COUNT
:
634 case MULTI_SUBSCRIPT
:
636 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
639 case BINOP_ASSIGN_MODIFY
:
650 args
= 1 + (i
< (int) BINOP_END
);
655 oplen
+= length_of_subexp (expr
, endpos
- oplen
);
662 /* Copy the subexpression ending just before index INEND in INEXPR
663 into OUTEXPR, starting at index OUTBEG.
664 In the process, convert it from suffix to prefix form. */
667 prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
)
668 register struct expression
*inexpr
;
669 struct expression
*outexpr
;
673 register int oplen
= 1;
674 register int args
= 0;
677 enum exp_opcode opcode
;
679 /* Compute how long the last operation is (in OPLEN),
680 and also how many preceding subexpressions serve as
681 arguments for it (in ARGS). */
683 opcode
= inexpr
->elts
[inend
- 1].opcode
;
688 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
689 oplen
= 5 + BYTES_TO_EXP_ELEM (oplen
+ 1);
712 case OP_F77_UNDETERMINED_ARGLIST
:
714 args
= 1 + longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
740 case STRUCTOP_STRUCT
:
749 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
750 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
754 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
755 oplen
= (oplen
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
756 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
);
761 args
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
762 args
-= longest_to_int (inexpr
->elts
[inend
- 3].longconst
);
768 case TERNOP_SLICE_COUNT
:
772 case BINOP_ASSIGN_MODIFY
:
778 case MULTI_SUBSCRIPT
:
780 args
= 1 + longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
789 args
= 1 + ((int) opcode
< (int) BINOP_END
);
792 /* Copy the final operator itself, from the end of the input
793 to the beginning of the output. */
795 memcpy (&outexpr
->elts
[outbeg
], &inexpr
->elts
[inend
],
796 EXP_ELEM_TO_BYTES (oplen
));
799 /* Find the lengths of the arg subexpressions. */
800 arglens
= (int *) alloca (args
* sizeof (int));
801 for (i
= args
- 1; i
>= 0; i
--)
803 oplen
= length_of_subexp (inexpr
, inend
);
808 /* Now copy each subexpression, preserving the order of
809 the subexpressions, but prefixifying each one.
810 In this loop, inend starts at the beginning of
811 the expression this level is working on
812 and marches forward over the arguments.
813 outbeg does similarly in the output. */
814 for (i
= 0; i
< args
; i
++)
818 prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
);
823 /* This page contains the two entry points to this file. */
825 /* Read an expression from the string *STRINGPTR points to,
826 parse it, and return a pointer to a struct expression that we malloc.
827 Use block BLOCK as the lexical context for variable names;
828 if BLOCK is zero, use the block of the selected stack frame.
829 Meanwhile, advance *STRINGPTR to point after the expression,
830 at the first nonwhite character that is not part of the expression
831 (possibly a null character).
833 If COMMA is nonzero, stop if a comma is reached. */
836 parse_exp_1 (stringptr
, block
, comma
)
841 struct cleanup
*old_chain
;
846 type_stack_depth
= 0;
848 comma_terminates
= comma
;
850 if (lexptr
== 0 || *lexptr
== 0)
851 error_no_arg ("expression to compute");
853 old_chain
= make_cleanup (free_funcalls
, 0);
856 expression_context_block
= block
? block
: get_selected_block ();
858 namecopy
= (char *) alloca (strlen (lexptr
) + 1);
861 expout
= (struct expression
*)
862 xmalloc (sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expout_size
));
863 expout
->language_defn
= current_language
;
864 make_cleanup (free_current_contents
, &expout
);
866 if (current_language
->la_parser ())
867 current_language
->la_error (NULL
);
869 discard_cleanups (old_chain
);
871 /* Record the actual number of expression elements, and then
872 reallocate the expression memory so that we free up any
875 expout
->nelts
= expout_ptr
;
876 expout
= (struct expression
*)
877 xrealloc ((char *) expout
,
878 sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expout_ptr
));;
880 /* Convert expression from postfix form as generated by yacc
881 parser, to a prefix form. */
883 DUMP_EXPRESSION (expout
, gdb_stdout
, "before conversion to prefix form");
884 prefixify_expression (expout
);
885 DUMP_EXPRESSION (expout
, gdb_stdout
, "after conversion to prefix form");
891 /* Parse STRING as an expression, and complain if this fails
892 to use up all of the contents of STRING. */
895 parse_expression (string
)
898 register struct expression
*exp
;
899 exp
= parse_exp_1 (&string
, 0, 0);
901 error ("Junk after end of expression.");
905 /* Stuff for maintaining a stack of types. Currently just used by C, but
906 probably useful for any language which declares its types "backwards". */
912 if (type_stack_depth
== type_stack_size
)
914 type_stack_size
*= 2;
915 type_stack
= (union type_stack_elt
*)
916 xrealloc ((char *) type_stack
, type_stack_size
* sizeof (*type_stack
));
918 type_stack
[type_stack_depth
++].piece
= tp
;
925 if (type_stack_depth
== type_stack_size
)
927 type_stack_size
*= 2;
928 type_stack
= (union type_stack_elt
*)
929 xrealloc ((char *) type_stack
, type_stack_size
* sizeof (*type_stack
));
931 type_stack
[type_stack_depth
++].int_val
= n
;
937 if (type_stack_depth
)
938 return type_stack
[--type_stack_depth
].piece
;
945 if (type_stack_depth
)
946 return type_stack
[--type_stack_depth
].int_val
;
947 /* "Can't happen". */
951 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
952 as modified by all the stuff on the stack. */
954 follow_types (follow_type
)
955 struct type
*follow_type
;
959 struct type
*range_type
;
968 follow_type
= lookup_pointer_type (follow_type
);
971 follow_type
= lookup_reference_type (follow_type
);
974 array_size
= pop_type_int ();
975 /* FIXME-type-allocation: need a way to free this type when we are
978 create_range_type ((struct type
*) NULL
,
980 array_size
>= 0 ? array_size
- 1 : 0);
982 create_array_type ((struct type
*) NULL
,
983 follow_type
, range_type
);
985 TYPE_ARRAY_UPPER_BOUND_TYPE(follow_type
)
986 = BOUND_CANNOT_BE_DETERMINED
;
989 /* FIXME-type-allocation: need a way to free this type when we are
991 follow_type
= lookup_function_type (follow_type
);
1000 type_stack_size
= 80;
1001 type_stack_depth
= 0;
1002 type_stack
= (union type_stack_elt
*)
1003 xmalloc (type_stack_size
* sizeof (*type_stack
));
1005 msym_text_symbol_type
=
1006 init_type (TYPE_CODE_FUNC
, 1, 0, "<text variable, no debug info>", NULL
);
1007 TYPE_TARGET_TYPE (msym_text_symbol_type
) = builtin_type_int
;
1008 msym_data_symbol_type
=
1009 init_type (TYPE_CODE_INT
, TARGET_INT_BIT
/ HOST_CHAR_BIT
, 0,
1010 "<data variable, no debug info>", NULL
);
1011 msym_unknown_symbol_type
=
1012 init_type (TYPE_CODE_INT
, 1, 0,
1013 "<variable (not text or data), no debug info>",