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