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