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