9d9697593c64dea23877a24610ef807321f94f84
1 /* Parse expressions for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 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., 675 Mass Ave, Cambridge, MA 02139, 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. */
35 #include "expression.h"
39 #include "parser-defs.h"
42 prefixify_expression
PARAMS ((struct expression
*));
45 length_of_subexp
PARAMS ((struct expression
*, int));
48 prefixify_subexp
PARAMS ((struct expression
*, struct expression
*, int, int));
50 /* Assign machine-independent names to certain registers
51 (unless overridden by the REGISTER_NAMES table) */
54 unsigned num_std_regs
= 0;
55 struct std_regs std_regs
[1];
57 struct std_regs std_regs
[] = {
74 unsigned num_std_regs
= (sizeof std_regs
/ sizeof std_regs
[0]);
79 /* Begin counting arguments for a function call,
80 saving the data about any containing call. */
85 register struct funcall
*new = (struct funcall
*) xmalloc (sizeof (struct funcall
));
87 new->next
= funcall_chain
;
88 new->arglist_len
= arglist_len
;
93 /* Return the number of arguments in a function call just terminated,
94 and restore the data for the containing function call. */
99 register int val
= arglist_len
;
100 register struct funcall
*call
= funcall_chain
;
101 funcall_chain
= call
->next
;
102 arglist_len
= call
->arglist_len
;
107 /* Free everything in the funcall chain.
108 Used when there is an error inside parsing. */
113 register struct funcall
*call
, *next
;
115 for (call
= funcall_chain
; call
; call
= next
)
122 /* This page contains the functions for adding data to the struct expression
123 being constructed. */
125 /* Add one element to the end of the expression. */
127 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
128 a register through here */
131 write_exp_elt (expelt
)
132 union exp_element expelt
;
134 if (expout_ptr
>= expout_size
)
137 expout
= (struct expression
*)
138 xrealloc ((char *) expout
, sizeof (struct expression
)
139 + EXP_ELEM_TO_BYTES (expout_size
));
141 expout
->elts
[expout_ptr
++] = expelt
;
145 write_exp_elt_opcode (expelt
)
146 enum exp_opcode expelt
;
148 union exp_element tmp
;
156 write_exp_elt_sym (expelt
)
157 struct symbol
*expelt
;
159 union exp_element tmp
;
167 write_exp_elt_longcst (expelt
)
170 union exp_element tmp
;
172 tmp
.longconst
= expelt
;
178 write_exp_elt_dblcst (expelt
)
181 union exp_element tmp
;
183 tmp
.doubleconst
= expelt
;
189 write_exp_elt_type (expelt
)
192 union exp_element tmp
;
200 write_exp_elt_intern (expelt
)
201 struct internalvar
*expelt
;
203 union exp_element tmp
;
205 tmp
.internalvar
= expelt
;
210 /* Add a string constant to the end of the expression.
212 String constants are stored by first writing an expression element
213 that contains the length of the string, then stuffing the string
214 constant itself into however many expression elements are needed
215 to hold it, and then writing another expression element that contains
216 the length of the string. I.E. an expression element at each end of
217 the string records the string length, so you can skip over the
218 expression elements containing the actual string bytes from either
219 end of the string. Note that this also allows gdb to handle
220 strings with embedded null bytes, as is required for some languages.
222 Don't be fooled by the fact that the string is null byte terminated,
223 this is strictly for the convenience of debugging gdb itself. Gdb
224 Gdb does not depend up the string being null terminated, since the
225 actual length is recorded in expression elements at each end of the
226 string. The null byte is taken into consideration when computing how
227 many expression elements are required to hold the string constant, of
232 write_exp_string (str
)
235 register int len
= str
.length
;
237 register char *strdata
;
239 /* Compute the number of expression elements required to hold the string
240 (including a null byte terminator), along with one expression element
241 at each end to record the actual string length (not including the
242 null byte terminator). */
244 lenelt
= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
246 /* Ensure that we have enough available expression elements to store
249 if ((expout_ptr
+ lenelt
) >= expout_size
)
251 expout_size
= max (expout_size
* 2, expout_ptr
+ lenelt
+ 10);
252 expout
= (struct expression
*)
253 xrealloc ((char *) expout
, (sizeof (struct expression
)
254 + EXP_ELEM_TO_BYTES (expout_size
)));
257 /* Write the leading length expression element (which advances the current
258 expression element index), then write the string constant followed by a
259 terminating null byte, and then write the trailing length expression
262 write_exp_elt_longcst ((LONGEST
) len
);
263 strdata
= (char *) &expout
->elts
[expout_ptr
];
264 memcpy (strdata
, str
.ptr
, len
);
265 *(strdata
+ len
) = '\0';
266 expout_ptr
+= lenelt
- 2;
267 write_exp_elt_longcst ((LONGEST
) len
);
270 /* Add a bitstring constant to the end of the expression.
272 Bitstring constants are stored by first writing an expression element
273 that contains the length of the bitstring (in bits), then stuffing the
274 bitstring constant itself into however many expression elements are
275 needed to hold it, and then writing another expression element that
276 contains the length of the bitstring. I.E. an expression element at
277 each end of the bitstring records the bitstring length, so you can skip
278 over the expression elements containing the actual bitstring bytes from
279 either end of the bitstring. */
282 write_exp_bitstring (str
)
285 register int bits
= str
.length
; /* length in bits */
286 register int len
= (bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
288 register char *strdata
;
290 /* Compute the number of expression elements required to hold the bitstring,
291 along with one expression element at each end to record the actual
292 bitstring length in bits. */
294 lenelt
= 2 + BYTES_TO_EXP_ELEM (len
);
296 /* Ensure that we have enough available expression elements to store
299 if ((expout_ptr
+ lenelt
) >= expout_size
)
301 expout_size
= max (expout_size
* 2, expout_ptr
+ lenelt
+ 10);
302 expout
= (struct expression
*)
303 xrealloc ((char *) expout
, (sizeof (struct expression
)
304 + EXP_ELEM_TO_BYTES (expout_size
)));
307 /* Write the leading length expression element (which advances the current
308 expression element index), then write the bitstring constant, and then
309 write the trailing length expression element. */
311 write_exp_elt_longcst ((LONGEST
) bits
);
312 strdata
= (char *) &expout
->elts
[expout_ptr
];
313 memcpy (strdata
, str
.ptr
, len
);
314 expout_ptr
+= lenelt
- 2;
315 write_exp_elt_longcst ((LONGEST
) bits
);
318 /* Return a null-terminated temporary copy of the name
319 of a string token. */
325 memcpy (namecopy
, token
.ptr
, token
.length
);
326 namecopy
[token
.length
] = 0;
330 /* Reverse an expression from suffix form (in which it is constructed)
331 to prefix form (in which we can conveniently print or execute it). */
334 prefixify_expression (expr
)
335 register struct expression
*expr
;
338 sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expr
->nelts
);
339 register struct expression
*temp
;
340 register int inpos
= expr
->nelts
, outpos
= 0;
342 temp
= (struct expression
*) alloca (len
);
344 /* Copy the original expression into temp. */
345 memcpy (temp
, expr
, len
);
347 prefixify_subexp (temp
, expr
, inpos
, outpos
);
350 /* Return the number of exp_elements in the subexpression of EXPR
351 whose last exp_element is at index ENDPOS - 1 in EXPR. */
354 length_of_subexp (expr
, endpos
)
355 register struct expression
*expr
;
358 register int oplen
= 1;
359 register int args
= 0;
363 error ("?error in length_of_subexp");
365 i
= (int) expr
->elts
[endpos
- 1].opcode
;
371 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
372 oplen
= 5 + BYTES_TO_EXP_ELEM (oplen
+ 1);
391 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
418 case STRUCTOP_STRUCT
:
424 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
425 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
429 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
430 oplen
= (oplen
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
431 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
);
436 args
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
437 args
-= longest_to_int (expr
->elts
[endpos
- 3].longconst
);
446 case MULTI_SUBSCRIPT
:
448 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
451 case BINOP_ASSIGN_MODIFY
:
462 args
= 1 + (i
< (int) BINOP_END
);
467 oplen
+= length_of_subexp (expr
, endpos
- oplen
);
474 /* Copy the subexpression ending just before index INEND in INEXPR
475 into OUTEXPR, starting at index OUTBEG.
476 In the process, convert it from suffix to prefix form. */
479 prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
)
480 register struct expression
*inexpr
;
481 struct expression
*outexpr
;
485 register int oplen
= 1;
486 register int args
= 0;
489 enum exp_opcode opcode
;
491 /* Compute how long the last operation is (in OPLEN),
492 and also how many preceding subexpressions serve as
493 arguments for it (in ARGS). */
495 opcode
= inexpr
->elts
[inend
- 1].opcode
;
500 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
501 oplen
= 5 + BYTES_TO_EXP_ELEM (oplen
+ 1);
520 args
= 1 + longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
546 case STRUCTOP_STRUCT
:
552 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
553 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
557 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
558 oplen
= (oplen
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
559 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
);
564 args
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
565 args
-= longest_to_int (inexpr
->elts
[inend
- 3].longconst
);
573 case BINOP_ASSIGN_MODIFY
:
579 case MULTI_SUBSCRIPT
:
581 args
= 1 + longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
590 args
= 1 + ((int) opcode
< (int) BINOP_END
);
593 /* Copy the final operator itself, from the end of the input
594 to the beginning of the output. */
596 memcpy (&outexpr
->elts
[outbeg
], &inexpr
->elts
[inend
],
597 EXP_ELEM_TO_BYTES (oplen
));
600 /* Find the lengths of the arg subexpressions. */
601 arglens
= (int *) alloca (args
* sizeof (int));
602 for (i
= args
- 1; i
>= 0; i
--)
604 oplen
= length_of_subexp (inexpr
, inend
);
609 /* Now copy each subexpression, preserving the order of
610 the subexpressions, but prefixifying each one.
611 In this loop, inend starts at the beginning of
612 the expression this level is working on
613 and marches forward over the arguments.
614 outbeg does similarly in the output. */
615 for (i
= 0; i
< args
; i
++)
619 prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
);
624 /* This page contains the two entry points to this file. */
626 /* Read an expression from the string *STRINGPTR points to,
627 parse it, and return a pointer to a struct expression that we malloc.
628 Use block BLOCK as the lexical context for variable names;
629 if BLOCK is zero, use the block of the selected stack frame.
630 Meanwhile, advance *STRINGPTR to point after the expression,
631 at the first nonwhite character that is not part of the expression
632 (possibly a null character).
634 If COMMA is nonzero, stop if a comma is reached. */
637 parse_exp_1 (stringptr
, block
, comma
)
642 struct cleanup
*old_chain
;
647 type_stack_depth
= 0;
649 comma_terminates
= comma
;
651 if (lexptr
== 0 || *lexptr
== 0)
652 error_no_arg ("expression to compute");
654 old_chain
= make_cleanup (free_funcalls
, 0);
657 expression_context_block
= block
? block
: get_selected_block ();
659 namecopy
= (char *) alloca (strlen (lexptr
) + 1);
662 expout
= (struct expression
*)
663 xmalloc (sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expout_size
));
664 expout
->language_defn
= current_language
;
665 make_cleanup (free_current_contents
, &expout
);
667 if (current_language
->la_parser ())
668 current_language
->la_error (NULL
);
670 discard_cleanups (old_chain
);
672 /* Record the actual number of expression elements, and then
673 reallocate the expression memory so that we free up any
676 expout
->nelts
= expout_ptr
;
677 expout
= (struct expression
*)
678 xrealloc ((char *) expout
,
679 sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expout_ptr
));;
681 /* Convert expression from postfix form as generated by yacc
682 parser, to a prefix form. */
684 DUMP_EXPRESSION (expout
, stdout
, "before conversion to prefix form");
685 prefixify_expression (expout
);
686 DUMP_EXPRESSION (expout
, stdout
, "after conversion to prefix form");
692 /* Parse STRING as an expression, and complain if this fails
693 to use up all of the contents of STRING. */
696 parse_expression (string
)
699 register struct expression
*exp
;
700 exp
= parse_exp_1 (&string
, 0, 0);
702 error ("Junk after end of expression.");
710 if (type_stack_depth
== type_stack_size
)
712 type_stack_size
*= 2;
713 type_stack
= (union type_stack_elt
*)
714 xrealloc ((char *) type_stack
, type_stack_size
* sizeof (*type_stack
));
716 type_stack
[type_stack_depth
++].piece
= tp
;
723 if (type_stack_depth
== type_stack_size
)
725 type_stack_size
*= 2;
726 type_stack
= (union type_stack_elt
*)
727 xrealloc ((char *) type_stack
, type_stack_size
* sizeof (*type_stack
));
729 type_stack
[type_stack_depth
++].int_val
= n
;
735 if (type_stack_depth
)
736 return type_stack
[--type_stack_depth
].piece
;
743 if (type_stack_depth
)
744 return type_stack
[--type_stack_depth
].int_val
;
745 /* "Can't happen". */
752 type_stack_size
= 80;
753 type_stack_depth
= 0;
754 type_stack
= (union type_stack_elt
*)
755 xmalloc (type_stack_size
* sizeof (*type_stack
));