]>
Commit | Line | Data |
---|---|---|
3d6b6a90 JG |
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. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. */ | |
21 | ||
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. */ | |
30 | ||
3d6b6a90 | 31 | #include "defs.h" |
3d6b6a90 | 32 | #include "symtab.h" |
1ab3bf1b | 33 | #include "gdbtypes.h" |
3d6b6a90 JG |
34 | #include "frame.h" |
35 | #include "expression.h" | |
36 | #include "value.h" | |
37 | #include "command.h" | |
38 | #include "language.h" | |
39 | #include "parser-defs.h" | |
40 | ||
1ab3bf1b JG |
41 | static void |
42 | prefixify_expression PARAMS ((struct expression *)); | |
43 | ||
44 | static int | |
45 | length_of_subexp PARAMS ((struct expression *, int)); | |
46 | ||
47 | static void | |
48 | prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int)); | |
49 | ||
3d6b6a90 JG |
50 | /* Assign machine-independent names to certain registers |
51 | (unless overridden by the REGISTER_NAMES table) */ | |
52 | ||
53 | struct std_regs std_regs[] = { | |
54 | #ifdef PC_REGNUM | |
55 | { "pc", PC_REGNUM }, | |
56 | #endif | |
57 | #ifdef FP_REGNUM | |
58 | { "fp", FP_REGNUM }, | |
59 | #endif | |
60 | #ifdef SP_REGNUM | |
61 | { "sp", SP_REGNUM }, | |
62 | #endif | |
63 | #ifdef PS_REGNUM | |
64 | { "ps", PS_REGNUM }, | |
65 | #endif | |
66 | }; | |
67 | ||
68 | unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]); | |
69 | ||
70 | ||
71 | /* Begin counting arguments for a function call, | |
72 | saving the data about any containing call. */ | |
73 | ||
74 | void | |
75 | start_arglist () | |
76 | { | |
77 | register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall)); | |
78 | ||
79 | new->next = funcall_chain; | |
80 | new->arglist_len = arglist_len; | |
81 | arglist_len = 0; | |
82 | funcall_chain = new; | |
83 | } | |
84 | ||
85 | /* Return the number of arguments in a function call just terminated, | |
86 | and restore the data for the containing function call. */ | |
87 | ||
88 | int | |
89 | end_arglist () | |
90 | { | |
91 | register int val = arglist_len; | |
92 | register struct funcall *call = funcall_chain; | |
93 | funcall_chain = call->next; | |
94 | arglist_len = call->arglist_len; | |
be772100 | 95 | free ((PTR)call); |
3d6b6a90 JG |
96 | return val; |
97 | } | |
98 | ||
99 | /* Free everything in the funcall chain. | |
100 | Used when there is an error inside parsing. */ | |
101 | ||
102 | void | |
103 | free_funcalls () | |
104 | { | |
105 | register struct funcall *call, *next; | |
106 | ||
107 | for (call = funcall_chain; call; call = next) | |
108 | { | |
109 | next = call->next; | |
be772100 | 110 | free ((PTR)call); |
3d6b6a90 JG |
111 | } |
112 | } | |
113 | \f | |
114 | /* This page contains the functions for adding data to the struct expression | |
115 | being constructed. */ | |
116 | ||
117 | /* Add one element to the end of the expression. */ | |
118 | ||
119 | /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into | |
120 | a register through here */ | |
121 | ||
122 | void | |
123 | write_exp_elt (expelt) | |
124 | union exp_element expelt; | |
125 | { | |
126 | if (expout_ptr >= expout_size) | |
127 | { | |
128 | expout_size *= 2; | |
1ab3bf1b | 129 | expout = (struct expression *) xrealloc ((char *) expout, |
3d6b6a90 JG |
130 | sizeof (struct expression) |
131 | + expout_size * sizeof (union exp_element)); | |
132 | } | |
133 | expout->elts[expout_ptr++] = expelt; | |
134 | } | |
135 | ||
136 | void | |
137 | write_exp_elt_opcode (expelt) | |
138 | enum exp_opcode expelt; | |
139 | { | |
140 | union exp_element tmp; | |
141 | ||
142 | tmp.opcode = expelt; | |
143 | ||
144 | write_exp_elt (tmp); | |
145 | } | |
146 | ||
147 | void | |
148 | write_exp_elt_sym (expelt) | |
149 | struct symbol *expelt; | |
150 | { | |
151 | union exp_element tmp; | |
152 | ||
153 | tmp.symbol = expelt; | |
154 | ||
155 | write_exp_elt (tmp); | |
156 | } | |
157 | ||
158 | void | |
159 | write_exp_elt_longcst (expelt) | |
160 | LONGEST expelt; | |
161 | { | |
162 | union exp_element tmp; | |
163 | ||
164 | tmp.longconst = expelt; | |
165 | ||
166 | write_exp_elt (tmp); | |
167 | } | |
168 | ||
169 | void | |
170 | write_exp_elt_dblcst (expelt) | |
171 | double expelt; | |
172 | { | |
173 | union exp_element tmp; | |
174 | ||
175 | tmp.doubleconst = expelt; | |
176 | ||
177 | write_exp_elt (tmp); | |
178 | } | |
179 | ||
180 | void | |
181 | write_exp_elt_type (expelt) | |
182 | struct type *expelt; | |
183 | { | |
184 | union exp_element tmp; | |
185 | ||
186 | tmp.type = expelt; | |
187 | ||
188 | write_exp_elt (tmp); | |
189 | } | |
190 | ||
191 | void | |
192 | write_exp_elt_intern (expelt) | |
193 | struct internalvar *expelt; | |
194 | { | |
195 | union exp_element tmp; | |
196 | ||
197 | tmp.internalvar = expelt; | |
198 | ||
199 | write_exp_elt (tmp); | |
200 | } | |
201 | ||
202 | /* Add a string constant to the end of the expression. | |
203 | Follow it by its length in bytes, as a separate exp_element. */ | |
204 | ||
205 | void | |
206 | write_exp_string (str) | |
207 | struct stoken str; | |
208 | { | |
209 | register int len = str.length; | |
210 | register int lenelt | |
211 | = (len + sizeof (union exp_element)) / sizeof (union exp_element); | |
212 | ||
213 | expout_ptr += lenelt; | |
214 | ||
215 | if (expout_ptr >= expout_size) | |
216 | { | |
217 | expout_size = max (expout_size * 2, expout_ptr + 10); | |
218 | expout = (struct expression *) | |
1ab3bf1b | 219 | xrealloc ((char *) expout, (sizeof (struct expression) |
3d6b6a90 JG |
220 | + (expout_size * sizeof (union exp_element)))); |
221 | } | |
222 | bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len); | |
223 | ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0; | |
224 | write_exp_elt_longcst ((LONGEST) len); | |
225 | } | |
226 | \f | |
227 | /* Return a null-terminated temporary copy of the name | |
228 | of a string token. */ | |
229 | ||
230 | char * | |
231 | copy_name (token) | |
232 | struct stoken token; | |
233 | { | |
234 | bcopy (token.ptr, namecopy, token.length); | |
235 | namecopy[token.length] = 0; | |
236 | return namecopy; | |
237 | } | |
238 | \f | |
239 | /* Reverse an expression from suffix form (in which it is constructed) | |
240 | to prefix form (in which we can conveniently print or execute it). */ | |
241 | ||
1ab3bf1b | 242 | static void |
3d6b6a90 JG |
243 | prefixify_expression (expr) |
244 | register struct expression *expr; | |
245 | { | |
246 | register int len = sizeof (struct expression) + | |
247 | expr->nelts * sizeof (union exp_element); | |
248 | register struct expression *temp; | |
249 | register int inpos = expr->nelts, outpos = 0; | |
250 | ||
251 | temp = (struct expression *) alloca (len); | |
252 | ||
253 | /* Copy the original expression into temp. */ | |
254 | bcopy (expr, temp, len); | |
255 | ||
256 | prefixify_subexp (temp, expr, inpos, outpos); | |
257 | } | |
258 | ||
259 | /* Return the number of exp_elements in the subexpression of EXPR | |
260 | whose last exp_element is at index ENDPOS - 1 in EXPR. */ | |
261 | ||
1ab3bf1b | 262 | static int |
3d6b6a90 JG |
263 | length_of_subexp (expr, endpos) |
264 | register struct expression *expr; | |
265 | register int endpos; | |
266 | { | |
267 | register int oplen = 1; | |
268 | register int args = 0; | |
269 | register int i; | |
270 | ||
271 | if (endpos < 0) | |
272 | error ("?error in length_of_subexp"); | |
273 | ||
274 | i = (int) expr->elts[endpos - 1].opcode; | |
275 | ||
276 | switch (i) | |
277 | { | |
278 | /* C++ */ | |
279 | case OP_SCOPE: | |
280 | oplen = 4 + ((expr->elts[endpos - 2].longconst | |
281 | + sizeof (union exp_element)) | |
282 | / sizeof (union exp_element)); | |
283 | break; | |
284 | ||
285 | case OP_LONG: | |
286 | case OP_DOUBLE: | |
287 | oplen = 4; | |
288 | break; | |
289 | ||
290 | case OP_TYPE: | |
291 | case OP_BOOL: | |
292 | case OP_VAR_VALUE: | |
293 | case OP_LAST: | |
294 | case OP_REGISTER: | |
295 | case OP_INTERNALVAR: | |
296 | oplen = 3; | |
297 | break; | |
298 | ||
299 | case OP_FUNCALL: | |
300 | oplen = 3; | |
301 | args = 1 + expr->elts[endpos - 2].longconst; | |
302 | break; | |
303 | ||
304 | case UNOP_MAX: | |
305 | case UNOP_MIN: | |
306 | oplen = 3; | |
307 | args = 0; | |
308 | break; | |
309 | ||
310 | case BINOP_VAL: | |
311 | case UNOP_CAST: | |
312 | case UNOP_MEMVAL: | |
313 | oplen = 3; | |
314 | args = 1; | |
315 | break; | |
316 | ||
317 | case UNOP_ABS: | |
318 | case UNOP_CAP: | |
319 | case UNOP_CHR: | |
320 | case UNOP_FLOAT: | |
321 | case UNOP_HIGH: | |
322 | case UNOP_ODD: | |
323 | case UNOP_ORD: | |
324 | case UNOP_TRUNC: | |
325 | oplen = 1; | |
326 | args = 1; | |
327 | break; | |
328 | ||
329 | case STRUCTOP_STRUCT: | |
330 | case STRUCTOP_PTR: | |
331 | args = 1; | |
332 | case OP_M2_STRING: | |
333 | case OP_STRING: | |
334 | oplen = 3 + ((expr->elts[endpos - 2].longconst | |
335 | + sizeof (union exp_element)) | |
336 | / sizeof (union exp_element)); | |
337 | break; | |
338 | ||
339 | case TERNOP_COND: | |
340 | args = 3; | |
341 | break; | |
342 | ||
343 | /* Modula-2 */ | |
344 | case BINOP_MULTI_SUBSCRIPT: | |
345 | oplen=3; | |
346 | args = 1 + expr->elts[endpos- 2].longconst; | |
347 | break; | |
348 | ||
349 | case BINOP_ASSIGN_MODIFY: | |
350 | oplen = 3; | |
351 | args = 2; | |
352 | break; | |
353 | ||
354 | /* C++ */ | |
355 | case OP_THIS: | |
356 | oplen = 2; | |
357 | break; | |
358 | ||
359 | default: | |
360 | args = 1 + (i < (int) BINOP_END); | |
361 | } | |
362 | ||
363 | while (args > 0) | |
364 | { | |
365 | oplen += length_of_subexp (expr, endpos - oplen); | |
366 | args--; | |
367 | } | |
368 | ||
369 | return oplen; | |
370 | } | |
371 | ||
372 | /* Copy the subexpression ending just before index INEND in INEXPR | |
373 | into OUTEXPR, starting at index OUTBEG. | |
374 | In the process, convert it from suffix to prefix form. */ | |
375 | ||
376 | static void | |
377 | prefixify_subexp (inexpr, outexpr, inend, outbeg) | |
378 | register struct expression *inexpr; | |
379 | struct expression *outexpr; | |
380 | register int inend; | |
381 | int outbeg; | |
382 | { | |
383 | register int oplen = 1; | |
384 | register int args = 0; | |
385 | register int i; | |
386 | int *arglens; | |
387 | enum exp_opcode opcode; | |
388 | ||
389 | /* Compute how long the last operation is (in OPLEN), | |
390 | and also how many preceding subexpressions serve as | |
391 | arguments for it (in ARGS). */ | |
392 | ||
393 | opcode = inexpr->elts[inend - 1].opcode; | |
394 | switch (opcode) | |
395 | { | |
396 | /* C++ */ | |
397 | case OP_SCOPE: | |
398 | oplen = 4 + ((inexpr->elts[inend - 2].longconst | |
399 | + sizeof (union exp_element)) | |
400 | / sizeof (union exp_element)); | |
401 | break; | |
402 | ||
403 | case OP_LONG: | |
404 | case OP_DOUBLE: | |
405 | oplen = 4; | |
406 | break; | |
407 | ||
408 | case OP_TYPE: | |
409 | case OP_BOOL: | |
410 | case OP_VAR_VALUE: | |
411 | case OP_LAST: | |
412 | case OP_REGISTER: | |
413 | case OP_INTERNALVAR: | |
414 | oplen = 3; | |
415 | break; | |
416 | ||
417 | case OP_FUNCALL: | |
418 | oplen = 3; | |
419 | args = 1 + inexpr->elts[inend - 2].longconst; | |
420 | break; | |
421 | ||
422 | case UNOP_MIN: | |
423 | case UNOP_MAX: | |
424 | oplen = 3; | |
425 | args = 0; | |
426 | break; | |
427 | ||
428 | case UNOP_CAST: | |
429 | case UNOP_MEMVAL: | |
430 | oplen = 3; | |
431 | args = 1; | |
432 | break; | |
433 | ||
434 | case UNOP_ABS: | |
435 | case UNOP_CAP: | |
436 | case UNOP_CHR: | |
437 | case UNOP_FLOAT: | |
438 | case UNOP_HIGH: | |
439 | case UNOP_ODD: | |
440 | case UNOP_ORD: | |
441 | case UNOP_TRUNC: | |
442 | oplen=1; | |
443 | args=1; | |
444 | break; | |
445 | ||
446 | case STRUCTOP_STRUCT: | |
447 | case STRUCTOP_PTR: | |
448 | args = 1; | |
449 | case OP_M2_STRING: | |
450 | case OP_STRING: | |
451 | oplen = 3 + ((inexpr->elts[inend - 2].longconst | |
452 | + sizeof (union exp_element)) | |
453 | / sizeof (union exp_element)); | |
454 | ||
455 | break; | |
456 | ||
457 | case TERNOP_COND: | |
458 | args = 3; | |
459 | break; | |
460 | ||
461 | case BINOP_ASSIGN_MODIFY: | |
462 | oplen = 3; | |
463 | args = 2; | |
464 | break; | |
465 | ||
466 | /* Modula-2 */ | |
467 | case BINOP_MULTI_SUBSCRIPT: | |
468 | oplen=3; | |
469 | args = 1 + inexpr->elts[inend - 2].longconst; | |
470 | break; | |
471 | ||
472 | /* C++ */ | |
473 | case OP_THIS: | |
474 | oplen = 2; | |
475 | break; | |
476 | ||
477 | default: | |
478 | args = 1 + ((int) opcode < (int) BINOP_END); | |
479 | } | |
480 | ||
481 | /* Copy the final operator itself, from the end of the input | |
482 | to the beginning of the output. */ | |
483 | inend -= oplen; | |
484 | bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg], | |
485 | oplen * sizeof (union exp_element)); | |
486 | outbeg += oplen; | |
487 | ||
488 | /* Find the lengths of the arg subexpressions. */ | |
489 | arglens = (int *) alloca (args * sizeof (int)); | |
490 | for (i = args - 1; i >= 0; i--) | |
491 | { | |
492 | oplen = length_of_subexp (inexpr, inend); | |
493 | arglens[i] = oplen; | |
494 | inend -= oplen; | |
495 | } | |
496 | ||
497 | /* Now copy each subexpression, preserving the order of | |
498 | the subexpressions, but prefixifying each one. | |
499 | In this loop, inend starts at the beginning of | |
500 | the expression this level is working on | |
501 | and marches forward over the arguments. | |
502 | outbeg does similarly in the output. */ | |
503 | for (i = 0; i < args; i++) | |
504 | { | |
505 | oplen = arglens[i]; | |
506 | inend += oplen; | |
507 | prefixify_subexp (inexpr, outexpr, inend, outbeg); | |
508 | outbeg += oplen; | |
509 | } | |
510 | } | |
511 | \f | |
512 | /* This page contains the two entry points to this file. */ | |
513 | ||
514 | /* Read an expression from the string *STRINGPTR points to, | |
515 | parse it, and return a pointer to a struct expression that we malloc. | |
516 | Use block BLOCK as the lexical context for variable names; | |
517 | if BLOCK is zero, use the block of the selected stack frame. | |
518 | Meanwhile, advance *STRINGPTR to point after the expression, | |
519 | at the first nonwhite character that is not part of the expression | |
520 | (possibly a null character). | |
521 | ||
522 | If COMMA is nonzero, stop if a comma is reached. */ | |
523 | ||
524 | struct expression * | |
525 | parse_exp_1 (stringptr, block, comma) | |
526 | char **stringptr; | |
527 | struct block *block; | |
528 | int comma; | |
529 | { | |
530 | struct cleanup *old_chain; | |
531 | ||
532 | lexptr = *stringptr; | |
533 | ||
534 | paren_depth = 0; | |
535 | type_stack_depth = 0; | |
536 | ||
537 | comma_terminates = comma; | |
538 | ||
539 | if (lexptr == 0 || *lexptr == 0) | |
540 | error_no_arg ("expression to compute"); | |
541 | ||
542 | old_chain = make_cleanup (free_funcalls, 0); | |
543 | funcall_chain = 0; | |
544 | ||
545 | expression_context_block = block ? block : get_selected_block (); | |
546 | ||
547 | namecopy = (char *) alloca (strlen (lexptr) + 1); | |
548 | expout_size = 10; | |
549 | expout_ptr = 0; | |
550 | expout = (struct expression *) | |
551 | xmalloc (sizeof (struct expression) | |
552 | + expout_size * sizeof (union exp_element)); | |
553 | expout->language_defn = current_language; | |
554 | make_cleanup (free_current_contents, &expout); | |
555 | ||
556 | if (current_language->la_parser ()) | |
557 | current_language->la_error (NULL); | |
558 | ||
559 | discard_cleanups (old_chain); | |
560 | expout->nelts = expout_ptr; | |
561 | expout = (struct expression *) | |
1ab3bf1b | 562 | xrealloc ((char *) expout, |
3d6b6a90 JG |
563 | sizeof (struct expression) |
564 | + expout_ptr * sizeof (union exp_element)); | |
565 | prefixify_expression (expout); | |
566 | *stringptr = lexptr; | |
567 | return expout; | |
568 | } | |
569 | ||
570 | /* Parse STRING as an expression, and complain if this fails | |
571 | to use up all of the contents of STRING. */ | |
572 | ||
573 | struct expression * | |
574 | parse_expression (string) | |
575 | char *string; | |
576 | { | |
577 | register struct expression *exp; | |
578 | exp = parse_exp_1 (&string, 0, 0); | |
579 | if (*string) | |
580 | error ("Junk after end of expression."); | |
581 | return exp; | |
582 | } | |
583 | ||
584 | void | |
585 | push_type (tp) | |
586 | enum type_pieces tp; | |
587 | { | |
588 | if (type_stack_depth == type_stack_size) | |
589 | { | |
590 | type_stack_size *= 2; | |
591 | type_stack = (union type_stack_elt *) | |
1ab3bf1b | 592 | xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack)); |
3d6b6a90 JG |
593 | } |
594 | type_stack[type_stack_depth++].piece = tp; | |
595 | } | |
596 | ||
597 | void | |
598 | push_type_int (n) | |
599 | int n; | |
600 | { | |
601 | if (type_stack_depth == type_stack_size) | |
602 | { | |
603 | type_stack_size *= 2; | |
604 | type_stack = (union type_stack_elt *) | |
1ab3bf1b | 605 | xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack)); |
3d6b6a90 JG |
606 | } |
607 | type_stack[type_stack_depth++].int_val = n; | |
608 | } | |
609 | ||
610 | enum type_pieces | |
611 | pop_type () | |
612 | { | |
613 | if (type_stack_depth) | |
614 | return type_stack[--type_stack_depth].piece; | |
615 | return tp_end; | |
616 | } | |
617 | ||
618 | int | |
619 | pop_type_int () | |
620 | { | |
621 | if (type_stack_depth) | |
622 | return type_stack[--type_stack_depth].int_val; | |
623 | /* "Can't happen". */ | |
624 | return 0; | |
625 | } | |
626 | ||
627 | void | |
628 | _initialize_parse () | |
629 | { | |
630 | type_stack_size = 80; | |
631 | type_stack_depth = 0; | |
632 | type_stack = (union type_stack_elt *) | |
633 | xmalloc (type_stack_size * sizeof (*type_stack)); | |
634 | } |