1cb62177a0e8c5e2f239fd7d548718fc94292c77
[platform/upstream/binutils.git] / gdb / parse.c
1 /* Parse expressions for GDB.
2    Copyright (C) 1986, 1989, 1990, 1991, 1994 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "gdb_string.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
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"
41 #include "symfile.h"    /* for overlay functions */
42 \f
43 /* Global variables declared in parser-defs.h (and commented there).  */
44 struct expression *expout;
45 int expout_size;
46 int expout_ptr;
47 struct block *expression_context_block;
48 struct block *innermost_block;
49 int arglist_len;
50 union type_stack_elt *type_stack;
51 int type_stack_depth, type_stack_size;
52 char *lexptr;
53 char *namecopy;
54 int paren_depth;
55 int comma_terminates;
56 \f
57 static void
58 free_funcalls PARAMS ((void));
59
60 static void
61 prefixify_expression PARAMS ((struct expression *));
62
63 static void
64 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
65
66 /* Data structure for saving values of arglist_len for function calls whose
67    arguments contain other function calls.  */
68
69 struct funcall
70   {
71     struct funcall *next;
72     int arglist_len;
73   };
74
75 static struct funcall *funcall_chain;
76
77 /* Assign machine-independent names to certain registers 
78    (unless overridden by the REGISTER_NAMES table) */
79
80 #ifdef NO_STD_REGS
81 unsigned num_std_regs = 0;
82 struct std_regs std_regs[1];
83 #else
84 struct std_regs std_regs[] = {
85
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
98
99 };
100
101 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
102
103 #endif
104
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
109 int
110 target_map_name_to_register (str, len)
111      char *str;
112      int len;
113 {
114   int i;
115
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. */
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
141   return -1;
142 }
143
144 /* Begin counting arguments for a function call,
145    saving the data about any containing call.  */
146
147 void
148 start_arglist ()
149 {
150   register struct funcall *new;
151
152   new = (struct funcall *) xmalloc (sizeof (struct funcall));
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
162 int
163 end_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;
169   free ((PTR)call);
170   return val;
171 }
172
173 /* Free everything in the funcall chain.
174    Used when there is an error inside parsing.  */
175
176 static void
177 free_funcalls ()
178 {
179   register struct funcall *call, *next;
180
181   for (call = funcall_chain; call; call = next)
182     {
183       next = call->next;
184       free ((PTR)call);
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
196 void
197 write_exp_elt (expelt)
198      union exp_element expelt;
199 {
200   if (expout_ptr >= expout_size)
201     {
202       expout_size *= 2;
203       expout = (struct expression *)
204         xrealloc ((char *) expout, sizeof (struct expression)
205                   + EXP_ELEM_TO_BYTES (expout_size));
206     }
207   expout->elts[expout_ptr++] = expelt;
208 }
209
210 void
211 write_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
221 void
222 write_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
232 void
233 write_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
241 void
242 write_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
252 void
253 write_exp_elt_dblcst (expelt)
254      DOUBLEST expelt;
255 {
256   union exp_element tmp;
257
258   tmp.doubleconst = expelt;
259
260   write_exp_elt (tmp);
261 }
262
263 void
264 write_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
274 void
275 write_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.
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
305
306 void
307 write_exp_string (str)
308      struct stoken str;
309 {
310   register int len = str.length;
311   register int lenelt;
312   register char *strdata;
313
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). */
318
319   lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
320
321   /* Ensure that we have enough available expression elements to store
322      everything. */
323
324   if ((expout_ptr + lenelt) >= expout_size)
325     {
326       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
327       expout = (struct expression *)
328         xrealloc ((char *) expout, (sizeof (struct expression)
329                                     + EXP_ELEM_TO_BYTES (expout_size)));
330     }
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;
342   write_exp_elt_longcst ((LONGEST) len);
343 }
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
356 void
357 write_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 }
392
393 /* Add the appropriate elements for a minimal symbol to the end of
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
400 static struct type *msym_text_symbol_type;
401 static struct type *msym_data_symbol_type;
402 static struct type *msym_unknown_symbol_type;
403
404 void
405 write_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 {
410   CORE_ADDR addr;
411
412   write_exp_elt_opcode (OP_LONG);
413   write_exp_elt_type (lookup_pointer_type (builtin_type_void));
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                                                 
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:
427     case mst_solib_trampoline:
428       write_exp_elt_type (msym_text_symbol_type);
429       break;
430
431     case mst_data:
432     case mst_file_data:
433     case mst_bss:
434     case mst_file_bss:
435       write_exp_elt_type (msym_data_symbol_type);
436       break;
437
438     default:
439       write_exp_elt_type (msym_unknown_symbol_type);
440       break;
441     }
442   write_exp_elt_opcode (UNOP_MEMVAL);
443 }
444 \f
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
468 void
469 write_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.  */
504   i = target_map_name_to_register( str.ptr + 1, str.length - 1 );
505   if( i >= 0 )
506     goto handle_register;
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
526 /* Return a null-terminated temporary copy of the name
527    of a string token.  */
528
529 char *
530 copy_name (token)
531      struct stoken token;
532 {
533   memcpy (namecopy, token.ptr, token.length);
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
541 static void
542 prefixify_expression (expr)
543      register struct expression *expr;
544 {
545   register int len =
546     sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
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.  */
553   memcpy (temp, expr, len);
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
561 int
562 length_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
570   if (endpos < 1)
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:
579       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
580       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
581       break;
582
583     case OP_LONG:
584     case OP_DOUBLE:
585     case OP_VAR_VALUE:
586       oplen = 4;
587       break;
588
589     case OP_TYPE:
590     case OP_BOOL:
591     case OP_LAST:
592     case OP_REGISTER:
593     case OP_INTERNALVAR:
594       oplen = 3;
595       break;
596
597     case OP_COMPLEX:
598       oplen = 1; 
599       args = 2;
600       break; 
601
602     case OP_FUNCALL:
603     case OP_F77_UNDETERMINED_ARGLIST:
604       oplen = 3;
605       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
606       break;
607
608     case UNOP_MAX:
609     case UNOP_MIN:
610       oplen = 3;
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
632     case OP_LABELED:
633     case STRUCTOP_STRUCT:
634     case STRUCTOP_PTR:
635       args = 1;
636       /* fall through */
637     case OP_M2_STRING:
638     case OP_STRING:
639     case OP_NAME:
640     case OP_EXPRSTRING:
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);
649       break;
650
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
658     case TERNOP_COND:
659     case TERNOP_SLICE:
660     case TERNOP_SLICE_COUNT:
661       args = 3;
662       break;
663
664       /* Modula-2 */
665    case MULTI_SUBSCRIPT:
666       oplen = 3;
667       args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
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
697 static void
698 prefixify_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:
719       oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
720       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
721       break;
722
723     case OP_LONG:
724     case OP_DOUBLE:
725     case OP_VAR_VALUE:
726       oplen = 4;
727       break;
728
729     case OP_TYPE:
730     case OP_BOOL:
731     case OP_LAST:
732     case OP_REGISTER:
733     case OP_INTERNALVAR:
734       oplen = 3;
735       break;
736
737     case OP_COMPLEX:
738       oplen = 1; 
739       args = 2; 
740       break; 
741
742     case OP_FUNCALL:
743     case OP_F77_UNDETERMINED_ARGLIST:
744       oplen = 3;
745       args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
746       break;
747
748     case UNOP_MIN:
749     case UNOP_MAX:
750       oplen = 3;
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
771     case STRUCTOP_STRUCT:
772     case STRUCTOP_PTR:
773     case OP_LABELED:
774       args = 1;
775       /* fall through */
776     case OP_M2_STRING:
777     case OP_STRING:
778     case OP_NAME:
779     case OP_EXPRSTRING:
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);
788       break;
789
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
797     case TERNOP_COND:
798     case TERNOP_SLICE:
799     case TERNOP_SLICE_COUNT:
800       args = 3;
801       break;
802
803     case BINOP_ASSIGN_MODIFY:
804       oplen = 3;
805       args = 2;
806       break;
807
808       /* Modula-2 */
809    case MULTI_SUBSCRIPT:
810       oplen = 3;
811       args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
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;
826   memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
827           EXP_ELEM_TO_BYTES (oplen));
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
866 struct expression *
867 parse_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 *)
893     xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
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);
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
906   expout->nelts = expout_ptr;
907   expout = (struct expression *)
908     xrealloc ((char *) expout,
909               sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
910
911   /* Convert expression from postfix form as generated by yacc
912      parser, to a prefix form. */
913
914   DUMP_EXPRESSION (expout, gdb_stdout, "before conversion to prefix form");
915   prefixify_expression (expout);
916   DUMP_EXPRESSION (expout, gdb_stdout, "after conversion to prefix form");
917
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
925 struct expression *
926 parse_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 }
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".  */
938
939 void 
940 push_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 *)
947         xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
948     }
949   type_stack[type_stack_depth++].piece = tp;
950 }
951
952 void
953 push_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 *)
960         xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
961     }
962   type_stack[type_stack_depth++].int_val = n;
963 }
964
965 enum type_pieces 
966 pop_type ()
967 {
968   if (type_stack_depth)
969     return type_stack[--type_stack_depth].piece;
970   return tp_end;
971 }
972
973 int
974 pop_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
982 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
983    as modified by all the stuff on the stack.  */
984 struct type *
985 follow_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 ();
1006         /* FIXME-type-allocation: need a way to free this type when we are
1007            done with it.  */
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;
1018         break;
1019       case tp_function:
1020         /* FIXME-type-allocation: need a way to free this type when we are
1021            done with it.  */
1022         follow_type = lookup_function_type (follow_type);
1023         break;
1024       }
1025   return follow_type;
1026 }
1027 \f
1028 void
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));
1035
1036   msym_text_symbol_type =
1037     init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
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,
1041                "<data variable, no debug info>", NULL);
1042   msym_unknown_symbol_type =
1043     init_type (TYPE_CODE_INT, 1, 0,
1044                "<variable (not text or data), no debug info>",
1045                NULL);
1046 }