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