| 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 | |
| 31 | #include "defs.h" |
| 32 | #include "symtab.h" |
| 33 | #include "gdbtypes.h" |
| 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 | |
| 41 | static void |
| 42 | free_funcalls PARAMS ((void)); |
| 43 | |
| 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 | |
| 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 | |
| 64 | /* Assign machine-independent names to certain registers |
| 65 | (unless overridden by the REGISTER_NAMES table) */ |
| 66 | |
| 67 | #ifdef NO_STD_REGS |
| 68 | unsigned num_std_regs = 0; |
| 69 | struct std_regs std_regs[1]; |
| 70 | #else |
| 71 | struct std_regs std_regs[] = { |
| 72 | |
| 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 |
| 85 | |
| 86 | }; |
| 87 | |
| 88 | unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]); |
| 89 | |
| 90 | #endif |
| 91 | |
| 92 | |
| 93 | /* Begin counting arguments for a function call, |
| 94 | saving the data about any containing call. */ |
| 95 | |
| 96 | void |
| 97 | start_arglist () |
| 98 | { |
| 99 | register struct funcall *new; |
| 100 | |
| 101 | new = (struct funcall *) xmalloc (sizeof (struct funcall)); |
| 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; |
| 118 | free ((PTR)call); |
| 119 | return val; |
| 120 | } |
| 121 | |
| 122 | /* Free everything in the funcall chain. |
| 123 | Used when there is an error inside parsing. */ |
| 124 | |
| 125 | static void |
| 126 | free_funcalls () |
| 127 | { |
| 128 | register struct funcall *call, *next; |
| 129 | |
| 130 | for (call = funcall_chain; call; call = next) |
| 131 | { |
| 132 | next = call->next; |
| 133 | free ((PTR)call); |
| 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; |
| 152 | expout = (struct expression *) |
| 153 | xrealloc ((char *) expout, sizeof (struct expression) |
| 154 | + EXP_ELEM_TO_BYTES (expout_size)); |
| 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 | |
| 181 | void |
| 182 | write_exp_elt_longcst (expelt) |
| 183 | LONGEST expelt; |
| 184 | { |
| 185 | union exp_element tmp; |
| 186 | |
| 187 | tmp.longconst = expelt; |
| 188 | |
| 189 | write_exp_elt (tmp); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | write_exp_elt_dblcst (expelt) |
| 194 | double expelt; |
| 195 | { |
| 196 | union exp_element tmp; |
| 197 | |
| 198 | tmp.doubleconst = expelt; |
| 199 | |
| 200 | write_exp_elt (tmp); |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | write_exp_elt_type (expelt) |
| 205 | struct type *expelt; |
| 206 | { |
| 207 | union exp_element tmp; |
| 208 | |
| 209 | tmp.type = expelt; |
| 210 | |
| 211 | write_exp_elt (tmp); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | write_exp_elt_intern (expelt) |
| 216 | struct internalvar *expelt; |
| 217 | { |
| 218 | union exp_element tmp; |
| 219 | |
| 220 | tmp.internalvar = expelt; |
| 221 | |
| 222 | write_exp_elt (tmp); |
| 223 | } |
| 224 | |
| 225 | /* Add a string constant to the end of the expression. |
| 226 | |
| 227 | String constants are stored by first writing an expression element |
| 228 | that contains the length of the string, then stuffing the string |
| 229 | constant itself into however many expression elements are needed |
| 230 | to hold it, and then writing another expression element that contains |
| 231 | the length of the string. I.E. an expression element at each end of |
| 232 | the string records the string length, so you can skip over the |
| 233 | expression elements containing the actual string bytes from either |
| 234 | end of the string. Note that this also allows gdb to handle |
| 235 | strings with embedded null bytes, as is required for some languages. |
| 236 | |
| 237 | Don't be fooled by the fact that the string is null byte terminated, |
| 238 | this is strictly for the convenience of debugging gdb itself. Gdb |
| 239 | Gdb does not depend up the string being null terminated, since the |
| 240 | actual length is recorded in expression elements at each end of the |
| 241 | string. The null byte is taken into consideration when computing how |
| 242 | many expression elements are required to hold the string constant, of |
| 243 | course. */ |
| 244 | |
| 245 | |
| 246 | void |
| 247 | write_exp_string (str) |
| 248 | struct stoken str; |
| 249 | { |
| 250 | register int len = str.length; |
| 251 | register int lenelt; |
| 252 | register char *strdata; |
| 253 | |
| 254 | /* Compute the number of expression elements required to hold the string |
| 255 | (including a null byte terminator), along with one expression element |
| 256 | at each end to record the actual string length (not including the |
| 257 | null byte terminator). */ |
| 258 | |
| 259 | lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1); |
| 260 | |
| 261 | /* Ensure that we have enough available expression elements to store |
| 262 | everything. */ |
| 263 | |
| 264 | if ((expout_ptr + lenelt) >= expout_size) |
| 265 | { |
| 266 | expout_size = max (expout_size * 2, expout_ptr + lenelt + 10); |
| 267 | expout = (struct expression *) |
| 268 | xrealloc ((char *) expout, (sizeof (struct expression) |
| 269 | + EXP_ELEM_TO_BYTES (expout_size))); |
| 270 | } |
| 271 | |
| 272 | /* Write the leading length expression element (which advances the current |
| 273 | expression element index), then write the string constant followed by a |
| 274 | terminating null byte, and then write the trailing length expression |
| 275 | element. */ |
| 276 | |
| 277 | write_exp_elt_longcst ((LONGEST) len); |
| 278 | strdata = (char *) &expout->elts[expout_ptr]; |
| 279 | memcpy (strdata, str.ptr, len); |
| 280 | *(strdata + len) = '\0'; |
| 281 | expout_ptr += lenelt - 2; |
| 282 | write_exp_elt_longcst ((LONGEST) len); |
| 283 | } |
| 284 | |
| 285 | /* Add a bitstring constant to the end of the expression. |
| 286 | |
| 287 | Bitstring constants are stored by first writing an expression element |
| 288 | that contains the length of the bitstring (in bits), then stuffing the |
| 289 | bitstring constant itself into however many expression elements are |
| 290 | needed to hold it, and then writing another expression element that |
| 291 | contains the length of the bitstring. I.E. an expression element at |
| 292 | each end of the bitstring records the bitstring length, so you can skip |
| 293 | over the expression elements containing the actual bitstring bytes from |
| 294 | either end of the bitstring. */ |
| 295 | |
| 296 | void |
| 297 | write_exp_bitstring (str) |
| 298 | struct stoken str; |
| 299 | { |
| 300 | register int bits = str.length; /* length in bits */ |
| 301 | register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT; |
| 302 | register int lenelt; |
| 303 | register char *strdata; |
| 304 | |
| 305 | /* Compute the number of expression elements required to hold the bitstring, |
| 306 | along with one expression element at each end to record the actual |
| 307 | bitstring length in bits. */ |
| 308 | |
| 309 | lenelt = 2 + BYTES_TO_EXP_ELEM (len); |
| 310 | |
| 311 | /* Ensure that we have enough available expression elements to store |
| 312 | everything. */ |
| 313 | |
| 314 | if ((expout_ptr + lenelt) >= expout_size) |
| 315 | { |
| 316 | expout_size = max (expout_size * 2, expout_ptr + lenelt + 10); |
| 317 | expout = (struct expression *) |
| 318 | xrealloc ((char *) expout, (sizeof (struct expression) |
| 319 | + EXP_ELEM_TO_BYTES (expout_size))); |
| 320 | } |
| 321 | |
| 322 | /* Write the leading length expression element (which advances the current |
| 323 | expression element index), then write the bitstring constant, and then |
| 324 | write the trailing length expression element. */ |
| 325 | |
| 326 | write_exp_elt_longcst ((LONGEST) bits); |
| 327 | strdata = (char *) &expout->elts[expout_ptr]; |
| 328 | memcpy (strdata, str.ptr, len); |
| 329 | expout_ptr += lenelt - 2; |
| 330 | write_exp_elt_longcst ((LONGEST) bits); |
| 331 | } |
| 332 | \f |
| 333 | /* Return a null-terminated temporary copy of the name |
| 334 | of a string token. */ |
| 335 | |
| 336 | char * |
| 337 | copy_name (token) |
| 338 | struct stoken token; |
| 339 | { |
| 340 | memcpy (namecopy, token.ptr, token.length); |
| 341 | namecopy[token.length] = 0; |
| 342 | return namecopy; |
| 343 | } |
| 344 | \f |
| 345 | /* Reverse an expression from suffix form (in which it is constructed) |
| 346 | to prefix form (in which we can conveniently print or execute it). */ |
| 347 | |
| 348 | static void |
| 349 | prefixify_expression (expr) |
| 350 | register struct expression *expr; |
| 351 | { |
| 352 | register int len = |
| 353 | sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts); |
| 354 | register struct expression *temp; |
| 355 | register int inpos = expr->nelts, outpos = 0; |
| 356 | |
| 357 | temp = (struct expression *) alloca (len); |
| 358 | |
| 359 | /* Copy the original expression into temp. */ |
| 360 | memcpy (temp, expr, len); |
| 361 | |
| 362 | prefixify_subexp (temp, expr, inpos, outpos); |
| 363 | } |
| 364 | |
| 365 | /* Return the number of exp_elements in the subexpression of EXPR |
| 366 | whose last exp_element is at index ENDPOS - 1 in EXPR. */ |
| 367 | |
| 368 | static int |
| 369 | length_of_subexp (expr, endpos) |
| 370 | register struct expression *expr; |
| 371 | register int endpos; |
| 372 | { |
| 373 | register int oplen = 1; |
| 374 | register int args = 0; |
| 375 | register int i; |
| 376 | |
| 377 | if (endpos < 1) |
| 378 | error ("?error in length_of_subexp"); |
| 379 | |
| 380 | i = (int) expr->elts[endpos - 1].opcode; |
| 381 | |
| 382 | switch (i) |
| 383 | { |
| 384 | /* C++ */ |
| 385 | case OP_SCOPE: |
| 386 | oplen = longest_to_int (expr->elts[endpos - 2].longconst); |
| 387 | oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1); |
| 388 | break; |
| 389 | |
| 390 | case OP_LONG: |
| 391 | case OP_DOUBLE: |
| 392 | oplen = 4; |
| 393 | break; |
| 394 | |
| 395 | case OP_TYPE: |
| 396 | case OP_BOOL: |
| 397 | case OP_VAR_VALUE: |
| 398 | case OP_LAST: |
| 399 | case OP_REGISTER: |
| 400 | case OP_INTERNALVAR: |
| 401 | oplen = 3; |
| 402 | break; |
| 403 | |
| 404 | case OP_FUNCALL: |
| 405 | oplen = 3; |
| 406 | args = 1 + longest_to_int (expr->elts[endpos - 2].longconst); |
| 407 | break; |
| 408 | |
| 409 | case UNOP_MAX: |
| 410 | case UNOP_MIN: |
| 411 | oplen = 3; |
| 412 | break; |
| 413 | |
| 414 | case BINOP_VAL: |
| 415 | case UNOP_CAST: |
| 416 | case UNOP_MEMVAL: |
| 417 | oplen = 3; |
| 418 | args = 1; |
| 419 | break; |
| 420 | |
| 421 | case UNOP_ABS: |
| 422 | case UNOP_CAP: |
| 423 | case UNOP_CHR: |
| 424 | case UNOP_FLOAT: |
| 425 | case UNOP_HIGH: |
| 426 | case UNOP_ODD: |
| 427 | case UNOP_ORD: |
| 428 | case UNOP_TRUNC: |
| 429 | oplen = 1; |
| 430 | args = 1; |
| 431 | break; |
| 432 | |
| 433 | case STRUCTOP_STRUCT: |
| 434 | case STRUCTOP_PTR: |
| 435 | args = 1; |
| 436 | /* fall through */ |
| 437 | case OP_M2_STRING: |
| 438 | case OP_STRING: |
| 439 | oplen = longest_to_int (expr->elts[endpos - 2].longconst); |
| 440 | oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1); |
| 441 | break; |
| 442 | |
| 443 | case OP_BITSTRING: |
| 444 | oplen = longest_to_int (expr->elts[endpos - 2].longconst); |
| 445 | oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT; |
| 446 | oplen = 4 + BYTES_TO_EXP_ELEM (oplen); |
| 447 | break; |
| 448 | |
| 449 | case OP_ARRAY: |
| 450 | oplen = 4; |
| 451 | args = longest_to_int (expr->elts[endpos - 2].longconst); |
| 452 | args -= longest_to_int (expr->elts[endpos - 3].longconst); |
| 453 | args += 1; |
| 454 | break; |
| 455 | |
| 456 | case TERNOP_COND: |
| 457 | args = 3; |
| 458 | break; |
| 459 | |
| 460 | /* Modula-2 */ |
| 461 | case MULTI_SUBSCRIPT: |
| 462 | oplen=3; |
| 463 | args = 1 + longest_to_int (expr->elts[endpos- 2].longconst); |
| 464 | break; |
| 465 | |
| 466 | case BINOP_ASSIGN_MODIFY: |
| 467 | oplen = 3; |
| 468 | args = 2; |
| 469 | break; |
| 470 | |
| 471 | /* C++ */ |
| 472 | case OP_THIS: |
| 473 | oplen = 2; |
| 474 | break; |
| 475 | |
| 476 | default: |
| 477 | args = 1 + (i < (int) BINOP_END); |
| 478 | } |
| 479 | |
| 480 | while (args > 0) |
| 481 | { |
| 482 | oplen += length_of_subexp (expr, endpos - oplen); |
| 483 | args--; |
| 484 | } |
| 485 | |
| 486 | return oplen; |
| 487 | } |
| 488 | |
| 489 | /* Copy the subexpression ending just before index INEND in INEXPR |
| 490 | into OUTEXPR, starting at index OUTBEG. |
| 491 | In the process, convert it from suffix to prefix form. */ |
| 492 | |
| 493 | static void |
| 494 | prefixify_subexp (inexpr, outexpr, inend, outbeg) |
| 495 | register struct expression *inexpr; |
| 496 | struct expression *outexpr; |
| 497 | register int inend; |
| 498 | int outbeg; |
| 499 | { |
| 500 | register int oplen = 1; |
| 501 | register int args = 0; |
| 502 | register int i; |
| 503 | int *arglens; |
| 504 | enum exp_opcode opcode; |
| 505 | |
| 506 | /* Compute how long the last operation is (in OPLEN), |
| 507 | and also how many preceding subexpressions serve as |
| 508 | arguments for it (in ARGS). */ |
| 509 | |
| 510 | opcode = inexpr->elts[inend - 1].opcode; |
| 511 | switch (opcode) |
| 512 | { |
| 513 | /* C++ */ |
| 514 | case OP_SCOPE: |
| 515 | oplen = longest_to_int (inexpr->elts[inend - 2].longconst); |
| 516 | oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1); |
| 517 | break; |
| 518 | |
| 519 | case OP_LONG: |
| 520 | case OP_DOUBLE: |
| 521 | oplen = 4; |
| 522 | break; |
| 523 | |
| 524 | case OP_TYPE: |
| 525 | case OP_BOOL: |
| 526 | case OP_VAR_VALUE: |
| 527 | case OP_LAST: |
| 528 | case OP_REGISTER: |
| 529 | case OP_INTERNALVAR: |
| 530 | oplen = 3; |
| 531 | break; |
| 532 | |
| 533 | case OP_FUNCALL: |
| 534 | oplen = 3; |
| 535 | args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst); |
| 536 | break; |
| 537 | |
| 538 | case UNOP_MIN: |
| 539 | case UNOP_MAX: |
| 540 | oplen = 3; |
| 541 | break; |
| 542 | |
| 543 | case UNOP_CAST: |
| 544 | case UNOP_MEMVAL: |
| 545 | oplen = 3; |
| 546 | args = 1; |
| 547 | break; |
| 548 | |
| 549 | case UNOP_ABS: |
| 550 | case UNOP_CAP: |
| 551 | case UNOP_CHR: |
| 552 | case UNOP_FLOAT: |
| 553 | case UNOP_HIGH: |
| 554 | case UNOP_ODD: |
| 555 | case UNOP_ORD: |
| 556 | case UNOP_TRUNC: |
| 557 | oplen=1; |
| 558 | args=1; |
| 559 | break; |
| 560 | |
| 561 | case STRUCTOP_STRUCT: |
| 562 | case STRUCTOP_PTR: |
| 563 | args = 1; |
| 564 | /* fall through */ |
| 565 | case OP_M2_STRING: |
| 566 | case OP_STRING: |
| 567 | oplen = longest_to_int (inexpr->elts[inend - 2].longconst); |
| 568 | oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1); |
| 569 | break; |
| 570 | |
| 571 | case OP_BITSTRING: |
| 572 | oplen = longest_to_int (inexpr->elts[inend - 2].longconst); |
| 573 | oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT; |
| 574 | oplen = 4 + BYTES_TO_EXP_ELEM (oplen); |
| 575 | break; |
| 576 | |
| 577 | case OP_ARRAY: |
| 578 | oplen = 4; |
| 579 | args = longest_to_int (inexpr->elts[inend - 2].longconst); |
| 580 | args -= longest_to_int (inexpr->elts[inend - 3].longconst); |
| 581 | args += 1; |
| 582 | break; |
| 583 | |
| 584 | case TERNOP_COND: |
| 585 | args = 3; |
| 586 | break; |
| 587 | |
| 588 | case BINOP_ASSIGN_MODIFY: |
| 589 | oplen = 3; |
| 590 | args = 2; |
| 591 | break; |
| 592 | |
| 593 | /* Modula-2 */ |
| 594 | case MULTI_SUBSCRIPT: |
| 595 | oplen=3; |
| 596 | args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst); |
| 597 | break; |
| 598 | |
| 599 | /* C++ */ |
| 600 | case OP_THIS: |
| 601 | oplen = 2; |
| 602 | break; |
| 603 | |
| 604 | default: |
| 605 | args = 1 + ((int) opcode < (int) BINOP_END); |
| 606 | } |
| 607 | |
| 608 | /* Copy the final operator itself, from the end of the input |
| 609 | to the beginning of the output. */ |
| 610 | inend -= oplen; |
| 611 | memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend], |
| 612 | EXP_ELEM_TO_BYTES (oplen)); |
| 613 | outbeg += oplen; |
| 614 | |
| 615 | /* Find the lengths of the arg subexpressions. */ |
| 616 | arglens = (int *) alloca (args * sizeof (int)); |
| 617 | for (i = args - 1; i >= 0; i--) |
| 618 | { |
| 619 | oplen = length_of_subexp (inexpr, inend); |
| 620 | arglens[i] = oplen; |
| 621 | inend -= oplen; |
| 622 | } |
| 623 | |
| 624 | /* Now copy each subexpression, preserving the order of |
| 625 | the subexpressions, but prefixifying each one. |
| 626 | In this loop, inend starts at the beginning of |
| 627 | the expression this level is working on |
| 628 | and marches forward over the arguments. |
| 629 | outbeg does similarly in the output. */ |
| 630 | for (i = 0; i < args; i++) |
| 631 | { |
| 632 | oplen = arglens[i]; |
| 633 | inend += oplen; |
| 634 | prefixify_subexp (inexpr, outexpr, inend, outbeg); |
| 635 | outbeg += oplen; |
| 636 | } |
| 637 | } |
| 638 | \f |
| 639 | /* This page contains the two entry points to this file. */ |
| 640 | |
| 641 | /* Read an expression from the string *STRINGPTR points to, |
| 642 | parse it, and return a pointer to a struct expression that we malloc. |
| 643 | Use block BLOCK as the lexical context for variable names; |
| 644 | if BLOCK is zero, use the block of the selected stack frame. |
| 645 | Meanwhile, advance *STRINGPTR to point after the expression, |
| 646 | at the first nonwhite character that is not part of the expression |
| 647 | (possibly a null character). |
| 648 | |
| 649 | If COMMA is nonzero, stop if a comma is reached. */ |
| 650 | |
| 651 | struct expression * |
| 652 | parse_exp_1 (stringptr, block, comma) |
| 653 | char **stringptr; |
| 654 | struct block *block; |
| 655 | int comma; |
| 656 | { |
| 657 | struct cleanup *old_chain; |
| 658 | |
| 659 | lexptr = *stringptr; |
| 660 | |
| 661 | paren_depth = 0; |
| 662 | type_stack_depth = 0; |
| 663 | |
| 664 | comma_terminates = comma; |
| 665 | |
| 666 | if (lexptr == 0 || *lexptr == 0) |
| 667 | error_no_arg ("expression to compute"); |
| 668 | |
| 669 | old_chain = make_cleanup (free_funcalls, 0); |
| 670 | funcall_chain = 0; |
| 671 | |
| 672 | expression_context_block = block ? block : get_selected_block (); |
| 673 | |
| 674 | namecopy = (char *) alloca (strlen (lexptr) + 1); |
| 675 | expout_size = 10; |
| 676 | expout_ptr = 0; |
| 677 | expout = (struct expression *) |
| 678 | xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size)); |
| 679 | expout->language_defn = current_language; |
| 680 | make_cleanup (free_current_contents, &expout); |
| 681 | |
| 682 | if (current_language->la_parser ()) |
| 683 | current_language->la_error (NULL); |
| 684 | |
| 685 | discard_cleanups (old_chain); |
| 686 | |
| 687 | /* Record the actual number of expression elements, and then |
| 688 | reallocate the expression memory so that we free up any |
| 689 | excess elements. */ |
| 690 | |
| 691 | expout->nelts = expout_ptr; |
| 692 | expout = (struct expression *) |
| 693 | xrealloc ((char *) expout, |
| 694 | sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));; |
| 695 | |
| 696 | /* Convert expression from postfix form as generated by yacc |
| 697 | parser, to a prefix form. */ |
| 698 | |
| 699 | DUMP_EXPRESSION (expout, stdout, "before conversion to prefix form"); |
| 700 | prefixify_expression (expout); |
| 701 | DUMP_EXPRESSION (expout, stdout, "after conversion to prefix form"); |
| 702 | |
| 703 | *stringptr = lexptr; |
| 704 | return expout; |
| 705 | } |
| 706 | |
| 707 | /* Parse STRING as an expression, and complain if this fails |
| 708 | to use up all of the contents of STRING. */ |
| 709 | |
| 710 | struct expression * |
| 711 | parse_expression (string) |
| 712 | char *string; |
| 713 | { |
| 714 | register struct expression *exp; |
| 715 | exp = parse_exp_1 (&string, 0, 0); |
| 716 | if (*string) |
| 717 | error ("Junk after end of expression."); |
| 718 | return exp; |
| 719 | } |
| 720 | |
| 721 | void |
| 722 | push_type (tp) |
| 723 | enum type_pieces tp; |
| 724 | { |
| 725 | if (type_stack_depth == type_stack_size) |
| 726 | { |
| 727 | type_stack_size *= 2; |
| 728 | type_stack = (union type_stack_elt *) |
| 729 | xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack)); |
| 730 | } |
| 731 | type_stack[type_stack_depth++].piece = tp; |
| 732 | } |
| 733 | |
| 734 | void |
| 735 | push_type_int (n) |
| 736 | int n; |
| 737 | { |
| 738 | if (type_stack_depth == type_stack_size) |
| 739 | { |
| 740 | type_stack_size *= 2; |
| 741 | type_stack = (union type_stack_elt *) |
| 742 | xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack)); |
| 743 | } |
| 744 | type_stack[type_stack_depth++].int_val = n; |
| 745 | } |
| 746 | |
| 747 | enum type_pieces |
| 748 | pop_type () |
| 749 | { |
| 750 | if (type_stack_depth) |
| 751 | return type_stack[--type_stack_depth].piece; |
| 752 | return tp_end; |
| 753 | } |
| 754 | |
| 755 | int |
| 756 | pop_type_int () |
| 757 | { |
| 758 | if (type_stack_depth) |
| 759 | return type_stack[--type_stack_depth].int_val; |
| 760 | /* "Can't happen". */ |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | void |
| 765 | _initialize_parse () |
| 766 | { |
| 767 | type_stack_size = 80; |
| 768 | type_stack_depth = 0; |
| 769 | type_stack = (union type_stack_elt *) |
| 770 | xmalloc (type_stack_size * sizeof (*type_stack)); |
| 771 | } |