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