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