Remove Makefile.in-29k-UDI. It's no longer needed now that the 29K stuff has
[deliverable/binutils-gdb.git] / gdb / parse.c
CommitLineData
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
6This file is part of GDB.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 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
41static void
42prefixify_expression PARAMS ((struct expression *));
43
44static int
45length_of_subexp PARAMS ((struct expression *, int));
46
47static void
48prefixify_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
a332e593
SC
53#ifdef NO_STD_REGS
54unsigned num_std_regs = 0;
55struct std_regs std_regs[1];
56#else
3d6b6a90 57struct std_regs std_regs[] = {
a332e593 58
3d6b6a90
JG
59#ifdef PC_REGNUM
60 { "pc", PC_REGNUM },
61#endif
62#ifdef FP_REGNUM
63 { "fp", FP_REGNUM },
64#endif
65#ifdef SP_REGNUM
66 { "sp", SP_REGNUM },
67#endif
68#ifdef PS_REGNUM
69 { "ps", PS_REGNUM },
70#endif
a332e593 71
3d6b6a90
JG
72};
73
74unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
75
a332e593
SC
76#endif
77
3d6b6a90
JG
78
79/* Begin counting arguments for a function call,
80 saving the data about any containing call. */
81
82void
83start_arglist ()
84{
85 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
86
87 new->next = funcall_chain;
88 new->arglist_len = arglist_len;
89 arglist_len = 0;
90 funcall_chain = new;
91}
92
93/* Return the number of arguments in a function call just terminated,
94 and restore the data for the containing function call. */
95
96int
97end_arglist ()
98{
99 register int val = arglist_len;
100 register struct funcall *call = funcall_chain;
101 funcall_chain = call->next;
102 arglist_len = call->arglist_len;
be772100 103 free ((PTR)call);
3d6b6a90
JG
104 return val;
105}
106
107/* Free everything in the funcall chain.
108 Used when there is an error inside parsing. */
109
110void
111free_funcalls ()
112{
113 register struct funcall *call, *next;
114
115 for (call = funcall_chain; call; call = next)
116 {
117 next = call->next;
be772100 118 free ((PTR)call);
3d6b6a90
JG
119 }
120}
121\f
122/* This page contains the functions for adding data to the struct expression
123 being constructed. */
124
125/* Add one element to the end of the expression. */
126
127/* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
128 a register through here */
129
130void
131write_exp_elt (expelt)
132 union exp_element expelt;
133{
134 if (expout_ptr >= expout_size)
135 {
136 expout_size *= 2;
81028ab0
FF
137 expout = (struct expression *)
138 xrealloc ((char *) expout, sizeof (struct expression)
139 + EXP_ELEM_TO_BYTES (expout_size));
3d6b6a90
JG
140 }
141 expout->elts[expout_ptr++] = expelt;
142}
143
144void
145write_exp_elt_opcode (expelt)
146 enum exp_opcode expelt;
147{
148 union exp_element tmp;
149
150 tmp.opcode = expelt;
151
152 write_exp_elt (tmp);
153}
154
155void
156write_exp_elt_sym (expelt)
157 struct symbol *expelt;
158{
159 union exp_element tmp;
160
161 tmp.symbol = expelt;
162
163 write_exp_elt (tmp);
164}
165
166void
167write_exp_elt_longcst (expelt)
168 LONGEST expelt;
169{
170 union exp_element tmp;
171
172 tmp.longconst = expelt;
173
174 write_exp_elt (tmp);
175}
176
177void
178write_exp_elt_dblcst (expelt)
179 double expelt;
180{
181 union exp_element tmp;
182
183 tmp.doubleconst = expelt;
184
185 write_exp_elt (tmp);
186}
187
188void
189write_exp_elt_type (expelt)
190 struct type *expelt;
191{
192 union exp_element tmp;
193
194 tmp.type = expelt;
195
196 write_exp_elt (tmp);
197}
198
199void
200write_exp_elt_intern (expelt)
201 struct internalvar *expelt;
202{
203 union exp_element tmp;
204
205 tmp.internalvar = expelt;
206
207 write_exp_elt (tmp);
208}
209
210/* Add a string constant to the end of the expression.
d1065385
FF
211
212 String constants are stored by first writing an expression element
213 that contains the length of the string, then stuffing the string
214 constant itself into however many expression elements are needed
215 to hold it, and then writing another expression element that contains
216 the length of the string. I.E. an expression element at each end of
217 the string records the string length, so you can skip over the
218 expression elements containing the actual string bytes from either
219 end of the string. Note that this also allows gdb to handle
220 strings with embedded null bytes, as is required for some languages.
221
222 Don't be fooled by the fact that the string is null byte terminated,
223 this is strictly for the convenience of debugging gdb itself. Gdb
224 Gdb does not depend up the string being null terminated, since the
225 actual length is recorded in expression elements at each end of the
226 string. The null byte is taken into consideration when computing how
227 many expression elements are required to hold the string constant, of
228 course. */
229
3d6b6a90
JG
230
231void
232write_exp_string (str)
233 struct stoken str;
234{
235 register int len = str.length;
d1065385
FF
236 register int lenelt;
237 register char *strdata;
3d6b6a90 238
d1065385
FF
239 /* Compute the number of expression elements required to hold the string
240 (including a null byte terminator), along with one expression element
241 at each end to record the actual string length (not including the
242 null byte terminator). */
3d6b6a90 243
81028ab0 244 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
d1065385
FF
245
246 /* Ensure that we have enough available expression elements to store
247 everything. */
248
249 if ((expout_ptr + lenelt) >= expout_size)
3d6b6a90 250 {
d1065385 251 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
3d6b6a90 252 expout = (struct expression *)
1ab3bf1b 253 xrealloc ((char *) expout, (sizeof (struct expression)
81028ab0 254 + EXP_ELEM_TO_BYTES (expout_size)));
3d6b6a90 255 }
d1065385
FF
256
257 /* Write the leading length expression element (which advances the current
258 expression element index), then write the string constant followed by a
259 terminating null byte, and then write the trailing length expression
260 element. */
261
262 write_exp_elt_longcst ((LONGEST) len);
263 strdata = (char *) &expout->elts[expout_ptr];
264 memcpy (strdata, str.ptr, len);
265 *(strdata + len) = '\0';
266 expout_ptr += lenelt - 2;
3d6b6a90
JG
267 write_exp_elt_longcst ((LONGEST) len);
268}
81028ab0
FF
269
270/* Add a bitstring constant to the end of the expression.
271
272 Bitstring constants are stored by first writing an expression element
273 that contains the length of the bitstring (in bits), then stuffing the
274 bitstring constant itself into however many expression elements are
275 needed to hold it, and then writing another expression element that
276 contains the length of the bitstring. I.E. an expression element at
277 each end of the bitstring records the bitstring length, so you can skip
278 over the expression elements containing the actual bitstring bytes from
279 either end of the bitstring. */
280
281void
282write_exp_bitstring (str)
283 struct stoken str;
284{
285 register int bits = str.length; /* length in bits */
286 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
287 register int lenelt;
288 register char *strdata;
289
290 /* Compute the number of expression elements required to hold the bitstring,
291 along with one expression element at each end to record the actual
292 bitstring length in bits. */
293
294 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
295
296 /* Ensure that we have enough available expression elements to store
297 everything. */
298
299 if ((expout_ptr + lenelt) >= expout_size)
300 {
301 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
302 expout = (struct expression *)
303 xrealloc ((char *) expout, (sizeof (struct expression)
304 + EXP_ELEM_TO_BYTES (expout_size)));
305 }
306
307 /* Write the leading length expression element (which advances the current
308 expression element index), then write the bitstring constant, and then
309 write the trailing length expression element. */
310
311 write_exp_elt_longcst ((LONGEST) bits);
312 strdata = (char *) &expout->elts[expout_ptr];
313 memcpy (strdata, str.ptr, len);
314 expout_ptr += lenelt - 2;
315 write_exp_elt_longcst ((LONGEST) bits);
316}
3d6b6a90
JG
317\f
318/* Return a null-terminated temporary copy of the name
319 of a string token. */
320
321char *
322copy_name (token)
323 struct stoken token;
324{
4ed3a9ea 325 memcpy (namecopy, token.ptr, token.length);
3d6b6a90
JG
326 namecopy[token.length] = 0;
327 return namecopy;
328}
329\f
330/* Reverse an expression from suffix form (in which it is constructed)
331 to prefix form (in which we can conveniently print or execute it). */
332
1ab3bf1b 333static void
3d6b6a90
JG
334prefixify_expression (expr)
335 register struct expression *expr;
336{
81028ab0
FF
337 register int len =
338 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
3d6b6a90
JG
339 register struct expression *temp;
340 register int inpos = expr->nelts, outpos = 0;
341
342 temp = (struct expression *) alloca (len);
343
344 /* Copy the original expression into temp. */
4ed3a9ea 345 memcpy (temp, expr, len);
3d6b6a90
JG
346
347 prefixify_subexp (temp, expr, inpos, outpos);
348}
349
350/* Return the number of exp_elements in the subexpression of EXPR
351 whose last exp_element is at index ENDPOS - 1 in EXPR. */
352
1ab3bf1b 353static int
3d6b6a90
JG
354length_of_subexp (expr, endpos)
355 register struct expression *expr;
356 register int endpos;
357{
358 register int oplen = 1;
359 register int args = 0;
360 register int i;
361
d1065385 362 if (endpos < 1)
3d6b6a90
JG
363 error ("?error in length_of_subexp");
364
365 i = (int) expr->elts[endpos - 1].opcode;
366
367 switch (i)
368 {
369 /* C++ */
370 case OP_SCOPE:
81028ab0
FF
371 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
372 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
3d6b6a90
JG
373 break;
374
375 case OP_LONG:
376 case OP_DOUBLE:
377 oplen = 4;
378 break;
379
380 case OP_TYPE:
381 case OP_BOOL:
382 case OP_VAR_VALUE:
383 case OP_LAST:
384 case OP_REGISTER:
385 case OP_INTERNALVAR:
386 oplen = 3;
387 break;
388
389 case OP_FUNCALL:
390 oplen = 3;
d1065385 391 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
3d6b6a90
JG
392 break;
393
394 case UNOP_MAX:
395 case UNOP_MIN:
396 oplen = 3;
3d6b6a90
JG
397 break;
398
399 case BINOP_VAL:
400 case UNOP_CAST:
401 case UNOP_MEMVAL:
402 oplen = 3;
403 args = 1;
404 break;
405
406 case UNOP_ABS:
407 case UNOP_CAP:
408 case UNOP_CHR:
409 case UNOP_FLOAT:
410 case UNOP_HIGH:
411 case UNOP_ODD:
412 case UNOP_ORD:
413 case UNOP_TRUNC:
414 oplen = 1;
415 args = 1;
416 break;
417
2640f7e1
JG
418 case STRUCTOP_STRUCT:
419 case STRUCTOP_PTR:
420 args = 1;
d1065385 421 /* fall through */
3d6b6a90
JG
422 case OP_M2_STRING:
423 case OP_STRING:
81028ab0
FF
424 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
425 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
426 break;
427
428 case OP_BITSTRING:
429 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
430 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
431 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
3d6b6a90
JG
432 break;
433
c4413e2c
FF
434 case OP_ARRAY:
435 oplen = 4;
436 args = longest_to_int (expr->elts[endpos - 2].longconst);
437 args -= longest_to_int (expr->elts[endpos - 3].longconst);
438 args += 1;
439 break;
440
3d6b6a90
JG
441 case TERNOP_COND:
442 args = 3;
443 break;
444
445 /* Modula-2 */
54bbbfb4 446 case MULTI_SUBSCRIPT:
3d6b6a90 447 oplen=3;
d1065385 448 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
3d6b6a90
JG
449 break;
450
451 case BINOP_ASSIGN_MODIFY:
452 oplen = 3;
453 args = 2;
454 break;
455
456 /* C++ */
457 case OP_THIS:
458 oplen = 2;
459 break;
460
461 default:
462 args = 1 + (i < (int) BINOP_END);
463 }
464
465 while (args > 0)
466 {
467 oplen += length_of_subexp (expr, endpos - oplen);
468 args--;
469 }
470
471 return oplen;
472}
473
474/* Copy the subexpression ending just before index INEND in INEXPR
475 into OUTEXPR, starting at index OUTBEG.
476 In the process, convert it from suffix to prefix form. */
477
478static void
479prefixify_subexp (inexpr, outexpr, inend, outbeg)
480 register struct expression *inexpr;
481 struct expression *outexpr;
482 register int inend;
483 int outbeg;
484{
485 register int oplen = 1;
486 register int args = 0;
487 register int i;
488 int *arglens;
489 enum exp_opcode opcode;
490
491 /* Compute how long the last operation is (in OPLEN),
492 and also how many preceding subexpressions serve as
493 arguments for it (in ARGS). */
494
495 opcode = inexpr->elts[inend - 1].opcode;
496 switch (opcode)
497 {
498 /* C++ */
499 case OP_SCOPE:
81028ab0
FF
500 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
501 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
3d6b6a90
JG
502 break;
503
504 case OP_LONG:
505 case OP_DOUBLE:
506 oplen = 4;
507 break;
508
509 case OP_TYPE:
510 case OP_BOOL:
511 case OP_VAR_VALUE:
512 case OP_LAST:
513 case OP_REGISTER:
514 case OP_INTERNALVAR:
515 oplen = 3;
516 break;
517
518 case OP_FUNCALL:
519 oplen = 3;
d1065385 520 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
3d6b6a90
JG
521 break;
522
523 case UNOP_MIN:
524 case UNOP_MAX:
525 oplen = 3;
3d6b6a90
JG
526 break;
527
528 case UNOP_CAST:
529 case UNOP_MEMVAL:
530 oplen = 3;
531 args = 1;
532 break;
533
534 case UNOP_ABS:
535 case UNOP_CAP:
536 case UNOP_CHR:
537 case UNOP_FLOAT:
538 case UNOP_HIGH:
539 case UNOP_ODD:
540 case UNOP_ORD:
541 case UNOP_TRUNC:
542 oplen=1;
543 args=1;
544 break;
545
61c1724b 546 case STRUCTOP_STRUCT:
2640f7e1
JG
547 case STRUCTOP_PTR:
548 args = 1;
d1065385 549 /* fall through */
3d6b6a90
JG
550 case OP_M2_STRING:
551 case OP_STRING:
81028ab0
FF
552 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
553 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
554 break;
555
556 case OP_BITSTRING:
557 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
558 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
559 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
3d6b6a90
JG
560 break;
561
c4413e2c
FF
562 case OP_ARRAY:
563 oplen = 4;
564 args = longest_to_int (inexpr->elts[inend - 2].longconst);
565 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
566 args += 1;
567 break;
568
3d6b6a90
JG
569 case TERNOP_COND:
570 args = 3;
571 break;
572
573 case BINOP_ASSIGN_MODIFY:
574 oplen = 3;
575 args = 2;
576 break;
577
578 /* Modula-2 */
54bbbfb4 579 case MULTI_SUBSCRIPT:
3d6b6a90 580 oplen=3;
d1065385 581 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
3d6b6a90
JG
582 break;
583
584 /* C++ */
585 case OP_THIS:
586 oplen = 2;
587 break;
588
589 default:
590 args = 1 + ((int) opcode < (int) BINOP_END);
591 }
592
593 /* Copy the final operator itself, from the end of the input
594 to the beginning of the output. */
595 inend -= oplen;
4ed3a9ea 596 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
81028ab0 597 EXP_ELEM_TO_BYTES (oplen));
3d6b6a90
JG
598 outbeg += oplen;
599
600 /* Find the lengths of the arg subexpressions. */
601 arglens = (int *) alloca (args * sizeof (int));
602 for (i = args - 1; i >= 0; i--)
603 {
604 oplen = length_of_subexp (inexpr, inend);
605 arglens[i] = oplen;
606 inend -= oplen;
607 }
608
609 /* Now copy each subexpression, preserving the order of
610 the subexpressions, but prefixifying each one.
611 In this loop, inend starts at the beginning of
612 the expression this level is working on
613 and marches forward over the arguments.
614 outbeg does similarly in the output. */
615 for (i = 0; i < args; i++)
616 {
617 oplen = arglens[i];
618 inend += oplen;
619 prefixify_subexp (inexpr, outexpr, inend, outbeg);
620 outbeg += oplen;
621 }
622}
623\f
624/* This page contains the two entry points to this file. */
625
626/* Read an expression from the string *STRINGPTR points to,
627 parse it, and return a pointer to a struct expression that we malloc.
628 Use block BLOCK as the lexical context for variable names;
629 if BLOCK is zero, use the block of the selected stack frame.
630 Meanwhile, advance *STRINGPTR to point after the expression,
631 at the first nonwhite character that is not part of the expression
632 (possibly a null character).
633
634 If COMMA is nonzero, stop if a comma is reached. */
635
636struct expression *
637parse_exp_1 (stringptr, block, comma)
638 char **stringptr;
639 struct block *block;
640 int comma;
641{
642 struct cleanup *old_chain;
643
644 lexptr = *stringptr;
645
646 paren_depth = 0;
647 type_stack_depth = 0;
648
649 comma_terminates = comma;
650
651 if (lexptr == 0 || *lexptr == 0)
652 error_no_arg ("expression to compute");
653
654 old_chain = make_cleanup (free_funcalls, 0);
655 funcall_chain = 0;
656
657 expression_context_block = block ? block : get_selected_block ();
658
659 namecopy = (char *) alloca (strlen (lexptr) + 1);
660 expout_size = 10;
661 expout_ptr = 0;
662 expout = (struct expression *)
81028ab0 663 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
3d6b6a90
JG
664 expout->language_defn = current_language;
665 make_cleanup (free_current_contents, &expout);
666
667 if (current_language->la_parser ())
668 current_language->la_error (NULL);
669
670 discard_cleanups (old_chain);
54bbbfb4
FF
671
672 /* Record the actual number of expression elements, and then
673 reallocate the expression memory so that we free up any
674 excess elements. */
675
3d6b6a90
JG
676 expout->nelts = expout_ptr;
677 expout = (struct expression *)
1ab3bf1b 678 xrealloc ((char *) expout,
81028ab0 679 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
54bbbfb4
FF
680
681 /* Convert expression from postfix form as generated by yacc
682 parser, to a prefix form. */
683
684 DUMP_EXPRESSION (expout, stdout, "before conversion to prefix form");
3d6b6a90 685 prefixify_expression (expout);
54bbbfb4
FF
686 DUMP_EXPRESSION (expout, stdout, "after conversion to prefix form");
687
3d6b6a90
JG
688 *stringptr = lexptr;
689 return expout;
690}
691
692/* Parse STRING as an expression, and complain if this fails
693 to use up all of the contents of STRING. */
694
695struct expression *
696parse_expression (string)
697 char *string;
698{
699 register struct expression *exp;
700 exp = parse_exp_1 (&string, 0, 0);
701 if (*string)
702 error ("Junk after end of expression.");
703 return exp;
704}
705
706void
707push_type (tp)
708 enum type_pieces tp;
709{
710 if (type_stack_depth == type_stack_size)
711 {
712 type_stack_size *= 2;
713 type_stack = (union type_stack_elt *)
1ab3bf1b 714 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
3d6b6a90
JG
715 }
716 type_stack[type_stack_depth++].piece = tp;
717}
718
719void
720push_type_int (n)
721 int n;
722{
723 if (type_stack_depth == type_stack_size)
724 {
725 type_stack_size *= 2;
726 type_stack = (union type_stack_elt *)
1ab3bf1b 727 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
3d6b6a90
JG
728 }
729 type_stack[type_stack_depth++].int_val = n;
730}
731
732enum type_pieces
733pop_type ()
734{
735 if (type_stack_depth)
736 return type_stack[--type_stack_depth].piece;
737 return tp_end;
738}
739
740int
741pop_type_int ()
742{
743 if (type_stack_depth)
744 return type_stack[--type_stack_depth].int_val;
745 /* "Can't happen". */
746 return 0;
747}
748
749void
750_initialize_parse ()
751{
752 type_stack_size = 80;
753 type_stack_depth = 0;
754 type_stack = (union type_stack_elt *)
755 xmalloc (type_stack_size * sizeof (*type_stack));
756}
This page took 0.097614 seconds and 4 git commands to generate.