Mon Nov 9 18:22:55 1998 Dave Brolley <brolley@cygnus.com>
[deliverable/binutils-gdb.git] / gdb / parse.c
CommitLineData
3d6b6a90 1/* Parse expressions for GDB.
ad3b8c4a 2 Copyright (C) 1986, 89, 90, 91, 94, 1998 Free Software Foundation, Inc.
3d6b6a90
JG
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
6c9638b4 20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
3d6b6a90
JG
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"
2b576293 32#include "gdb_string.h"
3d6b6a90 33#include "symtab.h"
1ab3bf1b 34#include "gdbtypes.h"
3d6b6a90
JG
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"
433732f2 41#include "gdbcmd.h"
36297ff3 42#include "symfile.h" /* for overlay functions */
79448221
JK
43\f
44/* Global variables declared in parser-defs.h (and commented there). */
45struct expression *expout;
46int expout_size;
47int expout_ptr;
48struct block *expression_context_block;
49struct block *innermost_block;
79448221
JK
50int arglist_len;
51union type_stack_elt *type_stack;
52int type_stack_depth, type_stack_size;
53char *lexptr;
54char *namecopy;
55int paren_depth;
56int comma_terminates;
57\f
433732f2
SG
58#ifdef MAINTENANCE_CMDS
59static int expressiondebug = 0;
60#endif
61
9da75ad3
FF
62static void
63free_funcalls PARAMS ((void));
64
1ab3bf1b
JG
65static void
66prefixify_expression PARAMS ((struct expression *));
67
1ab3bf1b
JG
68static void
69prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
70
9da75ad3
FF
71/* Data structure for saving values of arglist_len for function calls whose
72 arguments contain other function calls. */
73
74struct funcall
75 {
76 struct funcall *next;
77 int arglist_len;
78 };
79
80static struct funcall *funcall_chain;
81
3d6b6a90
JG
82/* Assign machine-independent names to certain registers
83 (unless overridden by the REGISTER_NAMES table) */
84
a332e593
SC
85#ifdef NO_STD_REGS
86unsigned num_std_regs = 0;
87struct std_regs std_regs[1];
88#else
3d6b6a90 89struct std_regs std_regs[] = {
a332e593 90
3d6b6a90
JG
91#ifdef PC_REGNUM
92 { "pc", PC_REGNUM },
93#endif
94#ifdef FP_REGNUM
95 { "fp", FP_REGNUM },
96#endif
97#ifdef SP_REGNUM
98 { "sp", SP_REGNUM },
99#endif
100#ifdef PS_REGNUM
101 { "ps", PS_REGNUM },
102#endif
a332e593 103
3d6b6a90
JG
104};
105
106unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
107
a332e593
SC
108#endif
109
678fa7ff
RU
110/* The generic method for targets to specify how their registers are named.
111 The mapping can be derived from three sources: reg_names; std_regs; or
112 a target specific alias hook. */
113
114int
115target_map_name_to_register (str, len)
116 char *str;
117 int len;
118{
119 int i;
120
3b3835cc
RU
121 /* First try target specific aliases. We try these first because on some
122 systems standard names can be context dependent (eg. $pc on a
123 multiprocessor can be could be any of several PCs). */
124#ifdef REGISTER_NAME_ALIAS_HOOK
125 i = REGISTER_NAME_ALIAS_HOOK (str, len);
126 if (i >= 0)
127 return i;
128#endif
129
130 /* Search architectural register name space. */
678fa7ff
RU
131 for (i = 0; i < NUM_REGS; i++)
132 if (reg_names[i] && len == strlen (reg_names[i])
133 && STREQN (str, reg_names[i], len))
134 {
135 return i;
136 }
137
138 /* Try standard aliases */
139 for (i = 0; i < num_std_regs; i++)
140 if (std_regs[i].name && len == strlen (std_regs[i].name)
141 && STREQN (str, std_regs[i].name, len))
142 {
143 return std_regs[i].regnum;
144 }
145
678fa7ff
RU
146 return -1;
147}
3d6b6a90
JG
148
149/* Begin counting arguments for a function call,
150 saving the data about any containing call. */
151
152void
153start_arglist ()
154{
9da75ad3 155 register struct funcall *new;
3d6b6a90 156
9da75ad3 157 new = (struct funcall *) xmalloc (sizeof (struct funcall));
3d6b6a90
JG
158 new->next = funcall_chain;
159 new->arglist_len = arglist_len;
160 arglist_len = 0;
161 funcall_chain = new;
162}
163
164/* Return the number of arguments in a function call just terminated,
165 and restore the data for the containing function call. */
166
167int
168end_arglist ()
169{
170 register int val = arglist_len;
171 register struct funcall *call = funcall_chain;
172 funcall_chain = call->next;
173 arglist_len = call->arglist_len;
be772100 174 free ((PTR)call);
3d6b6a90
JG
175 return val;
176}
177
178/* Free everything in the funcall chain.
179 Used when there is an error inside parsing. */
180
9da75ad3 181static void
3d6b6a90
JG
182free_funcalls ()
183{
184 register struct funcall *call, *next;
185
186 for (call = funcall_chain; call; call = next)
187 {
188 next = call->next;
be772100 189 free ((PTR)call);
3d6b6a90
JG
190 }
191}
192\f
193/* This page contains the functions for adding data to the struct expression
194 being constructed. */
195
196/* Add one element to the end of the expression. */
197
198/* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
199 a register through here */
200
201void
202write_exp_elt (expelt)
203 union exp_element expelt;
204{
205 if (expout_ptr >= expout_size)
206 {
207 expout_size *= 2;
81028ab0
FF
208 expout = (struct expression *)
209 xrealloc ((char *) expout, sizeof (struct expression)
210 + EXP_ELEM_TO_BYTES (expout_size));
3d6b6a90
JG
211 }
212 expout->elts[expout_ptr++] = expelt;
213}
214
215void
216write_exp_elt_opcode (expelt)
217 enum exp_opcode expelt;
218{
219 union exp_element tmp;
220
221 tmp.opcode = expelt;
222
223 write_exp_elt (tmp);
224}
225
226void
227write_exp_elt_sym (expelt)
228 struct symbol *expelt;
229{
230 union exp_element tmp;
231
232 tmp.symbol = expelt;
233
234 write_exp_elt (tmp);
235}
236
479fdd26
JK
237void
238write_exp_elt_block (b)
239 struct block *b;
240{
241 union exp_element tmp;
242 tmp.block = b;
243 write_exp_elt (tmp);
244}
245
3d6b6a90
JG
246void
247write_exp_elt_longcst (expelt)
248 LONGEST expelt;
249{
250 union exp_element tmp;
251
252 tmp.longconst = expelt;
253
254 write_exp_elt (tmp);
255}
256
257void
258write_exp_elt_dblcst (expelt)
aa220473 259 DOUBLEST expelt;
3d6b6a90
JG
260{
261 union exp_element tmp;
262
263 tmp.doubleconst = expelt;
264
265 write_exp_elt (tmp);
266}
267
268void
269write_exp_elt_type (expelt)
270 struct type *expelt;
271{
272 union exp_element tmp;
273
274 tmp.type = expelt;
275
276 write_exp_elt (tmp);
277}
278
279void
280write_exp_elt_intern (expelt)
281 struct internalvar *expelt;
282{
283 union exp_element tmp;
284
285 tmp.internalvar = expelt;
286
287 write_exp_elt (tmp);
288}
289
290/* Add a string constant to the end of the expression.
d1065385
FF
291
292 String constants are stored by first writing an expression element
293 that contains the length of the string, then stuffing the string
294 constant itself into however many expression elements are needed
295 to hold it, and then writing another expression element that contains
296 the length of the string. I.E. an expression element at each end of
297 the string records the string length, so you can skip over the
298 expression elements containing the actual string bytes from either
299 end of the string. Note that this also allows gdb to handle
300 strings with embedded null bytes, as is required for some languages.
301
302 Don't be fooled by the fact that the string is null byte terminated,
303 this is strictly for the convenience of debugging gdb itself. Gdb
304 Gdb does not depend up the string being null terminated, since the
305 actual length is recorded in expression elements at each end of the
306 string. The null byte is taken into consideration when computing how
307 many expression elements are required to hold the string constant, of
308 course. */
309
3d6b6a90
JG
310
311void
312write_exp_string (str)
313 struct stoken str;
314{
315 register int len = str.length;
d1065385
FF
316 register int lenelt;
317 register char *strdata;
3d6b6a90 318
d1065385
FF
319 /* Compute the number of expression elements required to hold the string
320 (including a null byte terminator), along with one expression element
321 at each end to record the actual string length (not including the
322 null byte terminator). */
3d6b6a90 323
81028ab0 324 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
d1065385
FF
325
326 /* Ensure that we have enough available expression elements to store
327 everything. */
328
329 if ((expout_ptr + lenelt) >= expout_size)
3d6b6a90 330 {
d1065385 331 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
3d6b6a90 332 expout = (struct expression *)
1ab3bf1b 333 xrealloc ((char *) expout, (sizeof (struct expression)
81028ab0 334 + EXP_ELEM_TO_BYTES (expout_size)));
3d6b6a90 335 }
d1065385
FF
336
337 /* Write the leading length expression element (which advances the current
338 expression element index), then write the string constant followed by a
339 terminating null byte, and then write the trailing length expression
340 element. */
341
342 write_exp_elt_longcst ((LONGEST) len);
343 strdata = (char *) &expout->elts[expout_ptr];
344 memcpy (strdata, str.ptr, len);
345 *(strdata + len) = '\0';
346 expout_ptr += lenelt - 2;
3d6b6a90
JG
347 write_exp_elt_longcst ((LONGEST) len);
348}
81028ab0
FF
349
350/* Add a bitstring constant to the end of the expression.
351
352 Bitstring constants are stored by first writing an expression element
353 that contains the length of the bitstring (in bits), then stuffing the
354 bitstring constant itself into however many expression elements are
355 needed to hold it, and then writing another expression element that
356 contains the length of the bitstring. I.E. an expression element at
357 each end of the bitstring records the bitstring length, so you can skip
358 over the expression elements containing the actual bitstring bytes from
359 either end of the bitstring. */
360
361void
362write_exp_bitstring (str)
363 struct stoken str;
364{
365 register int bits = str.length; /* length in bits */
366 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
367 register int lenelt;
368 register char *strdata;
369
370 /* Compute the number of expression elements required to hold the bitstring,
371 along with one expression element at each end to record the actual
372 bitstring length in bits. */
373
374 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
375
376 /* Ensure that we have enough available expression elements to store
377 everything. */
378
379 if ((expout_ptr + lenelt) >= expout_size)
380 {
381 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
382 expout = (struct expression *)
383 xrealloc ((char *) expout, (sizeof (struct expression)
384 + EXP_ELEM_TO_BYTES (expout_size)));
385 }
386
387 /* Write the leading length expression element (which advances the current
388 expression element index), then write the bitstring constant, and then
389 write the trailing length expression element. */
390
391 write_exp_elt_longcst ((LONGEST) bits);
392 strdata = (char *) &expout->elts[expout_ptr];
393 memcpy (strdata, str.ptr, len);
394 expout_ptr += lenelt - 2;
395 write_exp_elt_longcst ((LONGEST) bits);
396}
abe28b92
JK
397
398/* Add the appropriate elements for a minimal symbol to the end of
3fb93d86
JK
399 the expression. The rationale behind passing in text_symbol_type and
400 data_symbol_type was so that Modula-2 could pass in WORD for
401 data_symbol_type. Perhaps it still is useful to have those types vary
402 based on the language, but they no longer have names like "int", so
403 the initial rationale is gone. */
404
405static struct type *msym_text_symbol_type;
406static struct type *msym_data_symbol_type;
407static struct type *msym_unknown_symbol_type;
abe28b92
JK
408
409void
410write_exp_msymbol (msymbol, text_symbol_type, data_symbol_type)
411 struct minimal_symbol *msymbol;
412 struct type *text_symbol_type;
413 struct type *data_symbol_type;
414{
36297ff3
RU
415 CORE_ADDR addr;
416
abe28b92 417 write_exp_elt_opcode (OP_LONG);
4461196e 418 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
36297ff3
RU
419
420 addr = SYMBOL_VALUE_ADDRESS (msymbol);
421 if (overlay_debugging)
422 addr = symbol_overlayed_address (addr, SYMBOL_BFD_SECTION (msymbol));
423 write_exp_elt_longcst ((LONGEST) addr);
424
abe28b92
JK
425 write_exp_elt_opcode (OP_LONG);
426
427 write_exp_elt_opcode (UNOP_MEMVAL);
428 switch (msymbol -> type)
429 {
430 case mst_text:
431 case mst_file_text:
ae6d035d 432 case mst_solib_trampoline:
3fb93d86 433 write_exp_elt_type (msym_text_symbol_type);
abe28b92
JK
434 break;
435
436 case mst_data:
437 case mst_file_data:
438 case mst_bss:
439 case mst_file_bss:
3fb93d86 440 write_exp_elt_type (msym_data_symbol_type);
abe28b92
JK
441 break;
442
443 default:
3fb93d86 444 write_exp_elt_type (msym_unknown_symbol_type);
abe28b92
JK
445 break;
446 }
447 write_exp_elt_opcode (UNOP_MEMVAL);
448}
3d6b6a90 449\f
c700638c
PB
450/* Recognize tokens that start with '$'. These include:
451
452 $regname A native register name or a "standard
453 register name".
454
455 $variable A convenience variable with a name chosen
456 by the user.
457
458 $digits Value history with index <digits>, starting
459 from the first value which has index 1.
460
461 $$digits Value history with index <digits> relative
462 to the last value. I.E. $$0 is the last
463 value, $$1 is the one previous to that, $$2
464 is the one previous to $$1, etc.
465
466 $ | $0 | $$0 The last value in the value history.
467
468 $$ An abbreviation for the second to the last
469 value in the value history, I.E. $$1
470
471 */
472
473void
474write_dollar_variable (str)
475 struct stoken str;
476{
477 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
478 and $$digits (equivalent to $<-digits> if you could type that). */
479
480 int negate = 0;
481 int i = 1;
482 /* Double dollar means negate the number and add -1 as well.
483 Thus $$ alone means -1. */
484 if (str.length >= 2 && str.ptr[1] == '$')
485 {
486 negate = 1;
487 i = 2;
488 }
489 if (i == str.length)
490 {
491 /* Just dollars (one or two) */
492 i = - negate;
493 goto handle_last;
494 }
495 /* Is the rest of the token digits? */
496 for (; i < str.length; i++)
497 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
498 break;
499 if (i == str.length)
500 {
501 i = atoi (str.ptr + 1 + negate);
502 if (negate)
503 i = - i;
504 goto handle_last;
505 }
506
507 /* Handle tokens that refer to machine registers:
508 $ followed by a register name. */
678fa7ff
RU
509 i = target_map_name_to_register( str.ptr + 1, str.length - 1 );
510 if( i >= 0 )
511 goto handle_register;
c700638c
PB
512
513 /* Any other names starting in $ are debugger internal variables. */
514
515 write_exp_elt_opcode (OP_INTERNALVAR);
516 write_exp_elt_intern (lookup_internalvar (copy_name (str) + 1));
517 write_exp_elt_opcode (OP_INTERNALVAR);
518 return;
519 handle_last:
520 write_exp_elt_opcode (OP_LAST);
521 write_exp_elt_longcst ((LONGEST) i);
522 write_exp_elt_opcode (OP_LAST);
523 return;
524 handle_register:
525 write_exp_elt_opcode (OP_REGISTER);
526 write_exp_elt_longcst (i);
527 write_exp_elt_opcode (OP_REGISTER);
528 return;
529}
530\f
3d6b6a90
JG
531/* Return a null-terminated temporary copy of the name
532 of a string token. */
533
534char *
535copy_name (token)
536 struct stoken token;
537{
4ed3a9ea 538 memcpy (namecopy, token.ptr, token.length);
3d6b6a90
JG
539 namecopy[token.length] = 0;
540 return namecopy;
541}
542\f
543/* Reverse an expression from suffix form (in which it is constructed)
544 to prefix form (in which we can conveniently print or execute it). */
545
1ab3bf1b 546static void
3d6b6a90
JG
547prefixify_expression (expr)
548 register struct expression *expr;
549{
81028ab0
FF
550 register int len =
551 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
3d6b6a90
JG
552 register struct expression *temp;
553 register int inpos = expr->nelts, outpos = 0;
554
555 temp = (struct expression *) alloca (len);
556
557 /* Copy the original expression into temp. */
4ed3a9ea 558 memcpy (temp, expr, len);
3d6b6a90
JG
559
560 prefixify_subexp (temp, expr, inpos, outpos);
561}
562
563/* Return the number of exp_elements in the subexpression of EXPR
564 whose last exp_element is at index ENDPOS - 1 in EXPR. */
565
8d2755a9 566int
3d6b6a90
JG
567length_of_subexp (expr, endpos)
568 register struct expression *expr;
569 register int endpos;
570{
571 register int oplen = 1;
572 register int args = 0;
573 register int i;
574
d1065385 575 if (endpos < 1)
3d6b6a90
JG
576 error ("?error in length_of_subexp");
577
578 i = (int) expr->elts[endpos - 1].opcode;
579
580 switch (i)
581 {
582 /* C++ */
583 case OP_SCOPE:
81028ab0
FF
584 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
585 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
3d6b6a90
JG
586 break;
587
588 case OP_LONG:
589 case OP_DOUBLE:
479fdd26 590 case OP_VAR_VALUE:
3d6b6a90
JG
591 oplen = 4;
592 break;
593
594 case OP_TYPE:
595 case OP_BOOL:
3d6b6a90
JG
596 case OP_LAST:
597 case OP_REGISTER:
598 case OP_INTERNALVAR:
599 oplen = 3;
600 break;
601
ead95f8a 602 case OP_COMPLEX:
a91a6192
SS
603 oplen = 1;
604 args = 2;
605 break;
606
3d6b6a90 607 case OP_FUNCALL:
a91a6192 608 case OP_F77_UNDETERMINED_ARGLIST:
3d6b6a90 609 oplen = 3;
d1065385 610 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
3d6b6a90
JG
611 break;
612
613 case UNOP_MAX:
614 case UNOP_MIN:
615 oplen = 3;
3d6b6a90
JG
616 break;
617
618 case BINOP_VAL:
619 case UNOP_CAST:
620 case UNOP_MEMVAL:
621 oplen = 3;
622 args = 1;
623 break;
624
625 case UNOP_ABS:
626 case UNOP_CAP:
627 case UNOP_CHR:
628 case UNOP_FLOAT:
629 case UNOP_HIGH:
630 case UNOP_ODD:
631 case UNOP_ORD:
632 case UNOP_TRUNC:
633 oplen = 1;
634 args = 1;
635 break;
636
dcda44a0 637 case OP_LABELED:
2640f7e1
JG
638 case STRUCTOP_STRUCT:
639 case STRUCTOP_PTR:
640 args = 1;
d1065385 641 /* fall through */
3d6b6a90
JG
642 case OP_M2_STRING:
643 case OP_STRING:
3c02944a 644 case OP_NAME:
0e4ca328 645 case OP_EXPRSTRING:
81028ab0
FF
646 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
647 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
648 break;
649
650 case OP_BITSTRING:
651 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
652 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
653 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
3d6b6a90
JG
654 break;
655
c4413e2c
FF
656 case OP_ARRAY:
657 oplen = 4;
658 args = longest_to_int (expr->elts[endpos - 2].longconst);
659 args -= longest_to_int (expr->elts[endpos - 3].longconst);
660 args += 1;
661 break;
662
3d6b6a90 663 case TERNOP_COND:
f91a9e05
PB
664 case TERNOP_SLICE:
665 case TERNOP_SLICE_COUNT:
3d6b6a90
JG
666 args = 3;
667 break;
668
669 /* Modula-2 */
54bbbfb4 670 case MULTI_SUBSCRIPT:
a91a6192 671 oplen = 3;
d1065385 672 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
3d6b6a90
JG
673 break;
674
675 case BINOP_ASSIGN_MODIFY:
676 oplen = 3;
677 args = 2;
678 break;
679
680 /* C++ */
681 case OP_THIS:
682 oplen = 2;
683 break;
684
685 default:
686 args = 1 + (i < (int) BINOP_END);
687 }
688
689 while (args > 0)
690 {
691 oplen += length_of_subexp (expr, endpos - oplen);
692 args--;
693 }
694
695 return oplen;
696}
697
698/* Copy the subexpression ending just before index INEND in INEXPR
699 into OUTEXPR, starting at index OUTBEG.
700 In the process, convert it from suffix to prefix form. */
701
702static void
703prefixify_subexp (inexpr, outexpr, inend, outbeg)
704 register struct expression *inexpr;
705 struct expression *outexpr;
706 register int inend;
707 int outbeg;
708{
709 register int oplen = 1;
710 register int args = 0;
711 register int i;
712 int *arglens;
713 enum exp_opcode opcode;
714
715 /* Compute how long the last operation is (in OPLEN),
716 and also how many preceding subexpressions serve as
717 arguments for it (in ARGS). */
718
719 opcode = inexpr->elts[inend - 1].opcode;
720 switch (opcode)
721 {
722 /* C++ */
723 case OP_SCOPE:
81028ab0
FF
724 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
725 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
3d6b6a90
JG
726 break;
727
728 case OP_LONG:
729 case OP_DOUBLE:
479fdd26 730 case OP_VAR_VALUE:
3d6b6a90
JG
731 oplen = 4;
732 break;
733
734 case OP_TYPE:
735 case OP_BOOL:
3d6b6a90
JG
736 case OP_LAST:
737 case OP_REGISTER:
738 case OP_INTERNALVAR:
739 oplen = 3;
740 break;
741
ead95f8a 742 case OP_COMPLEX:
a91a6192
SS
743 oplen = 1;
744 args = 2;
745 break;
746
3d6b6a90 747 case OP_FUNCALL:
a91a6192 748 case OP_F77_UNDETERMINED_ARGLIST:
3d6b6a90 749 oplen = 3;
d1065385 750 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
3d6b6a90
JG
751 break;
752
753 case UNOP_MIN:
754 case UNOP_MAX:
755 oplen = 3;
3d6b6a90
JG
756 break;
757
758 case UNOP_CAST:
759 case UNOP_MEMVAL:
760 oplen = 3;
761 args = 1;
762 break;
763
764 case UNOP_ABS:
765 case UNOP_CAP:
766 case UNOP_CHR:
767 case UNOP_FLOAT:
768 case UNOP_HIGH:
769 case UNOP_ODD:
770 case UNOP_ORD:
771 case UNOP_TRUNC:
772 oplen=1;
773 args=1;
774 break;
775
61c1724b 776 case STRUCTOP_STRUCT:
2640f7e1 777 case STRUCTOP_PTR:
dcda44a0 778 case OP_LABELED:
2640f7e1 779 args = 1;
d1065385 780 /* fall through */
3d6b6a90
JG
781 case OP_M2_STRING:
782 case OP_STRING:
3c02944a 783 case OP_NAME:
0e4ca328 784 case OP_EXPRSTRING:
81028ab0
FF
785 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
786 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
787 break;
788
789 case OP_BITSTRING:
790 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
791 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
792 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
3d6b6a90
JG
793 break;
794
c4413e2c
FF
795 case OP_ARRAY:
796 oplen = 4;
797 args = longest_to_int (inexpr->elts[inend - 2].longconst);
798 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
799 args += 1;
800 break;
801
3d6b6a90 802 case TERNOP_COND:
f91a9e05
PB
803 case TERNOP_SLICE:
804 case TERNOP_SLICE_COUNT:
3d6b6a90
JG
805 args = 3;
806 break;
807
808 case BINOP_ASSIGN_MODIFY:
809 oplen = 3;
810 args = 2;
811 break;
812
813 /* Modula-2 */
54bbbfb4 814 case MULTI_SUBSCRIPT:
a91a6192 815 oplen = 3;
d1065385 816 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
3d6b6a90
JG
817 break;
818
819 /* C++ */
820 case OP_THIS:
821 oplen = 2;
822 break;
823
824 default:
825 args = 1 + ((int) opcode < (int) BINOP_END);
826 }
827
828 /* Copy the final operator itself, from the end of the input
829 to the beginning of the output. */
830 inend -= oplen;
4ed3a9ea 831 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
81028ab0 832 EXP_ELEM_TO_BYTES (oplen));
3d6b6a90
JG
833 outbeg += oplen;
834
835 /* Find the lengths of the arg subexpressions. */
836 arglens = (int *) alloca (args * sizeof (int));
837 for (i = args - 1; i >= 0; i--)
838 {
839 oplen = length_of_subexp (inexpr, inend);
840 arglens[i] = oplen;
841 inend -= oplen;
842 }
843
844 /* Now copy each subexpression, preserving the order of
845 the subexpressions, but prefixifying each one.
846 In this loop, inend starts at the beginning of
847 the expression this level is working on
848 and marches forward over the arguments.
849 outbeg does similarly in the output. */
850 for (i = 0; i < args; i++)
851 {
852 oplen = arglens[i];
853 inend += oplen;
854 prefixify_subexp (inexpr, outexpr, inend, outbeg);
855 outbeg += oplen;
856 }
857}
858\f
859/* This page contains the two entry points to this file. */
860
861/* Read an expression from the string *STRINGPTR points to,
862 parse it, and return a pointer to a struct expression that we malloc.
863 Use block BLOCK as the lexical context for variable names;
864 if BLOCK is zero, use the block of the selected stack frame.
865 Meanwhile, advance *STRINGPTR to point after the expression,
866 at the first nonwhite character that is not part of the expression
867 (possibly a null character).
868
869 If COMMA is nonzero, stop if a comma is reached. */
870
871struct expression *
872parse_exp_1 (stringptr, block, comma)
873 char **stringptr;
874 struct block *block;
875 int comma;
876{
877 struct cleanup *old_chain;
878
879 lexptr = *stringptr;
880
881 paren_depth = 0;
882 type_stack_depth = 0;
883
884 comma_terminates = comma;
885
886 if (lexptr == 0 || *lexptr == 0)
887 error_no_arg ("expression to compute");
888
ad3b8c4a 889 old_chain = make_cleanup ((make_cleanup_func) free_funcalls, 0);
3d6b6a90
JG
890 funcall_chain = 0;
891
892 expression_context_block = block ? block : get_selected_block ();
893
894 namecopy = (char *) alloca (strlen (lexptr) + 1);
895 expout_size = 10;
896 expout_ptr = 0;
897 expout = (struct expression *)
81028ab0 898 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
3d6b6a90 899 expout->language_defn = current_language;
ad3b8c4a 900 make_cleanup ((make_cleanup_func) free_current_contents, &expout);
3d6b6a90
JG
901
902 if (current_language->la_parser ())
903 current_language->la_error (NULL);
904
905 discard_cleanups (old_chain);
54bbbfb4
FF
906
907 /* Record the actual number of expression elements, and then
908 reallocate the expression memory so that we free up any
909 excess elements. */
910
3d6b6a90
JG
911 expout->nelts = expout_ptr;
912 expout = (struct expression *)
1ab3bf1b 913 xrealloc ((char *) expout,
81028ab0 914 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
54bbbfb4
FF
915
916 /* Convert expression from postfix form as generated by yacc
917 parser, to a prefix form. */
918
433732f2
SG
919#ifdef MAINTENANCE_CMDS
920 if (expressiondebug)
921 dump_prefix_expression (expout, gdb_stdout,
922 "before conversion to prefix form");
923#endif /* MAINTENANCE_CMDS */
924
3d6b6a90 925 prefixify_expression (expout);
433732f2
SG
926
927#ifdef MAINTENANCE_CMDS
928 if (expressiondebug)
929 dump_postfix_expression (expout, gdb_stdout,
930 "after conversion to prefix form");
931#endif /* MAINTENANCE_CMDS */
54bbbfb4 932
3d6b6a90
JG
933 *stringptr = lexptr;
934 return expout;
935}
936
937/* Parse STRING as an expression, and complain if this fails
938 to use up all of the contents of STRING. */
939
940struct expression *
941parse_expression (string)
942 char *string;
943{
944 register struct expression *exp;
945 exp = parse_exp_1 (&string, 0, 0);
946 if (*string)
947 error ("Junk after end of expression.");
948 return exp;
949}
f843c95f
JK
950\f
951/* Stuff for maintaining a stack of types. Currently just used by C, but
952 probably useful for any language which declares its types "backwards". */
3d6b6a90
JG
953
954void
955push_type (tp)
956 enum type_pieces tp;
957{
958 if (type_stack_depth == type_stack_size)
959 {
960 type_stack_size *= 2;
961 type_stack = (union type_stack_elt *)
1ab3bf1b 962 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
3d6b6a90
JG
963 }
964 type_stack[type_stack_depth++].piece = tp;
965}
966
967void
968push_type_int (n)
969 int n;
970{
971 if (type_stack_depth == type_stack_size)
972 {
973 type_stack_size *= 2;
974 type_stack = (union type_stack_elt *)
1ab3bf1b 975 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
3d6b6a90
JG
976 }
977 type_stack[type_stack_depth++].int_val = n;
978}
979
980enum type_pieces
981pop_type ()
982{
983 if (type_stack_depth)
984 return type_stack[--type_stack_depth].piece;
985 return tp_end;
986}
987
988int
989pop_type_int ()
990{
991 if (type_stack_depth)
992 return type_stack[--type_stack_depth].int_val;
993 /* "Can't happen". */
994 return 0;
995}
996
f843c95f
JK
997/* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
998 as modified by all the stuff on the stack. */
999struct type *
1000follow_types (follow_type)
1001 struct type *follow_type;
1002{
1003 int done = 0;
1004 int array_size;
1005 struct type *range_type;
1006
1007 while (!done)
1008 switch (pop_type ())
1009 {
1010 case tp_end:
1011 done = 1;
1012 break;
1013 case tp_pointer:
1014 follow_type = lookup_pointer_type (follow_type);
1015 break;
1016 case tp_reference:
1017 follow_type = lookup_reference_type (follow_type);
1018 break;
1019 case tp_array:
1020 array_size = pop_type_int ();
36633dcc
JK
1021 /* FIXME-type-allocation: need a way to free this type when we are
1022 done with it. */
fda36387
PB
1023 range_type =
1024 create_range_type ((struct type *) NULL,
1025 builtin_type_int, 0,
1026 array_size >= 0 ? array_size - 1 : 0);
1027 follow_type =
1028 create_array_type ((struct type *) NULL,
1029 follow_type, range_type);
1030 if (array_size < 0)
1031 TYPE_ARRAY_UPPER_BOUND_TYPE(follow_type)
1032 = BOUND_CANNOT_BE_DETERMINED;
f843c95f
JK
1033 break;
1034 case tp_function:
36633dcc
JK
1035 /* FIXME-type-allocation: need a way to free this type when we are
1036 done with it. */
f843c95f
JK
1037 follow_type = lookup_function_type (follow_type);
1038 break;
1039 }
1040 return follow_type;
1041}
1042\f
3d6b6a90
JG
1043void
1044_initialize_parse ()
1045{
1046 type_stack_size = 80;
1047 type_stack_depth = 0;
1048 type_stack = (union type_stack_elt *)
1049 xmalloc (type_stack_size * sizeof (*type_stack));
3fb93d86
JK
1050
1051 msym_text_symbol_type =
eedb3363 1052 init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
3fb93d86
JK
1053 TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
1054 msym_data_symbol_type =
1055 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
eedb3363 1056 "<data variable, no debug info>", NULL);
3fb93d86 1057 msym_unknown_symbol_type =
eedb3363
JK
1058 init_type (TYPE_CODE_INT, 1, 0,
1059 "<variable (not text or data), no debug info>",
3fb93d86 1060 NULL);
433732f2
SG
1061
1062#ifdef MAINTENANCE_CMDS
1063 add_show_from_set (
1064 add_set_cmd ("expressiondebug", class_maintenance, var_zinteger,
1065 (char *)&expressiondebug,
1066 "Set expression debugging.\n\
1067When non-zero, the internal representation of expressions will be printed.",
1068 &setlist),
1069 &showlist);
1070#endif
3d6b6a90 1071}
This page took 0.386405 seconds and 4 git commands to generate.