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