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