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