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