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) */
53 struct std_regs std_regs
[] = {
68 unsigned num_std_regs
= (sizeof std_regs
/ sizeof std_regs
[0]);
71 /* Begin counting arguments for a function call,
72 saving the data about any containing call. */
77 register struct funcall
*new = (struct funcall
*) xmalloc (sizeof (struct funcall
));
79 new->next
= funcall_chain
;
80 new->arglist_len
= arglist_len
;
85 /* Return the number of arguments in a function call just terminated,
86 and restore the data for the containing function call. */
91 register int val
= arglist_len
;
92 register struct funcall
*call
= funcall_chain
;
93 funcall_chain
= call
->next
;
94 arglist_len
= call
->arglist_len
;
99 /* Free everything in the funcall chain.
100 Used when there is an error inside parsing. */
105 register struct funcall
*call
, *next
;
107 for (call
= funcall_chain
; call
; call
= next
)
114 /* This page contains the functions for adding data to the struct expression
115 being constructed. */
117 /* Add one element to the end of the expression. */
119 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
120 a register through here */
123 write_exp_elt (expelt
)
124 union exp_element expelt
;
126 if (expout_ptr
>= expout_size
)
129 expout
= (struct expression
*)
130 xrealloc ((char *) expout
, sizeof (struct expression
)
131 + EXP_ELEM_TO_BYTES (expout_size
));
133 expout
->elts
[expout_ptr
++] = expelt
;
137 write_exp_elt_opcode (expelt
)
138 enum exp_opcode expelt
;
140 union exp_element tmp
;
148 write_exp_elt_sym (expelt
)
149 struct symbol
*expelt
;
151 union exp_element tmp
;
159 write_exp_elt_longcst (expelt
)
162 union exp_element tmp
;
164 tmp
.longconst
= expelt
;
170 write_exp_elt_dblcst (expelt
)
173 union exp_element tmp
;
175 tmp
.doubleconst
= expelt
;
181 write_exp_elt_type (expelt
)
184 union exp_element tmp
;
192 write_exp_elt_intern (expelt
)
193 struct internalvar
*expelt
;
195 union exp_element tmp
;
197 tmp
.internalvar
= expelt
;
202 /* Add a string constant to the end of the expression.
204 String constants are stored by first writing an expression element
205 that contains the length of the string, then stuffing the string
206 constant itself into however many expression elements are needed
207 to hold it, and then writing another expression element that contains
208 the length of the string. I.E. an expression element at each end of
209 the string records the string length, so you can skip over the
210 expression elements containing the actual string bytes from either
211 end of the string. Note that this also allows gdb to handle
212 strings with embedded null bytes, as is required for some languages.
214 Don't be fooled by the fact that the string is null byte terminated,
215 this is strictly for the convenience of debugging gdb itself. Gdb
216 Gdb does not depend up the string being null terminated, since the
217 actual length is recorded in expression elements at each end of the
218 string. The null byte is taken into consideration when computing how
219 many expression elements are required to hold the string constant, of
224 write_exp_string (str
)
227 register int len
= str
.length
;
229 register char *strdata
;
231 /* Compute the number of expression elements required to hold the string
232 (including a null byte terminator), along with one expression element
233 at each end to record the actual string length (not including the
234 null byte terminator). */
236 lenelt
= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
238 /* Ensure that we have enough available expression elements to store
241 if ((expout_ptr
+ lenelt
) >= expout_size
)
243 expout_size
= max (expout_size
* 2, expout_ptr
+ lenelt
+ 10);
244 expout
= (struct expression
*)
245 xrealloc ((char *) expout
, (sizeof (struct expression
)
246 + EXP_ELEM_TO_BYTES (expout_size
)));
249 /* Write the leading length expression element (which advances the current
250 expression element index), then write the string constant followed by a
251 terminating null byte, and then write the trailing length expression
254 write_exp_elt_longcst ((LONGEST
) len
);
255 strdata
= (char *) &expout
->elts
[expout_ptr
];
256 memcpy (strdata
, str
.ptr
, len
);
257 *(strdata
+ len
) = '\0';
258 expout_ptr
+= lenelt
- 2;
259 write_exp_elt_longcst ((LONGEST
) len
);
262 /* Add a bitstring constant to the end of the expression.
264 Bitstring constants are stored by first writing an expression element
265 that contains the length of the bitstring (in bits), then stuffing the
266 bitstring constant itself into however many expression elements are
267 needed to hold it, and then writing another expression element that
268 contains the length of the bitstring. I.E. an expression element at
269 each end of the bitstring records the bitstring length, so you can skip
270 over the expression elements containing the actual bitstring bytes from
271 either end of the bitstring. */
274 write_exp_bitstring (str
)
277 register int bits
= str
.length
; /* length in bits */
278 register int len
= (bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
280 register char *strdata
;
282 /* Compute the number of expression elements required to hold the bitstring,
283 along with one expression element at each end to record the actual
284 bitstring length in bits. */
286 lenelt
= 2 + BYTES_TO_EXP_ELEM (len
);
288 /* Ensure that we have enough available expression elements to store
291 if ((expout_ptr
+ lenelt
) >= expout_size
)
293 expout_size
= max (expout_size
* 2, expout_ptr
+ lenelt
+ 10);
294 expout
= (struct expression
*)
295 xrealloc ((char *) expout
, (sizeof (struct expression
)
296 + EXP_ELEM_TO_BYTES (expout_size
)));
299 /* Write the leading length expression element (which advances the current
300 expression element index), then write the bitstring constant, and then
301 write the trailing length expression element. */
303 write_exp_elt_longcst ((LONGEST
) bits
);
304 strdata
= (char *) &expout
->elts
[expout_ptr
];
305 memcpy (strdata
, str
.ptr
, len
);
306 expout_ptr
+= lenelt
- 2;
307 write_exp_elt_longcst ((LONGEST
) bits
);
310 /* Return a null-terminated temporary copy of the name
311 of a string token. */
317 memcpy (namecopy
, token
.ptr
, token
.length
);
318 namecopy
[token
.length
] = 0;
322 /* Reverse an expression from suffix form (in which it is constructed)
323 to prefix form (in which we can conveniently print or execute it). */
326 prefixify_expression (expr
)
327 register struct expression
*expr
;
330 sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expr
->nelts
);
331 register struct expression
*temp
;
332 register int inpos
= expr
->nelts
, outpos
= 0;
334 temp
= (struct expression
*) alloca (len
);
336 /* Copy the original expression into temp. */
337 memcpy (temp
, expr
, len
);
339 prefixify_subexp (temp
, expr
, inpos
, outpos
);
342 /* Return the number of exp_elements in the subexpression of EXPR
343 whose last exp_element is at index ENDPOS - 1 in EXPR. */
346 length_of_subexp (expr
, endpos
)
347 register struct expression
*expr
;
350 register int oplen
= 1;
351 register int args
= 0;
355 error ("?error in length_of_subexp");
357 i
= (int) expr
->elts
[endpos
- 1].opcode
;
363 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
364 oplen
= 5 + BYTES_TO_EXP_ELEM (oplen
+ 1);
383 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
410 case STRUCTOP_STRUCT
:
416 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
417 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
421 oplen
= longest_to_int (expr
->elts
[endpos
- 2].longconst
);
422 oplen
= (oplen
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
423 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
);
431 case MULTI_SUBSCRIPT
:
433 args
= 1 + longest_to_int (expr
->elts
[endpos
- 2].longconst
);
436 case BINOP_ASSIGN_MODIFY
:
447 args
= 1 + (i
< (int) BINOP_END
);
452 oplen
+= length_of_subexp (expr
, endpos
- oplen
);
459 /* Copy the subexpression ending just before index INEND in INEXPR
460 into OUTEXPR, starting at index OUTBEG.
461 In the process, convert it from suffix to prefix form. */
464 prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
)
465 register struct expression
*inexpr
;
466 struct expression
*outexpr
;
470 register int oplen
= 1;
471 register int args
= 0;
474 enum exp_opcode opcode
;
476 /* Compute how long the last operation is (in OPLEN),
477 and also how many preceding subexpressions serve as
478 arguments for it (in ARGS). */
480 opcode
= inexpr
->elts
[inend
- 1].opcode
;
485 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
486 oplen
= 5 + BYTES_TO_EXP_ELEM (oplen
+ 1);
505 args
= 1 + longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
531 case STRUCTOP_STRUCT
:
537 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
538 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
+ 1);
542 oplen
= longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
543 oplen
= (oplen
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
544 oplen
= 4 + BYTES_TO_EXP_ELEM (oplen
);
551 case BINOP_ASSIGN_MODIFY
:
557 case MULTI_SUBSCRIPT
:
559 args
= 1 + longest_to_int (inexpr
->elts
[inend
- 2].longconst
);
568 args
= 1 + ((int) opcode
< (int) BINOP_END
);
571 /* Copy the final operator itself, from the end of the input
572 to the beginning of the output. */
574 memcpy (&outexpr
->elts
[outbeg
], &inexpr
->elts
[inend
],
575 EXP_ELEM_TO_BYTES (oplen
));
578 /* Find the lengths of the arg subexpressions. */
579 arglens
= (int *) alloca (args
* sizeof (int));
580 for (i
= args
- 1; i
>= 0; i
--)
582 oplen
= length_of_subexp (inexpr
, inend
);
587 /* Now copy each subexpression, preserving the order of
588 the subexpressions, but prefixifying each one.
589 In this loop, inend starts at the beginning of
590 the expression this level is working on
591 and marches forward over the arguments.
592 outbeg does similarly in the output. */
593 for (i
= 0; i
< args
; i
++)
597 prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
);
602 /* This page contains the two entry points to this file. */
604 /* Read an expression from the string *STRINGPTR points to,
605 parse it, and return a pointer to a struct expression that we malloc.
606 Use block BLOCK as the lexical context for variable names;
607 if BLOCK is zero, use the block of the selected stack frame.
608 Meanwhile, advance *STRINGPTR to point after the expression,
609 at the first nonwhite character that is not part of the expression
610 (possibly a null character).
612 If COMMA is nonzero, stop if a comma is reached. */
615 parse_exp_1 (stringptr
, block
, comma
)
620 struct cleanup
*old_chain
;
625 type_stack_depth
= 0;
627 comma_terminates
= comma
;
629 if (lexptr
== 0 || *lexptr
== 0)
630 error_no_arg ("expression to compute");
632 old_chain
= make_cleanup (free_funcalls
, 0);
635 expression_context_block
= block
? block
: get_selected_block ();
637 namecopy
= (char *) alloca (strlen (lexptr
) + 1);
640 expout
= (struct expression
*)
641 xmalloc (sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expout_size
));
642 expout
->language_defn
= current_language
;
643 make_cleanup (free_current_contents
, &expout
);
645 if (current_language
->la_parser ())
646 current_language
->la_error (NULL
);
648 discard_cleanups (old_chain
);
650 /* Record the actual number of expression elements, and then
651 reallocate the expression memory so that we free up any
654 expout
->nelts
= expout_ptr
;
655 expout
= (struct expression
*)
656 xrealloc ((char *) expout
,
657 sizeof (struct expression
) + EXP_ELEM_TO_BYTES (expout_ptr
));;
659 /* Convert expression from postfix form as generated by yacc
660 parser, to a prefix form. */
662 DUMP_EXPRESSION (expout
, stdout
, "before conversion to prefix form");
663 prefixify_expression (expout
);
664 DUMP_EXPRESSION (expout
, stdout
, "after conversion to prefix form");
670 /* Parse STRING as an expression, and complain if this fails
671 to use up all of the contents of STRING. */
674 parse_expression (string
)
677 register struct expression
*exp
;
678 exp
= parse_exp_1 (&string
, 0, 0);
680 error ("Junk after end of expression.");
688 if (type_stack_depth
== type_stack_size
)
690 type_stack_size
*= 2;
691 type_stack
= (union type_stack_elt
*)
692 xrealloc ((char *) type_stack
, type_stack_size
* sizeof (*type_stack
));
694 type_stack
[type_stack_depth
++].piece
= tp
;
701 if (type_stack_depth
== type_stack_size
)
703 type_stack_size
*= 2;
704 type_stack
= (union type_stack_elt
*)
705 xrealloc ((char *) type_stack
, type_stack_size
* sizeof (*type_stack
));
707 type_stack
[type_stack_depth
++].int_val
= n
;
713 if (type_stack_depth
)
714 return type_stack
[--type_stack_depth
].piece
;
721 if (type_stack_depth
)
722 return type_stack
[--type_stack_depth
].int_val
;
723 /* "Can't happen". */
730 type_stack_size
= 80;
731 type_stack_depth
= 0;
732 type_stack
= (union type_stack_elt
*)
733 xmalloc (type_stack_size
* sizeof (*type_stack
));
This page took 0.054235 seconds and 4 git commands to generate.