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