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