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