ceece4d85b394e4e064bc013ea5a0c4e866dd2b0
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"
41 /* Assign machine-independent names to certain registers
42 (unless overridden by the REGISTER_NAMES table) */
44 struct std_regs std_regs
[] = {
59 unsigned num_std_regs
= (sizeof std_regs
/ sizeof std_regs
[0]);
62 /* Begin counting arguments for a function call,
63 saving the data about any containing call. */
68 register struct funcall
*new = (struct funcall
*) xmalloc (sizeof (struct funcall
));
70 new->next
= funcall_chain
;
71 new->arglist_len
= arglist_len
;
76 /* Return the number of arguments in a function call just terminated,
77 and restore the data for the containing function call. */
82 register int val
= arglist_len
;
83 register struct funcall
*call
= funcall_chain
;
84 funcall_chain
= call
->next
;
85 arglist_len
= call
->arglist_len
;
90 /* Free everything in the funcall chain.
91 Used when there is an error inside parsing. */
96 register struct funcall
*call
, *next
;
98 for (call
= funcall_chain
; call
; call
= next
)
105 /* This page contains the functions for adding data to the struct expression
106 being constructed. */
108 /* Add one element to the end of the expression. */
110 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
111 a register through here */
114 write_exp_elt (expelt
)
115 union exp_element expelt
;
117 if (expout_ptr
>= expout_size
)
120 expout
= (struct expression
*) xrealloc (expout
,
121 sizeof (struct expression
)
122 + expout_size
* sizeof (union exp_element
));
124 expout
->elts
[expout_ptr
++] = expelt
;
128 write_exp_elt_opcode (expelt
)
129 enum exp_opcode expelt
;
131 union exp_element tmp
;
139 write_exp_elt_sym (expelt
)
140 struct symbol
*expelt
;
142 union exp_element tmp
;
150 write_exp_elt_longcst (expelt
)
153 union exp_element tmp
;
155 tmp
.longconst
= expelt
;
161 write_exp_elt_dblcst (expelt
)
164 union exp_element tmp
;
166 tmp
.doubleconst
= expelt
;
172 write_exp_elt_type (expelt
)
175 union exp_element tmp
;
183 write_exp_elt_intern (expelt
)
184 struct internalvar
*expelt
;
186 union exp_element tmp
;
188 tmp
.internalvar
= expelt
;
193 /* Add a string constant to the end of the expression.
194 Follow it by its length in bytes, as a separate exp_element. */
197 write_exp_string (str
)
200 register int len
= str
.length
;
202 = (len
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
204 expout_ptr
+= lenelt
;
206 if (expout_ptr
>= expout_size
)
208 expout_size
= max (expout_size
* 2, expout_ptr
+ 10);
209 expout
= (struct expression
*)
210 xrealloc (expout
, (sizeof (struct expression
)
211 + (expout_size
* sizeof (union exp_element
))));
213 bcopy (str
.ptr
, (char *) &expout
->elts
[expout_ptr
- lenelt
], len
);
214 ((char *) &expout
->elts
[expout_ptr
- lenelt
])[len
] = 0;
215 write_exp_elt_longcst ((LONGEST
) len
);
218 /* Return a null-terminated temporary copy of the name
219 of a string token. */
225 bcopy (token
.ptr
, namecopy
, token
.length
);
226 namecopy
[token
.length
] = 0;
230 /* Reverse an expression from suffix form (in which it is constructed)
231 to prefix form (in which we can conveniently print or execute it). */
233 static void prefixify_subexp ();
236 prefixify_expression (expr
)
237 register struct expression
*expr
;
239 register int len
= sizeof (struct expression
) +
240 expr
->nelts
* sizeof (union exp_element
);
241 register struct expression
*temp
;
242 register int inpos
= expr
->nelts
, outpos
= 0;
244 temp
= (struct expression
*) alloca (len
);
246 /* Copy the original expression into temp. */
247 bcopy (expr
, temp
, len
);
249 prefixify_subexp (temp
, expr
, inpos
, outpos
);
252 /* Return the number of exp_elements in the subexpression of EXPR
253 whose last exp_element is at index ENDPOS - 1 in EXPR. */
256 length_of_subexp (expr
, endpos
)
257 register struct expression
*expr
;
260 register int oplen
= 1;
261 register int args
= 0;
265 error ("?error in length_of_subexp");
267 i
= (int) expr
->elts
[endpos
- 1].opcode
;
273 oplen
= 4 + ((expr
->elts
[endpos
- 2].longconst
274 + sizeof (union exp_element
))
275 / sizeof (union exp_element
));
294 args
= 1 + expr
->elts
[endpos
- 2].longconst
;
322 case STRUCTOP_STRUCT
:
327 oplen
= 3 + ((expr
->elts
[endpos
- 2].longconst
328 + sizeof (union exp_element
))
329 / sizeof (union exp_element
));
337 case BINOP_MULTI_SUBSCRIPT
:
339 args
= 1 + expr
->elts
[endpos
- 2].longconst
;
342 case BINOP_ASSIGN_MODIFY
:
353 args
= 1 + (i
< (int) BINOP_END
);
358 oplen
+= length_of_subexp (expr
, endpos
- oplen
);
365 /* Copy the subexpression ending just before index INEND in INEXPR
366 into OUTEXPR, starting at index OUTBEG.
367 In the process, convert it from suffix to prefix form. */
370 prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
)
371 register struct expression
*inexpr
;
372 struct expression
*outexpr
;
376 register int oplen
= 1;
377 register int args
= 0;
380 enum exp_opcode opcode
;
382 /* Compute how long the last operation is (in OPLEN),
383 and also how many preceding subexpressions serve as
384 arguments for it (in ARGS). */
386 opcode
= inexpr
->elts
[inend
- 1].opcode
;
391 oplen
= 4 + ((inexpr
->elts
[inend
- 2].longconst
392 + sizeof (union exp_element
))
393 / sizeof (union exp_element
));
412 args
= 1 + inexpr
->elts
[inend
- 2].longconst
;
439 case STRUCTOP_STRUCT
:
444 oplen
= 3 + ((inexpr
->elts
[inend
- 2].longconst
445 + sizeof (union exp_element
))
446 / sizeof (union exp_element
));
454 case BINOP_ASSIGN_MODIFY
:
460 case BINOP_MULTI_SUBSCRIPT
:
462 args
= 1 + inexpr
->elts
[inend
- 2].longconst
;
471 args
= 1 + ((int) opcode
< (int) BINOP_END
);
474 /* Copy the final operator itself, from the end of the input
475 to the beginning of the output. */
477 bcopy (&inexpr
->elts
[inend
], &outexpr
->elts
[outbeg
],
478 oplen
* sizeof (union exp_element
));
481 /* Find the lengths of the arg subexpressions. */
482 arglens
= (int *) alloca (args
* sizeof (int));
483 for (i
= args
- 1; i
>= 0; i
--)
485 oplen
= length_of_subexp (inexpr
, inend
);
490 /* Now copy each subexpression, preserving the order of
491 the subexpressions, but prefixifying each one.
492 In this loop, inend starts at the beginning of
493 the expression this level is working on
494 and marches forward over the arguments.
495 outbeg does similarly in the output. */
496 for (i
= 0; i
< args
; i
++)
500 prefixify_subexp (inexpr
, outexpr
, inend
, outbeg
);
505 /* This page contains the two entry points to this file. */
507 /* Read an expression from the string *STRINGPTR points to,
508 parse it, and return a pointer to a struct expression that we malloc.
509 Use block BLOCK as the lexical context for variable names;
510 if BLOCK is zero, use the block of the selected stack frame.
511 Meanwhile, advance *STRINGPTR to point after the expression,
512 at the first nonwhite character that is not part of the expression
513 (possibly a null character).
515 If COMMA is nonzero, stop if a comma is reached. */
518 parse_exp_1 (stringptr
, block
, comma
)
523 struct cleanup
*old_chain
;
528 type_stack_depth
= 0;
530 comma_terminates
= comma
;
532 if (lexptr
== 0 || *lexptr
== 0)
533 error_no_arg ("expression to compute");
535 old_chain
= make_cleanup (free_funcalls
, 0);
538 expression_context_block
= block
? block
: get_selected_block ();
540 namecopy
= (char *) alloca (strlen (lexptr
) + 1);
543 expout
= (struct expression
*)
544 xmalloc (sizeof (struct expression
)
545 + expout_size
* sizeof (union exp_element
));
546 expout
->language_defn
= current_language
;
547 make_cleanup (free_current_contents
, &expout
);
549 if (current_language
->la_parser ())
550 current_language
->la_error (NULL
);
552 discard_cleanups (old_chain
);
553 expout
->nelts
= expout_ptr
;
554 expout
= (struct expression
*)
556 sizeof (struct expression
)
557 + expout_ptr
* sizeof (union exp_element
));
558 prefixify_expression (expout
);
563 /* Parse STRING as an expression, and complain if this fails
564 to use up all of the contents of STRING. */
567 parse_expression (string
)
570 register struct expression
*exp
;
571 exp
= parse_exp_1 (&string
, 0, 0);
573 error ("Junk after end of expression.");
581 if (type_stack_depth
== type_stack_size
)
583 type_stack_size
*= 2;
584 type_stack
= (union type_stack_elt
*)
585 xrealloc (type_stack
, type_stack_size
* sizeof (*type_stack
));
587 type_stack
[type_stack_depth
++].piece
= tp
;
594 if (type_stack_depth
== type_stack_size
)
596 type_stack_size
*= 2;
597 type_stack
= (union type_stack_elt
*)
598 xrealloc (type_stack
, type_stack_size
* sizeof (*type_stack
));
600 type_stack
[type_stack_depth
++].int_val
= n
;
606 if (type_stack_depth
)
607 return type_stack
[--type_stack_depth
].piece
;
614 if (type_stack_depth
)
615 return type_stack
[--type_stack_depth
].int_val
;
616 /* "Can't happen". */
623 type_stack_size
= 80;
624 type_stack_depth
= 0;
625 type_stack
= (union type_stack_elt
*)
626 xmalloc (type_stack_size
* sizeof (*type_stack
));