| 1 | /* GDB-specific functions for operating on agent expressions. |
| 2 | |
| 3 | Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, |
| 4 | Inc. |
| 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., 51 Franklin Street, Fifth Floor, |
| 21 | Boston, MA 02110-1301, USA. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "symtab.h" |
| 25 | #include "symfile.h" |
| 26 | #include "gdbtypes.h" |
| 27 | #include "value.h" |
| 28 | #include "expression.h" |
| 29 | #include "command.h" |
| 30 | #include "gdbcmd.h" |
| 31 | #include "frame.h" |
| 32 | #include "target.h" |
| 33 | #include "ax.h" |
| 34 | #include "ax-gdb.h" |
| 35 | #include "gdb_string.h" |
| 36 | #include "block.h" |
| 37 | #include "regcache.h" |
| 38 | |
| 39 | /* To make sense of this file, you should read doc/agentexpr.texi. |
| 40 | Then look at the types and enums in ax-gdb.h. For the code itself, |
| 41 | look at gen_expr, towards the bottom; that's the main function that |
| 42 | looks at the GDB expressions and calls everything else to generate |
| 43 | code. |
| 44 | |
| 45 | I'm beginning to wonder whether it wouldn't be nicer to internally |
| 46 | generate trees, with types, and then spit out the bytecode in |
| 47 | linear form afterwards; we could generate fewer `swap', `ext', and |
| 48 | `zero_ext' bytecodes that way; it would make good constant folding |
| 49 | easier, too. But at the moment, I think we should be willing to |
| 50 | pay for the simplicity of this code with less-than-optimal bytecode |
| 51 | strings. |
| 52 | |
| 53 | Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */ |
| 54 | \f |
| 55 | |
| 56 | |
| 57 | /* Prototypes for local functions. */ |
| 58 | |
| 59 | /* There's a standard order to the arguments of these functions: |
| 60 | union exp_element ** --- pointer into expression |
| 61 | struct agent_expr * --- agent expression buffer to generate code into |
| 62 | struct axs_value * --- describes value left on top of stack */ |
| 63 | |
| 64 | static struct value *const_var_ref (struct symbol *var); |
| 65 | static struct value *const_expr (union exp_element **pc); |
| 66 | static struct value *maybe_const_expr (union exp_element **pc); |
| 67 | |
| 68 | static void gen_traced_pop (struct agent_expr *, struct axs_value *); |
| 69 | |
| 70 | static void gen_sign_extend (struct agent_expr *, struct type *); |
| 71 | static void gen_extend (struct agent_expr *, struct type *); |
| 72 | static void gen_fetch (struct agent_expr *, struct type *); |
| 73 | static void gen_left_shift (struct agent_expr *, int); |
| 74 | |
| 75 | |
| 76 | static void gen_frame_args_address (struct agent_expr *); |
| 77 | static void gen_frame_locals_address (struct agent_expr *); |
| 78 | static void gen_offset (struct agent_expr *ax, int offset); |
| 79 | static void gen_sym_offset (struct agent_expr *, struct symbol *); |
| 80 | static void gen_var_ref (struct agent_expr *ax, |
| 81 | struct axs_value *value, struct symbol *var); |
| 82 | |
| 83 | |
| 84 | static void gen_int_literal (struct agent_expr *ax, |
| 85 | struct axs_value *value, |
| 86 | LONGEST k, struct type *type); |
| 87 | |
| 88 | |
| 89 | static void require_rvalue (struct agent_expr *ax, struct axs_value *value); |
| 90 | static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value); |
| 91 | static int type_wider_than (struct type *type1, struct type *type2); |
| 92 | static struct type *max_type (struct type *type1, struct type *type2); |
| 93 | static void gen_conversion (struct agent_expr *ax, |
| 94 | struct type *from, struct type *to); |
| 95 | static int is_nontrivial_conversion (struct type *from, struct type *to); |
| 96 | static void gen_usual_arithmetic (struct agent_expr *ax, |
| 97 | struct axs_value *value1, |
| 98 | struct axs_value *value2); |
| 99 | static void gen_integral_promotions (struct agent_expr *ax, |
| 100 | struct axs_value *value); |
| 101 | static void gen_cast (struct agent_expr *ax, |
| 102 | struct axs_value *value, struct type *type); |
| 103 | static void gen_scale (struct agent_expr *ax, |
| 104 | enum agent_op op, struct type *type); |
| 105 | static void gen_add (struct agent_expr *ax, |
| 106 | struct axs_value *value, |
| 107 | struct axs_value *value1, |
| 108 | struct axs_value *value2, char *name); |
| 109 | static void gen_sub (struct agent_expr *ax, |
| 110 | struct axs_value *value, |
| 111 | struct axs_value *value1, struct axs_value *value2); |
| 112 | static void gen_binop (struct agent_expr *ax, |
| 113 | struct axs_value *value, |
| 114 | struct axs_value *value1, |
| 115 | struct axs_value *value2, |
| 116 | enum agent_op op, |
| 117 | enum agent_op op_unsigned, int may_carry, char *name); |
| 118 | static void gen_logical_not (struct agent_expr *ax, struct axs_value *value); |
| 119 | static void gen_complement (struct agent_expr *ax, struct axs_value *value); |
| 120 | static void gen_deref (struct agent_expr *, struct axs_value *); |
| 121 | static void gen_address_of (struct agent_expr *, struct axs_value *); |
| 122 | static int find_field (struct type *type, char *name); |
| 123 | static void gen_bitfield_ref (struct agent_expr *ax, |
| 124 | struct axs_value *value, |
| 125 | struct type *type, int start, int end); |
| 126 | static void gen_struct_ref (struct agent_expr *ax, |
| 127 | struct axs_value *value, |
| 128 | char *field, |
| 129 | char *operator_name, char *operand_name); |
| 130 | static void gen_repeat (union exp_element **pc, |
| 131 | struct agent_expr *ax, struct axs_value *value); |
| 132 | static void gen_sizeof (union exp_element **pc, |
| 133 | struct agent_expr *ax, struct axs_value *value); |
| 134 | static void gen_expr (union exp_element **pc, |
| 135 | struct agent_expr *ax, struct axs_value *value); |
| 136 | |
| 137 | static void agent_command (char *exp, int from_tty); |
| 138 | \f |
| 139 | |
| 140 | /* Detecting constant expressions. */ |
| 141 | |
| 142 | /* If the variable reference at *PC is a constant, return its value. |
| 143 | Otherwise, return zero. |
| 144 | |
| 145 | Hey, Wally! How can a variable reference be a constant? |
| 146 | |
| 147 | Well, Beav, this function really handles the OP_VAR_VALUE operator, |
| 148 | not specifically variable references. GDB uses OP_VAR_VALUE to |
| 149 | refer to any kind of symbolic reference: function names, enum |
| 150 | elements, and goto labels are all handled through the OP_VAR_VALUE |
| 151 | operator, even though they're constants. It makes sense given the |
| 152 | situation. |
| 153 | |
| 154 | Gee, Wally, don'cha wonder sometimes if data representations that |
| 155 | subvert commonly accepted definitions of terms in favor of heavily |
| 156 | context-specific interpretations are really just a tool of the |
| 157 | programming hegemony to preserve their power and exclude the |
| 158 | proletariat? */ |
| 159 | |
| 160 | static struct value * |
| 161 | const_var_ref (struct symbol *var) |
| 162 | { |
| 163 | struct type *type = SYMBOL_TYPE (var); |
| 164 | |
| 165 | switch (SYMBOL_CLASS (var)) |
| 166 | { |
| 167 | case LOC_CONST: |
| 168 | return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var)); |
| 169 | |
| 170 | case LOC_LABEL: |
| 171 | return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var)); |
| 172 | |
| 173 | default: |
| 174 | return 0; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /* If the expression starting at *PC has a constant value, return it. |
| 180 | Otherwise, return zero. If we return a value, then *PC will be |
| 181 | advanced to the end of it. If we return zero, *PC could be |
| 182 | anywhere. */ |
| 183 | static struct value * |
| 184 | const_expr (union exp_element **pc) |
| 185 | { |
| 186 | enum exp_opcode op = (*pc)->opcode; |
| 187 | struct value *v1; |
| 188 | |
| 189 | switch (op) |
| 190 | { |
| 191 | case OP_LONG: |
| 192 | { |
| 193 | struct type *type = (*pc)[1].type; |
| 194 | LONGEST k = (*pc)[2].longconst; |
| 195 | (*pc) += 4; |
| 196 | return value_from_longest (type, k); |
| 197 | } |
| 198 | |
| 199 | case OP_VAR_VALUE: |
| 200 | { |
| 201 | struct value *v = const_var_ref ((*pc)[2].symbol); |
| 202 | (*pc) += 4; |
| 203 | return v; |
| 204 | } |
| 205 | |
| 206 | /* We could add more operators in here. */ |
| 207 | |
| 208 | case UNOP_NEG: |
| 209 | (*pc)++; |
| 210 | v1 = const_expr (pc); |
| 211 | if (v1) |
| 212 | return value_neg (v1); |
| 213 | else |
| 214 | return 0; |
| 215 | |
| 216 | default: |
| 217 | return 0; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /* Like const_expr, but guarantee also that *PC is undisturbed if the |
| 223 | expression is not constant. */ |
| 224 | static struct value * |
| 225 | maybe_const_expr (union exp_element **pc) |
| 226 | { |
| 227 | union exp_element *tentative_pc = *pc; |
| 228 | struct value *v = const_expr (&tentative_pc); |
| 229 | |
| 230 | /* If we got a value, then update the real PC. */ |
| 231 | if (v) |
| 232 | *pc = tentative_pc; |
| 233 | |
| 234 | return v; |
| 235 | } |
| 236 | \f |
| 237 | |
| 238 | /* Generating bytecode from GDB expressions: general assumptions */ |
| 239 | |
| 240 | /* Here are a few general assumptions made throughout the code; if you |
| 241 | want to make a change that contradicts one of these, then you'd |
| 242 | better scan things pretty thoroughly. |
| 243 | |
| 244 | - We assume that all values occupy one stack element. For example, |
| 245 | sometimes we'll swap to get at the left argument to a binary |
| 246 | operator. If we decide that void values should occupy no stack |
| 247 | elements, or that synthetic arrays (whose size is determined at |
| 248 | run time, created by the `@' operator) should occupy two stack |
| 249 | elements (address and length), then this will cause trouble. |
| 250 | |
| 251 | - We assume the stack elements are infinitely wide, and that we |
| 252 | don't have to worry what happens if the user requests an |
| 253 | operation that is wider than the actual interpreter's stack. |
| 254 | That is, it's up to the interpreter to handle directly all the |
| 255 | integer widths the user has access to. (Woe betide the language |
| 256 | with bignums!) |
| 257 | |
| 258 | - We don't support side effects. Thus, we don't have to worry about |
| 259 | GCC's generalized lvalues, function calls, etc. |
| 260 | |
| 261 | - We don't support floating point. Many places where we switch on |
| 262 | some type don't bother to include cases for floating point; there |
| 263 | may be even more subtle ways this assumption exists. For |
| 264 | example, the arguments to % must be integers. |
| 265 | |
| 266 | - We assume all subexpressions have a static, unchanging type. If |
| 267 | we tried to support convenience variables, this would be a |
| 268 | problem. |
| 269 | |
| 270 | - All values on the stack should always be fully zero- or |
| 271 | sign-extended. |
| 272 | |
| 273 | (I wasn't sure whether to choose this or its opposite --- that |
| 274 | only addresses are assumed extended --- but it turns out that |
| 275 | neither convention completely eliminates spurious extend |
| 276 | operations (if everything is always extended, then you have to |
| 277 | extend after add, because it could overflow; if nothing is |
| 278 | extended, then you end up producing extends whenever you change |
| 279 | sizes), and this is simpler.) */ |
| 280 | \f |
| 281 | |
| 282 | /* Generating bytecode from GDB expressions: the `trace' kludge */ |
| 283 | |
| 284 | /* The compiler in this file is a general-purpose mechanism for |
| 285 | translating GDB expressions into bytecode. One ought to be able to |
| 286 | find a million and one uses for it. |
| 287 | |
| 288 | However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake |
| 289 | of expediency. Let he who is without sin cast the first stone. |
| 290 | |
| 291 | For the data tracing facility, we need to insert `trace' bytecodes |
| 292 | before each data fetch; this records all the memory that the |
| 293 | expression touches in the course of evaluation, so that memory will |
| 294 | be available when the user later tries to evaluate the expression |
| 295 | in GDB. |
| 296 | |
| 297 | This should be done (I think) in a post-processing pass, that walks |
| 298 | an arbitrary agent expression and inserts `trace' operations at the |
| 299 | appropriate points. But it's much faster to just hack them |
| 300 | directly into the code. And since we're in a crunch, that's what |
| 301 | I've done. |
| 302 | |
| 303 | Setting the flag trace_kludge to non-zero enables the code that |
| 304 | emits the trace bytecodes at the appropriate points. */ |
| 305 | static int trace_kludge; |
| 306 | |
| 307 | /* Trace the lvalue on the stack, if it needs it. In either case, pop |
| 308 | the value. Useful on the left side of a comma, and at the end of |
| 309 | an expression being used for tracing. */ |
| 310 | static void |
| 311 | gen_traced_pop (struct agent_expr *ax, struct axs_value *value) |
| 312 | { |
| 313 | if (trace_kludge) |
| 314 | switch (value->kind) |
| 315 | { |
| 316 | case axs_rvalue: |
| 317 | /* We don't trace rvalues, just the lvalues necessary to |
| 318 | produce them. So just dispose of this value. */ |
| 319 | ax_simple (ax, aop_pop); |
| 320 | break; |
| 321 | |
| 322 | case axs_lvalue_memory: |
| 323 | { |
| 324 | int length = TYPE_LENGTH (value->type); |
| 325 | |
| 326 | /* There's no point in trying to use a trace_quick bytecode |
| 327 | here, since "trace_quick SIZE pop" is three bytes, whereas |
| 328 | "const8 SIZE trace" is also three bytes, does the same |
| 329 | thing, and the simplest code which generates that will also |
| 330 | work correctly for objects with large sizes. */ |
| 331 | ax_const_l (ax, length); |
| 332 | ax_simple (ax, aop_trace); |
| 333 | } |
| 334 | break; |
| 335 | |
| 336 | case axs_lvalue_register: |
| 337 | /* We need to mention the register somewhere in the bytecode, |
| 338 | so ax_reqs will pick it up and add it to the mask of |
| 339 | registers used. */ |
| 340 | ax_reg (ax, value->u.reg); |
| 341 | ax_simple (ax, aop_pop); |
| 342 | break; |
| 343 | } |
| 344 | else |
| 345 | /* If we're not tracing, just pop the value. */ |
| 346 | ax_simple (ax, aop_pop); |
| 347 | } |
| 348 | \f |
| 349 | |
| 350 | |
| 351 | /* Generating bytecode from GDB expressions: helper functions */ |
| 352 | |
| 353 | /* Assume that the lower bits of the top of the stack is a value of |
| 354 | type TYPE, and the upper bits are zero. Sign-extend if necessary. */ |
| 355 | static void |
| 356 | gen_sign_extend (struct agent_expr *ax, struct type *type) |
| 357 | { |
| 358 | /* Do we need to sign-extend this? */ |
| 359 | if (!TYPE_UNSIGNED (type)) |
| 360 | ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | /* Assume the lower bits of the top of the stack hold a value of type |
| 365 | TYPE, and the upper bits are garbage. Sign-extend or truncate as |
| 366 | needed. */ |
| 367 | static void |
| 368 | gen_extend (struct agent_expr *ax, struct type *type) |
| 369 | { |
| 370 | int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT; |
| 371 | /* I just had to. */ |
| 372 | ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits)); |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /* Assume that the top of the stack contains a value of type "pointer |
| 377 | to TYPE"; generate code to fetch its value. Note that TYPE is the |
| 378 | target type, not the pointer type. */ |
| 379 | static void |
| 380 | gen_fetch (struct agent_expr *ax, struct type *type) |
| 381 | { |
| 382 | if (trace_kludge) |
| 383 | { |
| 384 | /* Record the area of memory we're about to fetch. */ |
| 385 | ax_trace_quick (ax, TYPE_LENGTH (type)); |
| 386 | } |
| 387 | |
| 388 | switch (TYPE_CODE (type)) |
| 389 | { |
| 390 | case TYPE_CODE_PTR: |
| 391 | case TYPE_CODE_ENUM: |
| 392 | case TYPE_CODE_INT: |
| 393 | case TYPE_CODE_CHAR: |
| 394 | /* It's a scalar value, so we know how to dereference it. How |
| 395 | many bytes long is it? */ |
| 396 | switch (TYPE_LENGTH (type)) |
| 397 | { |
| 398 | case 8 / TARGET_CHAR_BIT: |
| 399 | ax_simple (ax, aop_ref8); |
| 400 | break; |
| 401 | case 16 / TARGET_CHAR_BIT: |
| 402 | ax_simple (ax, aop_ref16); |
| 403 | break; |
| 404 | case 32 / TARGET_CHAR_BIT: |
| 405 | ax_simple (ax, aop_ref32); |
| 406 | break; |
| 407 | case 64 / TARGET_CHAR_BIT: |
| 408 | ax_simple (ax, aop_ref64); |
| 409 | break; |
| 410 | |
| 411 | /* Either our caller shouldn't have asked us to dereference |
| 412 | that pointer (other code's fault), or we're not |
| 413 | implementing something we should be (this code's fault). |
| 414 | In any case, it's a bug the user shouldn't see. */ |
| 415 | default: |
| 416 | internal_error (__FILE__, __LINE__, |
| 417 | _("gen_fetch: strange size")); |
| 418 | } |
| 419 | |
| 420 | gen_sign_extend (ax, type); |
| 421 | break; |
| 422 | |
| 423 | default: |
| 424 | /* Either our caller shouldn't have asked us to dereference that |
| 425 | pointer (other code's fault), or we're not implementing |
| 426 | something we should be (this code's fault). In any case, |
| 427 | it's a bug the user shouldn't see. */ |
| 428 | internal_error (__FILE__, __LINE__, |
| 429 | _("gen_fetch: bad type code")); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* Generate code to left shift the top of the stack by DISTANCE bits, or |
| 435 | right shift it by -DISTANCE bits if DISTANCE < 0. This generates |
| 436 | unsigned (logical) right shifts. */ |
| 437 | static void |
| 438 | gen_left_shift (struct agent_expr *ax, int distance) |
| 439 | { |
| 440 | if (distance > 0) |
| 441 | { |
| 442 | ax_const_l (ax, distance); |
| 443 | ax_simple (ax, aop_lsh); |
| 444 | } |
| 445 | else if (distance < 0) |
| 446 | { |
| 447 | ax_const_l (ax, -distance); |
| 448 | ax_simple (ax, aop_rsh_unsigned); |
| 449 | } |
| 450 | } |
| 451 | \f |
| 452 | |
| 453 | |
| 454 | /* Generating bytecode from GDB expressions: symbol references */ |
| 455 | |
| 456 | /* Generate code to push the base address of the argument portion of |
| 457 | the top stack frame. */ |
| 458 | static void |
| 459 | gen_frame_args_address (struct agent_expr *ax) |
| 460 | { |
| 461 | int frame_reg; |
| 462 | LONGEST frame_offset; |
| 463 | |
| 464 | TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset); |
| 465 | ax_reg (ax, frame_reg); |
| 466 | gen_offset (ax, frame_offset); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /* Generate code to push the base address of the locals portion of the |
| 471 | top stack frame. */ |
| 472 | static void |
| 473 | gen_frame_locals_address (struct agent_expr *ax) |
| 474 | { |
| 475 | int frame_reg; |
| 476 | LONGEST frame_offset; |
| 477 | |
| 478 | TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset); |
| 479 | ax_reg (ax, frame_reg); |
| 480 | gen_offset (ax, frame_offset); |
| 481 | } |
| 482 | |
| 483 | |
| 484 | /* Generate code to add OFFSET to the top of the stack. Try to |
| 485 | generate short and readable code. We use this for getting to |
| 486 | variables on the stack, and structure members. If we were |
| 487 | programming in ML, it would be clearer why these are the same |
| 488 | thing. */ |
| 489 | static void |
| 490 | gen_offset (struct agent_expr *ax, int offset) |
| 491 | { |
| 492 | /* It would suffice to simply push the offset and add it, but this |
| 493 | makes it easier to read positive and negative offsets in the |
| 494 | bytecode. */ |
| 495 | if (offset > 0) |
| 496 | { |
| 497 | ax_const_l (ax, offset); |
| 498 | ax_simple (ax, aop_add); |
| 499 | } |
| 500 | else if (offset < 0) |
| 501 | { |
| 502 | ax_const_l (ax, -offset); |
| 503 | ax_simple (ax, aop_sub); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | |
| 508 | /* In many cases, a symbol's value is the offset from some other |
| 509 | address (stack frame, base register, etc.) Generate code to add |
| 510 | VAR's value to the top of the stack. */ |
| 511 | static void |
| 512 | gen_sym_offset (struct agent_expr *ax, struct symbol *var) |
| 513 | { |
| 514 | gen_offset (ax, SYMBOL_VALUE (var)); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | /* Generate code for a variable reference to AX. The variable is the |
| 519 | symbol VAR. Set VALUE to describe the result. */ |
| 520 | |
| 521 | static void |
| 522 | gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var) |
| 523 | { |
| 524 | /* Dereference any typedefs. */ |
| 525 | value->type = check_typedef (SYMBOL_TYPE (var)); |
| 526 | |
| 527 | /* I'm imitating the code in read_var_value. */ |
| 528 | switch (SYMBOL_CLASS (var)) |
| 529 | { |
| 530 | case LOC_CONST: /* A constant, like an enum value. */ |
| 531 | ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var)); |
| 532 | value->kind = axs_rvalue; |
| 533 | break; |
| 534 | |
| 535 | case LOC_LABEL: /* A goto label, being used as a value. */ |
| 536 | ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var)); |
| 537 | value->kind = axs_rvalue; |
| 538 | break; |
| 539 | |
| 540 | case LOC_CONST_BYTES: |
| 541 | internal_error (__FILE__, __LINE__, |
| 542 | _("gen_var_ref: LOC_CONST_BYTES symbols are not supported")); |
| 543 | |
| 544 | /* Variable at a fixed location in memory. Easy. */ |
| 545 | case LOC_STATIC: |
| 546 | /* Push the address of the variable. */ |
| 547 | ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var)); |
| 548 | value->kind = axs_lvalue_memory; |
| 549 | break; |
| 550 | |
| 551 | case LOC_ARG: /* var lives in argument area of frame */ |
| 552 | gen_frame_args_address (ax); |
| 553 | gen_sym_offset (ax, var); |
| 554 | value->kind = axs_lvalue_memory; |
| 555 | break; |
| 556 | |
| 557 | case LOC_REF_ARG: /* As above, but the frame slot really |
| 558 | holds the address of the variable. */ |
| 559 | gen_frame_args_address (ax); |
| 560 | gen_sym_offset (ax, var); |
| 561 | /* Don't assume any particular pointer size. */ |
| 562 | gen_fetch (ax, lookup_pointer_type (builtin_type_void)); |
| 563 | value->kind = axs_lvalue_memory; |
| 564 | break; |
| 565 | |
| 566 | case LOC_LOCAL: /* var lives in locals area of frame */ |
| 567 | case LOC_LOCAL_ARG: |
| 568 | gen_frame_locals_address (ax); |
| 569 | gen_sym_offset (ax, var); |
| 570 | value->kind = axs_lvalue_memory; |
| 571 | break; |
| 572 | |
| 573 | case LOC_BASEREG: /* relative to some base register */ |
| 574 | case LOC_BASEREG_ARG: |
| 575 | ax_reg (ax, SYMBOL_BASEREG (var)); |
| 576 | gen_sym_offset (ax, var); |
| 577 | value->kind = axs_lvalue_memory; |
| 578 | break; |
| 579 | |
| 580 | case LOC_TYPEDEF: |
| 581 | error (_("Cannot compute value of typedef `%s'."), |
| 582 | SYMBOL_PRINT_NAME (var)); |
| 583 | break; |
| 584 | |
| 585 | case LOC_BLOCK: |
| 586 | ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var))); |
| 587 | value->kind = axs_rvalue; |
| 588 | break; |
| 589 | |
| 590 | case LOC_REGISTER: |
| 591 | case LOC_REGPARM: |
| 592 | /* Don't generate any code at all; in the process of treating |
| 593 | this as an lvalue or rvalue, the caller will generate the |
| 594 | right code. */ |
| 595 | value->kind = axs_lvalue_register; |
| 596 | value->u.reg = SYMBOL_VALUE (var); |
| 597 | break; |
| 598 | |
| 599 | /* A lot like LOC_REF_ARG, but the pointer lives directly in a |
| 600 | register, not on the stack. Simpler than LOC_REGISTER and |
| 601 | LOC_REGPARM, because it's just like any other case where the |
| 602 | thing has a real address. */ |
| 603 | case LOC_REGPARM_ADDR: |
| 604 | ax_reg (ax, SYMBOL_VALUE (var)); |
| 605 | value->kind = axs_lvalue_memory; |
| 606 | break; |
| 607 | |
| 608 | case LOC_UNRESOLVED: |
| 609 | { |
| 610 | struct minimal_symbol *msym |
| 611 | = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (var), NULL, NULL); |
| 612 | if (!msym) |
| 613 | error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var)); |
| 614 | |
| 615 | /* Push the address of the variable. */ |
| 616 | ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym)); |
| 617 | value->kind = axs_lvalue_memory; |
| 618 | } |
| 619 | break; |
| 620 | |
| 621 | case LOC_COMPUTED: |
| 622 | case LOC_COMPUTED_ARG: |
| 623 | /* FIXME: cagney/2004-01-26: It should be possible to |
| 624 | unconditionally call the SYMBOL_OPS method when available. |
| 625 | Unfortunately DWARF 2 stores the frame-base (instead of the |
| 626 | function) location in a function's symbol. Oops! For the |
| 627 | moment enable this when/where applicable. */ |
| 628 | SYMBOL_OPS (var)->tracepoint_var_ref (var, ax, value); |
| 629 | break; |
| 630 | |
| 631 | case LOC_OPTIMIZED_OUT: |
| 632 | error (_("The variable `%s' has been optimized out."), |
| 633 | SYMBOL_PRINT_NAME (var)); |
| 634 | break; |
| 635 | |
| 636 | default: |
| 637 | error (_("Cannot find value of botched symbol `%s'."), |
| 638 | SYMBOL_PRINT_NAME (var)); |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | \f |
| 643 | |
| 644 | |
| 645 | /* Generating bytecode from GDB expressions: literals */ |
| 646 | |
| 647 | static void |
| 648 | gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k, |
| 649 | struct type *type) |
| 650 | { |
| 651 | ax_const_l (ax, k); |
| 652 | value->kind = axs_rvalue; |
| 653 | value->type = type; |
| 654 | } |
| 655 | \f |
| 656 | |
| 657 | |
| 658 | /* Generating bytecode from GDB expressions: unary conversions, casts */ |
| 659 | |
| 660 | /* Take what's on the top of the stack (as described by VALUE), and |
| 661 | try to make an rvalue out of it. Signal an error if we can't do |
| 662 | that. */ |
| 663 | static void |
| 664 | require_rvalue (struct agent_expr *ax, struct axs_value *value) |
| 665 | { |
| 666 | switch (value->kind) |
| 667 | { |
| 668 | case axs_rvalue: |
| 669 | /* It's already an rvalue. */ |
| 670 | break; |
| 671 | |
| 672 | case axs_lvalue_memory: |
| 673 | /* The top of stack is the address of the object. Dereference. */ |
| 674 | gen_fetch (ax, value->type); |
| 675 | break; |
| 676 | |
| 677 | case axs_lvalue_register: |
| 678 | /* There's nothing on the stack, but value->u.reg is the |
| 679 | register number containing the value. |
| 680 | |
| 681 | When we add floating-point support, this is going to have to |
| 682 | change. What about SPARC register pairs, for example? */ |
| 683 | ax_reg (ax, value->u.reg); |
| 684 | gen_extend (ax, value->type); |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | value->kind = axs_rvalue; |
| 689 | } |
| 690 | |
| 691 | |
| 692 | /* Assume the top of the stack is described by VALUE, and perform the |
| 693 | usual unary conversions. This is motivated by ANSI 6.2.2, but of |
| 694 | course GDB expressions are not ANSI; they're the mishmash union of |
| 695 | a bunch of languages. Rah. |
| 696 | |
| 697 | NOTE! This function promises to produce an rvalue only when the |
| 698 | incoming value is of an appropriate type. In other words, the |
| 699 | consumer of the value this function produces may assume the value |
| 700 | is an rvalue only after checking its type. |
| 701 | |
| 702 | The immediate issue is that if the user tries to use a structure or |
| 703 | union as an operand of, say, the `+' operator, we don't want to try |
| 704 | to convert that structure to an rvalue; require_rvalue will bomb on |
| 705 | structs and unions. Rather, we want to simply pass the struct |
| 706 | lvalue through unchanged, and let `+' raise an error. */ |
| 707 | |
| 708 | static void |
| 709 | gen_usual_unary (struct agent_expr *ax, struct axs_value *value) |
| 710 | { |
| 711 | /* We don't have to generate any code for the usual integral |
| 712 | conversions, since values are always represented as full-width on |
| 713 | the stack. Should we tweak the type? */ |
| 714 | |
| 715 | /* Some types require special handling. */ |
| 716 | switch (TYPE_CODE (value->type)) |
| 717 | { |
| 718 | /* Functions get converted to a pointer to the function. */ |
| 719 | case TYPE_CODE_FUNC: |
| 720 | value->type = lookup_pointer_type (value->type); |
| 721 | value->kind = axs_rvalue; /* Should always be true, but just in case. */ |
| 722 | break; |
| 723 | |
| 724 | /* Arrays get converted to a pointer to their first element, and |
| 725 | are no longer an lvalue. */ |
| 726 | case TYPE_CODE_ARRAY: |
| 727 | { |
| 728 | struct type *elements = TYPE_TARGET_TYPE (value->type); |
| 729 | value->type = lookup_pointer_type (elements); |
| 730 | value->kind = axs_rvalue; |
| 731 | /* We don't need to generate any code; the address of the array |
| 732 | is also the address of its first element. */ |
| 733 | } |
| 734 | break; |
| 735 | |
| 736 | /* Don't try to convert structures and unions to rvalues. Let the |
| 737 | consumer signal an error. */ |
| 738 | case TYPE_CODE_STRUCT: |
| 739 | case TYPE_CODE_UNION: |
| 740 | return; |
| 741 | |
| 742 | /* If the value is an enum, call it an integer. */ |
| 743 | case TYPE_CODE_ENUM: |
| 744 | value->type = builtin_type_int; |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | /* If the value is an lvalue, dereference it. */ |
| 749 | require_rvalue (ax, value); |
| 750 | } |
| 751 | |
| 752 | |
| 753 | /* Return non-zero iff the type TYPE1 is considered "wider" than the |
| 754 | type TYPE2, according to the rules described in gen_usual_arithmetic. */ |
| 755 | static int |
| 756 | type_wider_than (struct type *type1, struct type *type2) |
| 757 | { |
| 758 | return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2) |
| 759 | || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2) |
| 760 | && TYPE_UNSIGNED (type1) |
| 761 | && !TYPE_UNSIGNED (type2))); |
| 762 | } |
| 763 | |
| 764 | |
| 765 | /* Return the "wider" of the two types TYPE1 and TYPE2. */ |
| 766 | static struct type * |
| 767 | max_type (struct type *type1, struct type *type2) |
| 768 | { |
| 769 | return type_wider_than (type1, type2) ? type1 : type2; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | /* Generate code to convert a scalar value of type FROM to type TO. */ |
| 774 | static void |
| 775 | gen_conversion (struct agent_expr *ax, struct type *from, struct type *to) |
| 776 | { |
| 777 | /* Perhaps there is a more graceful way to state these rules. */ |
| 778 | |
| 779 | /* If we're converting to a narrower type, then we need to clear out |
| 780 | the upper bits. */ |
| 781 | if (TYPE_LENGTH (to) < TYPE_LENGTH (from)) |
| 782 | gen_extend (ax, from); |
| 783 | |
| 784 | /* If the two values have equal width, but different signednesses, |
| 785 | then we need to extend. */ |
| 786 | else if (TYPE_LENGTH (to) == TYPE_LENGTH (from)) |
| 787 | { |
| 788 | if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to)) |
| 789 | gen_extend (ax, to); |
| 790 | } |
| 791 | |
| 792 | /* If we're converting to a wider type, and becoming unsigned, then |
| 793 | we need to zero out any possible sign bits. */ |
| 794 | else if (TYPE_LENGTH (to) > TYPE_LENGTH (from)) |
| 795 | { |
| 796 | if (TYPE_UNSIGNED (to)) |
| 797 | gen_extend (ax, to); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | |
| 802 | /* Return non-zero iff the type FROM will require any bytecodes to be |
| 803 | emitted to be converted to the type TO. */ |
| 804 | static int |
| 805 | is_nontrivial_conversion (struct type *from, struct type *to) |
| 806 | { |
| 807 | struct agent_expr *ax = new_agent_expr (0); |
| 808 | int nontrivial; |
| 809 | |
| 810 | /* Actually generate the code, and see if anything came out. At the |
| 811 | moment, it would be trivial to replicate the code in |
| 812 | gen_conversion here, but in the future, when we're supporting |
| 813 | floating point and the like, it may not be. Doing things this |
| 814 | way allows this function to be independent of the logic in |
| 815 | gen_conversion. */ |
| 816 | gen_conversion (ax, from, to); |
| 817 | nontrivial = ax->len > 0; |
| 818 | free_agent_expr (ax); |
| 819 | return nontrivial; |
| 820 | } |
| 821 | |
| 822 | |
| 823 | /* Generate code to perform the "usual arithmetic conversions" (ANSI C |
| 824 | 6.2.1.5) for the two operands of an arithmetic operator. This |
| 825 | effectively finds a "least upper bound" type for the two arguments, |
| 826 | and promotes each argument to that type. *VALUE1 and *VALUE2 |
| 827 | describe the values as they are passed in, and as they are left. */ |
| 828 | static void |
| 829 | gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1, |
| 830 | struct axs_value *value2) |
| 831 | { |
| 832 | /* Do the usual binary conversions. */ |
| 833 | if (TYPE_CODE (value1->type) == TYPE_CODE_INT |
| 834 | && TYPE_CODE (value2->type) == TYPE_CODE_INT) |
| 835 | { |
| 836 | /* The ANSI integral promotions seem to work this way: Order the |
| 837 | integer types by size, and then by signedness: an n-bit |
| 838 | unsigned type is considered "wider" than an n-bit signed |
| 839 | type. Promote to the "wider" of the two types, and always |
| 840 | promote at least to int. */ |
| 841 | struct type *target = max_type (builtin_type_int, |
| 842 | max_type (value1->type, value2->type)); |
| 843 | |
| 844 | /* Deal with value2, on the top of the stack. */ |
| 845 | gen_conversion (ax, value2->type, target); |
| 846 | |
| 847 | /* Deal with value1, not on the top of the stack. Don't |
| 848 | generate the `swap' instructions if we're not actually going |
| 849 | to do anything. */ |
| 850 | if (is_nontrivial_conversion (value1->type, target)) |
| 851 | { |
| 852 | ax_simple (ax, aop_swap); |
| 853 | gen_conversion (ax, value1->type, target); |
| 854 | ax_simple (ax, aop_swap); |
| 855 | } |
| 856 | |
| 857 | value1->type = value2->type = target; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | |
| 862 | /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on |
| 863 | the value on the top of the stack, as described by VALUE. Assume |
| 864 | the value has integral type. */ |
| 865 | static void |
| 866 | gen_integral_promotions (struct agent_expr *ax, struct axs_value *value) |
| 867 | { |
| 868 | if (!type_wider_than (value->type, builtin_type_int)) |
| 869 | { |
| 870 | gen_conversion (ax, value->type, builtin_type_int); |
| 871 | value->type = builtin_type_int; |
| 872 | } |
| 873 | else if (!type_wider_than (value->type, builtin_type_unsigned_int)) |
| 874 | { |
| 875 | gen_conversion (ax, value->type, builtin_type_unsigned_int); |
| 876 | value->type = builtin_type_unsigned_int; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | |
| 881 | /* Generate code for a cast to TYPE. */ |
| 882 | static void |
| 883 | gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type) |
| 884 | { |
| 885 | /* GCC does allow casts to yield lvalues, so this should be fixed |
| 886 | before merging these changes into the trunk. */ |
| 887 | require_rvalue (ax, value); |
| 888 | /* Dereference typedefs. */ |
| 889 | type = check_typedef (type); |
| 890 | |
| 891 | switch (TYPE_CODE (type)) |
| 892 | { |
| 893 | case TYPE_CODE_PTR: |
| 894 | /* It's implementation-defined, and I'll bet this is what GCC |
| 895 | does. */ |
| 896 | break; |
| 897 | |
| 898 | case TYPE_CODE_ARRAY: |
| 899 | case TYPE_CODE_STRUCT: |
| 900 | case TYPE_CODE_UNION: |
| 901 | case TYPE_CODE_FUNC: |
| 902 | error (_("Invalid type cast: intended type must be scalar.")); |
| 903 | |
| 904 | case TYPE_CODE_ENUM: |
| 905 | /* We don't have to worry about the size of the value, because |
| 906 | all our integral values are fully sign-extended, and when |
| 907 | casting pointers we can do anything we like. Is there any |
| 908 | way for us to actually know what GCC actually does with a |
| 909 | cast like this? */ |
| 910 | value->type = type; |
| 911 | break; |
| 912 | |
| 913 | case TYPE_CODE_INT: |
| 914 | gen_conversion (ax, value->type, type); |
| 915 | break; |
| 916 | |
| 917 | case TYPE_CODE_VOID: |
| 918 | /* We could pop the value, and rely on everyone else to check |
| 919 | the type and notice that this value doesn't occupy a stack |
| 920 | slot. But for now, leave the value on the stack, and |
| 921 | preserve the "value == stack element" assumption. */ |
| 922 | break; |
| 923 | |
| 924 | default: |
| 925 | error (_("Casts to requested type are not yet implemented.")); |
| 926 | } |
| 927 | |
| 928 | value->type = type; |
| 929 | } |
| 930 | \f |
| 931 | |
| 932 | |
| 933 | /* Generating bytecode from GDB expressions: arithmetic */ |
| 934 | |
| 935 | /* Scale the integer on the top of the stack by the size of the target |
| 936 | of the pointer type TYPE. */ |
| 937 | static void |
| 938 | gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type) |
| 939 | { |
| 940 | struct type *element = TYPE_TARGET_TYPE (type); |
| 941 | |
| 942 | if (TYPE_LENGTH (element) != 1) |
| 943 | { |
| 944 | ax_const_l (ax, TYPE_LENGTH (element)); |
| 945 | ax_simple (ax, op); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | |
| 950 | /* Generate code for an addition; non-trivial because we deal with |
| 951 | pointer arithmetic. We set VALUE to describe the result value; we |
| 952 | assume VALUE1 and VALUE2 describe the two operands, and that |
| 953 | they've undergone the usual binary conversions. Used by both |
| 954 | BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */ |
| 955 | static void |
| 956 | gen_add (struct agent_expr *ax, struct axs_value *value, |
| 957 | struct axs_value *value1, struct axs_value *value2, char *name) |
| 958 | { |
| 959 | /* Is it INT+PTR? */ |
| 960 | if (TYPE_CODE (value1->type) == TYPE_CODE_INT |
| 961 | && TYPE_CODE (value2->type) == TYPE_CODE_PTR) |
| 962 | { |
| 963 | /* Swap the values and proceed normally. */ |
| 964 | ax_simple (ax, aop_swap); |
| 965 | gen_scale (ax, aop_mul, value2->type); |
| 966 | ax_simple (ax, aop_add); |
| 967 | gen_extend (ax, value2->type); /* Catch overflow. */ |
| 968 | value->type = value2->type; |
| 969 | } |
| 970 | |
| 971 | /* Is it PTR+INT? */ |
| 972 | else if (TYPE_CODE (value1->type) == TYPE_CODE_PTR |
| 973 | && TYPE_CODE (value2->type) == TYPE_CODE_INT) |
| 974 | { |
| 975 | gen_scale (ax, aop_mul, value1->type); |
| 976 | ax_simple (ax, aop_add); |
| 977 | gen_extend (ax, value1->type); /* Catch overflow. */ |
| 978 | value->type = value1->type; |
| 979 | } |
| 980 | |
| 981 | /* Must be number + number; the usual binary conversions will have |
| 982 | brought them both to the same width. */ |
| 983 | else if (TYPE_CODE (value1->type) == TYPE_CODE_INT |
| 984 | && TYPE_CODE (value2->type) == TYPE_CODE_INT) |
| 985 | { |
| 986 | ax_simple (ax, aop_add); |
| 987 | gen_extend (ax, value1->type); /* Catch overflow. */ |
| 988 | value->type = value1->type; |
| 989 | } |
| 990 | |
| 991 | else |
| 992 | error (_("Invalid combination of types in %s."), name); |
| 993 | |
| 994 | value->kind = axs_rvalue; |
| 995 | } |
| 996 | |
| 997 | |
| 998 | /* Generate code for an addition; non-trivial because we have to deal |
| 999 | with pointer arithmetic. We set VALUE to describe the result |
| 1000 | value; we assume VALUE1 and VALUE2 describe the two operands, and |
| 1001 | that they've undergone the usual binary conversions. */ |
| 1002 | static void |
| 1003 | gen_sub (struct agent_expr *ax, struct axs_value *value, |
| 1004 | struct axs_value *value1, struct axs_value *value2) |
| 1005 | { |
| 1006 | if (TYPE_CODE (value1->type) == TYPE_CODE_PTR) |
| 1007 | { |
| 1008 | /* Is it PTR - INT? */ |
| 1009 | if (TYPE_CODE (value2->type) == TYPE_CODE_INT) |
| 1010 | { |
| 1011 | gen_scale (ax, aop_mul, value1->type); |
| 1012 | ax_simple (ax, aop_sub); |
| 1013 | gen_extend (ax, value1->type); /* Catch overflow. */ |
| 1014 | value->type = value1->type; |
| 1015 | } |
| 1016 | |
| 1017 | /* Is it PTR - PTR? Strictly speaking, the types ought to |
| 1018 | match, but this is what the normal GDB expression evaluator |
| 1019 | tests for. */ |
| 1020 | else if (TYPE_CODE (value2->type) == TYPE_CODE_PTR |
| 1021 | && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type)) |
| 1022 | == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))) |
| 1023 | { |
| 1024 | ax_simple (ax, aop_sub); |
| 1025 | gen_scale (ax, aop_div_unsigned, value1->type); |
| 1026 | value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */ |
| 1027 | } |
| 1028 | else |
| 1029 | error (_("\ |
| 1030 | First argument of `-' is a pointer, but second argument is neither\n\ |
| 1031 | an integer nor a pointer of the same type.")); |
| 1032 | } |
| 1033 | |
| 1034 | /* Must be number + number. */ |
| 1035 | else if (TYPE_CODE (value1->type) == TYPE_CODE_INT |
| 1036 | && TYPE_CODE (value2->type) == TYPE_CODE_INT) |
| 1037 | { |
| 1038 | ax_simple (ax, aop_sub); |
| 1039 | gen_extend (ax, value1->type); /* Catch overflow. */ |
| 1040 | value->type = value1->type; |
| 1041 | } |
| 1042 | |
| 1043 | else |
| 1044 | error (_("Invalid combination of types in subtraction.")); |
| 1045 | |
| 1046 | value->kind = axs_rvalue; |
| 1047 | } |
| 1048 | |
| 1049 | /* Generate code for a binary operator that doesn't do pointer magic. |
| 1050 | We set VALUE to describe the result value; we assume VALUE1 and |
| 1051 | VALUE2 describe the two operands, and that they've undergone the |
| 1052 | usual binary conversions. MAY_CARRY should be non-zero iff the |
| 1053 | result needs to be extended. NAME is the English name of the |
| 1054 | operator, used in error messages */ |
| 1055 | static void |
| 1056 | gen_binop (struct agent_expr *ax, struct axs_value *value, |
| 1057 | struct axs_value *value1, struct axs_value *value2, enum agent_op op, |
| 1058 | enum agent_op op_unsigned, int may_carry, char *name) |
| 1059 | { |
| 1060 | /* We only handle INT op INT. */ |
| 1061 | if ((TYPE_CODE (value1->type) != TYPE_CODE_INT) |
| 1062 | || (TYPE_CODE (value2->type) != TYPE_CODE_INT)) |
| 1063 | error (_("Invalid combination of types in %s."), name); |
| 1064 | |
| 1065 | ax_simple (ax, |
| 1066 | TYPE_UNSIGNED (value1->type) ? op_unsigned : op); |
| 1067 | if (may_carry) |
| 1068 | gen_extend (ax, value1->type); /* catch overflow */ |
| 1069 | value->type = value1->type; |
| 1070 | value->kind = axs_rvalue; |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | static void |
| 1075 | gen_logical_not (struct agent_expr *ax, struct axs_value *value) |
| 1076 | { |
| 1077 | if (TYPE_CODE (value->type) != TYPE_CODE_INT |
| 1078 | && TYPE_CODE (value->type) != TYPE_CODE_PTR) |
| 1079 | error (_("Invalid type of operand to `!'.")); |
| 1080 | |
| 1081 | gen_usual_unary (ax, value); |
| 1082 | ax_simple (ax, aop_log_not); |
| 1083 | value->type = builtin_type_int; |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | static void |
| 1088 | gen_complement (struct agent_expr *ax, struct axs_value *value) |
| 1089 | { |
| 1090 | if (TYPE_CODE (value->type) != TYPE_CODE_INT) |
| 1091 | error (_("Invalid type of operand to `~'.")); |
| 1092 | |
| 1093 | gen_usual_unary (ax, value); |
| 1094 | gen_integral_promotions (ax, value); |
| 1095 | ax_simple (ax, aop_bit_not); |
| 1096 | gen_extend (ax, value->type); |
| 1097 | } |
| 1098 | \f |
| 1099 | |
| 1100 | |
| 1101 | /* Generating bytecode from GDB expressions: * & . -> @ sizeof */ |
| 1102 | |
| 1103 | /* Dereference the value on the top of the stack. */ |
| 1104 | static void |
| 1105 | gen_deref (struct agent_expr *ax, struct axs_value *value) |
| 1106 | { |
| 1107 | /* The caller should check the type, because several operators use |
| 1108 | this, and we don't know what error message to generate. */ |
| 1109 | if (TYPE_CODE (value->type) != TYPE_CODE_PTR) |
| 1110 | internal_error (__FILE__, __LINE__, |
| 1111 | _("gen_deref: expected a pointer")); |
| 1112 | |
| 1113 | /* We've got an rvalue now, which is a pointer. We want to yield an |
| 1114 | lvalue, whose address is exactly that pointer. So we don't |
| 1115 | actually emit any code; we just change the type from "Pointer to |
| 1116 | T" to "T", and mark the value as an lvalue in memory. Leave it |
| 1117 | to the consumer to actually dereference it. */ |
| 1118 | value->type = check_typedef (TYPE_TARGET_TYPE (value->type)); |
| 1119 | value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC) |
| 1120 | ? axs_rvalue : axs_lvalue_memory); |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | /* Produce the address of the lvalue on the top of the stack. */ |
| 1125 | static void |
| 1126 | gen_address_of (struct agent_expr *ax, struct axs_value *value) |
| 1127 | { |
| 1128 | /* Special case for taking the address of a function. The ANSI |
| 1129 | standard describes this as a special case, too, so this |
| 1130 | arrangement is not without motivation. */ |
| 1131 | if (TYPE_CODE (value->type) == TYPE_CODE_FUNC) |
| 1132 | /* The value's already an rvalue on the stack, so we just need to |
| 1133 | change the type. */ |
| 1134 | value->type = lookup_pointer_type (value->type); |
| 1135 | else |
| 1136 | switch (value->kind) |
| 1137 | { |
| 1138 | case axs_rvalue: |
| 1139 | error (_("Operand of `&' is an rvalue, which has no address.")); |
| 1140 | |
| 1141 | case axs_lvalue_register: |
| 1142 | error (_("Operand of `&' is in a register, and has no address.")); |
| 1143 | |
| 1144 | case axs_lvalue_memory: |
| 1145 | value->kind = axs_rvalue; |
| 1146 | value->type = lookup_pointer_type (value->type); |
| 1147 | break; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | /* A lot of this stuff will have to change to support C++. But we're |
| 1153 | not going to deal with that at the moment. */ |
| 1154 | |
| 1155 | /* Find the field in the structure type TYPE named NAME, and return |
| 1156 | its index in TYPE's field array. */ |
| 1157 | static int |
| 1158 | find_field (struct type *type, char *name) |
| 1159 | { |
| 1160 | int i; |
| 1161 | |
| 1162 | CHECK_TYPEDEF (type); |
| 1163 | |
| 1164 | /* Make sure this isn't C++. */ |
| 1165 | if (TYPE_N_BASECLASSES (type) != 0) |
| 1166 | internal_error (__FILE__, __LINE__, |
| 1167 | _("find_field: derived classes supported")); |
| 1168 | |
| 1169 | for (i = 0; i < TYPE_NFIELDS (type); i++) |
| 1170 | { |
| 1171 | char *this_name = TYPE_FIELD_NAME (type, i); |
| 1172 | |
| 1173 | if (this_name && strcmp (name, this_name) == 0) |
| 1174 | return i; |
| 1175 | |
| 1176 | if (this_name[0] == '\0') |
| 1177 | internal_error (__FILE__, __LINE__, |
| 1178 | _("find_field: anonymous unions not supported")); |
| 1179 | } |
| 1180 | |
| 1181 | error (_("Couldn't find member named `%s' in struct/union `%s'"), |
| 1182 | name, TYPE_TAG_NAME (type)); |
| 1183 | |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | /* Generate code to push the value of a bitfield of a structure whose |
| 1189 | address is on the top of the stack. START and END give the |
| 1190 | starting and one-past-ending *bit* numbers of the field within the |
| 1191 | structure. */ |
| 1192 | static void |
| 1193 | gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value, |
| 1194 | struct type *type, int start, int end) |
| 1195 | { |
| 1196 | /* Note that ops[i] fetches 8 << i bits. */ |
| 1197 | static enum agent_op ops[] |
| 1198 | = |
| 1199 | {aop_ref8, aop_ref16, aop_ref32, aop_ref64}; |
| 1200 | static int num_ops = (sizeof (ops) / sizeof (ops[0])); |
| 1201 | |
| 1202 | /* We don't want to touch any byte that the bitfield doesn't |
| 1203 | actually occupy; we shouldn't make any accesses we're not |
| 1204 | explicitly permitted to. We rely here on the fact that the |
| 1205 | bytecode `ref' operators work on unaligned addresses. |
| 1206 | |
| 1207 | It takes some fancy footwork to get the stack to work the way |
| 1208 | we'd like. Say we're retrieving a bitfield that requires three |
| 1209 | fetches. Initially, the stack just contains the address: |
| 1210 | addr |
| 1211 | For the first fetch, we duplicate the address |
| 1212 | addr addr |
| 1213 | then add the byte offset, do the fetch, and shift and mask as |
| 1214 | needed, yielding a fragment of the value, properly aligned for |
| 1215 | the final bitwise or: |
| 1216 | addr frag1 |
| 1217 | then we swap, and repeat the process: |
| 1218 | frag1 addr --- address on top |
| 1219 | frag1 addr addr --- duplicate it |
| 1220 | frag1 addr frag2 --- get second fragment |
| 1221 | frag1 frag2 addr --- swap again |
| 1222 | frag1 frag2 frag3 --- get third fragment |
| 1223 | Notice that, since the third fragment is the last one, we don't |
| 1224 | bother duplicating the address this time. Now we have all the |
| 1225 | fragments on the stack, and we can simply `or' them together, |
| 1226 | yielding the final value of the bitfield. */ |
| 1227 | |
| 1228 | /* The first and one-after-last bits in the field, but rounded down |
| 1229 | and up to byte boundaries. */ |
| 1230 | int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT; |
| 1231 | int bound_end = (((end + TARGET_CHAR_BIT - 1) |
| 1232 | / TARGET_CHAR_BIT) |
| 1233 | * TARGET_CHAR_BIT); |
| 1234 | |
| 1235 | /* current bit offset within the structure */ |
| 1236 | int offset; |
| 1237 | |
| 1238 | /* The index in ops of the opcode we're considering. */ |
| 1239 | int op; |
| 1240 | |
| 1241 | /* The number of fragments we generated in the process. Probably |
| 1242 | equal to the number of `one' bits in bytesize, but who cares? */ |
| 1243 | int fragment_count; |
| 1244 | |
| 1245 | /* Dereference any typedefs. */ |
| 1246 | type = check_typedef (type); |
| 1247 | |
| 1248 | /* Can we fetch the number of bits requested at all? */ |
| 1249 | if ((end - start) > ((1 << num_ops) * 8)) |
| 1250 | internal_error (__FILE__, __LINE__, |
| 1251 | _("gen_bitfield_ref: bitfield too wide")); |
| 1252 | |
| 1253 | /* Note that we know here that we only need to try each opcode once. |
| 1254 | That may not be true on machines with weird byte sizes. */ |
| 1255 | offset = bound_start; |
| 1256 | fragment_count = 0; |
| 1257 | for (op = num_ops - 1; op >= 0; op--) |
| 1258 | { |
| 1259 | /* number of bits that ops[op] would fetch */ |
| 1260 | int op_size = 8 << op; |
| 1261 | |
| 1262 | /* The stack at this point, from bottom to top, contains zero or |
| 1263 | more fragments, then the address. */ |
| 1264 | |
| 1265 | /* Does this fetch fit within the bitfield? */ |
| 1266 | if (offset + op_size <= bound_end) |
| 1267 | { |
| 1268 | /* Is this the last fragment? */ |
| 1269 | int last_frag = (offset + op_size == bound_end); |
| 1270 | |
| 1271 | if (!last_frag) |
| 1272 | ax_simple (ax, aop_dup); /* keep a copy of the address */ |
| 1273 | |
| 1274 | /* Add the offset. */ |
| 1275 | gen_offset (ax, offset / TARGET_CHAR_BIT); |
| 1276 | |
| 1277 | if (trace_kludge) |
| 1278 | { |
| 1279 | /* Record the area of memory we're about to fetch. */ |
| 1280 | ax_trace_quick (ax, op_size / TARGET_CHAR_BIT); |
| 1281 | } |
| 1282 | |
| 1283 | /* Perform the fetch. */ |
| 1284 | ax_simple (ax, ops[op]); |
| 1285 | |
| 1286 | /* Shift the bits we have to their proper position. |
| 1287 | gen_left_shift will generate right shifts when the operand |
| 1288 | is negative. |
| 1289 | |
| 1290 | A big-endian field diagram to ponder: |
| 1291 | byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7 |
| 1292 | +------++------++------++------++------++------++------++------+ |
| 1293 | xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx |
| 1294 | ^ ^ ^ ^ |
| 1295 | bit number 16 32 48 53 |
| 1296 | These are bit numbers as supplied by GDB. Note that the |
| 1297 | bit numbers run from right to left once you've fetched the |
| 1298 | value! |
| 1299 | |
| 1300 | A little-endian field diagram to ponder: |
| 1301 | byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0 |
| 1302 | +------++------++------++------++------++------++------++------+ |
| 1303 | xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx |
| 1304 | ^ ^ ^ ^ ^ |
| 1305 | bit number 48 32 16 4 0 |
| 1306 | |
| 1307 | In both cases, the most significant end is on the left |
| 1308 | (i.e. normal numeric writing order), which means that you |
| 1309 | don't go crazy thinking about `left' and `right' shifts. |
| 1310 | |
| 1311 | We don't have to worry about masking yet: |
| 1312 | - If they contain garbage off the least significant end, then we |
| 1313 | must be looking at the low end of the field, and the right |
| 1314 | shift will wipe them out. |
| 1315 | - If they contain garbage off the most significant end, then we |
| 1316 | must be looking at the most significant end of the word, and |
| 1317 | the sign/zero extension will wipe them out. |
| 1318 | - If we're in the interior of the word, then there is no garbage |
| 1319 | on either end, because the ref operators zero-extend. */ |
| 1320 | if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) |
| 1321 | gen_left_shift (ax, end - (offset + op_size)); |
| 1322 | else |
| 1323 | gen_left_shift (ax, offset - start); |
| 1324 | |
| 1325 | if (!last_frag) |
| 1326 | /* Bring the copy of the address up to the top. */ |
| 1327 | ax_simple (ax, aop_swap); |
| 1328 | |
| 1329 | offset += op_size; |
| 1330 | fragment_count++; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | /* Generate enough bitwise `or' operations to combine all the |
| 1335 | fragments we left on the stack. */ |
| 1336 | while (fragment_count-- > 1) |
| 1337 | ax_simple (ax, aop_bit_or); |
| 1338 | |
| 1339 | /* Sign- or zero-extend the value as appropriate. */ |
| 1340 | ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start)); |
| 1341 | |
| 1342 | /* This is *not* an lvalue. Ugh. */ |
| 1343 | value->kind = axs_rvalue; |
| 1344 | value->type = type; |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | /* Generate code to reference the member named FIELD of a structure or |
| 1349 | union. The top of the stack, as described by VALUE, should have |
| 1350 | type (pointer to a)* struct/union. OPERATOR_NAME is the name of |
| 1351 | the operator being compiled, and OPERAND_NAME is the kind of thing |
| 1352 | it operates on; we use them in error messages. */ |
| 1353 | static void |
| 1354 | gen_struct_ref (struct agent_expr *ax, struct axs_value *value, char *field, |
| 1355 | char *operator_name, char *operand_name) |
| 1356 | { |
| 1357 | struct type *type; |
| 1358 | int i; |
| 1359 | |
| 1360 | /* Follow pointers until we reach a non-pointer. These aren't the C |
| 1361 | semantics, but they're what the normal GDB evaluator does, so we |
| 1362 | should at least be consistent. */ |
| 1363 | while (TYPE_CODE (value->type) == TYPE_CODE_PTR) |
| 1364 | { |
| 1365 | gen_usual_unary (ax, value); |
| 1366 | gen_deref (ax, value); |
| 1367 | } |
| 1368 | type = check_typedef (value->type); |
| 1369 | |
| 1370 | /* This must yield a structure or a union. */ |
| 1371 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT |
| 1372 | && TYPE_CODE (type) != TYPE_CODE_UNION) |
| 1373 | error (_("The left operand of `%s' is not a %s."), |
| 1374 | operator_name, operand_name); |
| 1375 | |
| 1376 | /* And it must be in memory; we don't deal with structure rvalues, |
| 1377 | or structures living in registers. */ |
| 1378 | if (value->kind != axs_lvalue_memory) |
| 1379 | error (_("Structure does not live in memory.")); |
| 1380 | |
| 1381 | i = find_field (type, field); |
| 1382 | |
| 1383 | /* Is this a bitfield? */ |
| 1384 | if (TYPE_FIELD_PACKED (type, i)) |
| 1385 | gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i), |
| 1386 | TYPE_FIELD_BITPOS (type, i), |
| 1387 | (TYPE_FIELD_BITPOS (type, i) |
| 1388 | + TYPE_FIELD_BITSIZE (type, i))); |
| 1389 | else |
| 1390 | { |
| 1391 | gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT); |
| 1392 | value->kind = axs_lvalue_memory; |
| 1393 | value->type = TYPE_FIELD_TYPE (type, i); |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | /* Generate code for GDB's magical `repeat' operator. |
| 1399 | LVALUE @ INT creates an array INT elements long, and whose elements |
| 1400 | have the same type as LVALUE, located in memory so that LVALUE is |
| 1401 | its first element. For example, argv[0]@argc gives you the array |
| 1402 | of command-line arguments. |
| 1403 | |
| 1404 | Unfortunately, because we have to know the types before we actually |
| 1405 | have a value for the expression, we can't implement this perfectly |
| 1406 | without changing the type system, having values that occupy two |
| 1407 | stack slots, doing weird things with sizeof, etc. So we require |
| 1408 | the right operand to be a constant expression. */ |
| 1409 | static void |
| 1410 | gen_repeat (union exp_element **pc, struct agent_expr *ax, |
| 1411 | struct axs_value *value) |
| 1412 | { |
| 1413 | struct axs_value value1; |
| 1414 | /* We don't want to turn this into an rvalue, so no conversions |
| 1415 | here. */ |
| 1416 | gen_expr (pc, ax, &value1); |
| 1417 | if (value1.kind != axs_lvalue_memory) |
| 1418 | error (_("Left operand of `@' must be an object in memory.")); |
| 1419 | |
| 1420 | /* Evaluate the length; it had better be a constant. */ |
| 1421 | { |
| 1422 | struct value *v = const_expr (pc); |
| 1423 | int length; |
| 1424 | |
| 1425 | if (!v) |
| 1426 | error (_("Right operand of `@' must be a constant, in agent expressions.")); |
| 1427 | if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT) |
| 1428 | error (_("Right operand of `@' must be an integer.")); |
| 1429 | length = value_as_long (v); |
| 1430 | if (length <= 0) |
| 1431 | error (_("Right operand of `@' must be positive.")); |
| 1432 | |
| 1433 | /* The top of the stack is already the address of the object, so |
| 1434 | all we need to do is frob the type of the lvalue. */ |
| 1435 | { |
| 1436 | /* FIXME-type-allocation: need a way to free this type when we are |
| 1437 | done with it. */ |
| 1438 | struct type *range |
| 1439 | = create_range_type (0, builtin_type_int, 0, length - 1); |
| 1440 | struct type *array = create_array_type (0, value1.type, range); |
| 1441 | |
| 1442 | value->kind = axs_lvalue_memory; |
| 1443 | value->type = array; |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | /* Emit code for the `sizeof' operator. |
| 1450 | *PC should point at the start of the operand expression; we advance it |
| 1451 | to the first instruction after the operand. */ |
| 1452 | static void |
| 1453 | gen_sizeof (union exp_element **pc, struct agent_expr *ax, |
| 1454 | struct axs_value *value) |
| 1455 | { |
| 1456 | /* We don't care about the value of the operand expression; we only |
| 1457 | care about its type. However, in the current arrangement, the |
| 1458 | only way to find an expression's type is to generate code for it. |
| 1459 | So we generate code for the operand, and then throw it away, |
| 1460 | replacing it with code that simply pushes its size. */ |
| 1461 | int start = ax->len; |
| 1462 | gen_expr (pc, ax, value); |
| 1463 | |
| 1464 | /* Throw away the code we just generated. */ |
| 1465 | ax->len = start; |
| 1466 | |
| 1467 | ax_const_l (ax, TYPE_LENGTH (value->type)); |
| 1468 | value->kind = axs_rvalue; |
| 1469 | value->type = builtin_type_int; |
| 1470 | } |
| 1471 | \f |
| 1472 | |
| 1473 | /* Generating bytecode from GDB expressions: general recursive thingy */ |
| 1474 | |
| 1475 | /* XXX: i18n */ |
| 1476 | /* A gen_expr function written by a Gen-X'er guy. |
| 1477 | Append code for the subexpression of EXPR starting at *POS_P to AX. */ |
| 1478 | static void |
| 1479 | gen_expr (union exp_element **pc, struct agent_expr *ax, |
| 1480 | struct axs_value *value) |
| 1481 | { |
| 1482 | /* Used to hold the descriptions of operand expressions. */ |
| 1483 | struct axs_value value1, value2; |
| 1484 | enum exp_opcode op = (*pc)[0].opcode; |
| 1485 | |
| 1486 | /* If we're looking at a constant expression, just push its value. */ |
| 1487 | { |
| 1488 | struct value *v = maybe_const_expr (pc); |
| 1489 | |
| 1490 | if (v) |
| 1491 | { |
| 1492 | ax_const_l (ax, value_as_long (v)); |
| 1493 | value->kind = axs_rvalue; |
| 1494 | value->type = check_typedef (value_type (v)); |
| 1495 | return; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | /* Otherwise, go ahead and generate code for it. */ |
| 1500 | switch (op) |
| 1501 | { |
| 1502 | /* Binary arithmetic operators. */ |
| 1503 | case BINOP_ADD: |
| 1504 | case BINOP_SUB: |
| 1505 | case BINOP_MUL: |
| 1506 | case BINOP_DIV: |
| 1507 | case BINOP_REM: |
| 1508 | case BINOP_SUBSCRIPT: |
| 1509 | case BINOP_BITWISE_AND: |
| 1510 | case BINOP_BITWISE_IOR: |
| 1511 | case BINOP_BITWISE_XOR: |
| 1512 | (*pc)++; |
| 1513 | gen_expr (pc, ax, &value1); |
| 1514 | gen_usual_unary (ax, &value1); |
| 1515 | gen_expr (pc, ax, &value2); |
| 1516 | gen_usual_unary (ax, &value2); |
| 1517 | gen_usual_arithmetic (ax, &value1, &value2); |
| 1518 | switch (op) |
| 1519 | { |
| 1520 | case BINOP_ADD: |
| 1521 | gen_add (ax, value, &value1, &value2, "addition"); |
| 1522 | break; |
| 1523 | case BINOP_SUB: |
| 1524 | gen_sub (ax, value, &value1, &value2); |
| 1525 | break; |
| 1526 | case BINOP_MUL: |
| 1527 | gen_binop (ax, value, &value1, &value2, |
| 1528 | aop_mul, aop_mul, 1, "multiplication"); |
| 1529 | break; |
| 1530 | case BINOP_DIV: |
| 1531 | gen_binop (ax, value, &value1, &value2, |
| 1532 | aop_div_signed, aop_div_unsigned, 1, "division"); |
| 1533 | break; |
| 1534 | case BINOP_REM: |
| 1535 | gen_binop (ax, value, &value1, &value2, |
| 1536 | aop_rem_signed, aop_rem_unsigned, 1, "remainder"); |
| 1537 | break; |
| 1538 | case BINOP_SUBSCRIPT: |
| 1539 | gen_add (ax, value, &value1, &value2, "array subscripting"); |
| 1540 | if (TYPE_CODE (value->type) != TYPE_CODE_PTR) |
| 1541 | error (_("Invalid combination of types in array subscripting.")); |
| 1542 | gen_deref (ax, value); |
| 1543 | break; |
| 1544 | case BINOP_BITWISE_AND: |
| 1545 | gen_binop (ax, value, &value1, &value2, |
| 1546 | aop_bit_and, aop_bit_and, 0, "bitwise and"); |
| 1547 | break; |
| 1548 | |
| 1549 | case BINOP_BITWISE_IOR: |
| 1550 | gen_binop (ax, value, &value1, &value2, |
| 1551 | aop_bit_or, aop_bit_or, 0, "bitwise or"); |
| 1552 | break; |
| 1553 | |
| 1554 | case BINOP_BITWISE_XOR: |
| 1555 | gen_binop (ax, value, &value1, &value2, |
| 1556 | aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or"); |
| 1557 | break; |
| 1558 | |
| 1559 | default: |
| 1560 | /* We should only list operators in the outer case statement |
| 1561 | that we actually handle in the inner case statement. */ |
| 1562 | internal_error (__FILE__, __LINE__, |
| 1563 | _("gen_expr: op case sets don't match")); |
| 1564 | } |
| 1565 | break; |
| 1566 | |
| 1567 | /* Note that we need to be a little subtle about generating code |
| 1568 | for comma. In C, we can do some optimizations here because |
| 1569 | we know the left operand is only being evaluated for effect. |
| 1570 | However, if the tracing kludge is in effect, then we always |
| 1571 | need to evaluate the left hand side fully, so that all the |
| 1572 | variables it mentions get traced. */ |
| 1573 | case BINOP_COMMA: |
| 1574 | (*pc)++; |
| 1575 | gen_expr (pc, ax, &value1); |
| 1576 | /* Don't just dispose of the left operand. We might be tracing, |
| 1577 | in which case we want to emit code to trace it if it's an |
| 1578 | lvalue. */ |
| 1579 | gen_traced_pop (ax, &value1); |
| 1580 | gen_expr (pc, ax, value); |
| 1581 | /* It's the consumer's responsibility to trace the right operand. */ |
| 1582 | break; |
| 1583 | |
| 1584 | case OP_LONG: /* some integer constant */ |
| 1585 | { |
| 1586 | struct type *type = (*pc)[1].type; |
| 1587 | LONGEST k = (*pc)[2].longconst; |
| 1588 | (*pc) += 4; |
| 1589 | gen_int_literal (ax, value, k, type); |
| 1590 | } |
| 1591 | break; |
| 1592 | |
| 1593 | case OP_VAR_VALUE: |
| 1594 | gen_var_ref (ax, value, (*pc)[2].symbol); |
| 1595 | (*pc) += 4; |
| 1596 | break; |
| 1597 | |
| 1598 | case OP_REGISTER: |
| 1599 | { |
| 1600 | int reg = (int) (*pc)[1].longconst; |
| 1601 | (*pc) += 3; |
| 1602 | value->kind = axs_lvalue_register; |
| 1603 | value->u.reg = reg; |
| 1604 | value->type = register_type (current_gdbarch, reg); |
| 1605 | } |
| 1606 | break; |
| 1607 | |
| 1608 | case OP_INTERNALVAR: |
| 1609 | error (_("GDB agent expressions cannot use convenience variables.")); |
| 1610 | |
| 1611 | /* Weirdo operator: see comments for gen_repeat for details. */ |
| 1612 | case BINOP_REPEAT: |
| 1613 | /* Note that gen_repeat handles its own argument evaluation. */ |
| 1614 | (*pc)++; |
| 1615 | gen_repeat (pc, ax, value); |
| 1616 | break; |
| 1617 | |
| 1618 | case UNOP_CAST: |
| 1619 | { |
| 1620 | struct type *type = (*pc)[1].type; |
| 1621 | (*pc) += 3; |
| 1622 | gen_expr (pc, ax, value); |
| 1623 | gen_cast (ax, value, type); |
| 1624 | } |
| 1625 | break; |
| 1626 | |
| 1627 | case UNOP_MEMVAL: |
| 1628 | { |
| 1629 | struct type *type = check_typedef ((*pc)[1].type); |
| 1630 | (*pc) += 3; |
| 1631 | gen_expr (pc, ax, value); |
| 1632 | /* I'm not sure I understand UNOP_MEMVAL entirely. I think |
| 1633 | it's just a hack for dealing with minsyms; you take some |
| 1634 | integer constant, pretend it's the address of an lvalue of |
| 1635 | the given type, and dereference it. */ |
| 1636 | if (value->kind != axs_rvalue) |
| 1637 | /* This would be weird. */ |
| 1638 | internal_error (__FILE__, __LINE__, |
| 1639 | _("gen_expr: OP_MEMVAL operand isn't an rvalue???")); |
| 1640 | value->type = type; |
| 1641 | value->kind = axs_lvalue_memory; |
| 1642 | } |
| 1643 | break; |
| 1644 | |
| 1645 | case UNOP_PLUS: |
| 1646 | (*pc)++; |
| 1647 | /* + FOO is equivalent to 0 + FOO, which can be optimized. */ |
| 1648 | gen_expr (pc, ax, value); |
| 1649 | gen_usual_unary (ax, value); |
| 1650 | break; |
| 1651 | |
| 1652 | case UNOP_NEG: |
| 1653 | (*pc)++; |
| 1654 | /* -FOO is equivalent to 0 - FOO. */ |
| 1655 | gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int); |
| 1656 | gen_usual_unary (ax, &value1); /* shouldn't do much */ |
| 1657 | gen_expr (pc, ax, &value2); |
| 1658 | gen_usual_unary (ax, &value2); |
| 1659 | gen_usual_arithmetic (ax, &value1, &value2); |
| 1660 | gen_sub (ax, value, &value1, &value2); |
| 1661 | break; |
| 1662 | |
| 1663 | case UNOP_LOGICAL_NOT: |
| 1664 | (*pc)++; |
| 1665 | gen_expr (pc, ax, value); |
| 1666 | gen_logical_not (ax, value); |
| 1667 | break; |
| 1668 | |
| 1669 | case UNOP_COMPLEMENT: |
| 1670 | (*pc)++; |
| 1671 | gen_expr (pc, ax, value); |
| 1672 | gen_complement (ax, value); |
| 1673 | break; |
| 1674 | |
| 1675 | case UNOP_IND: |
| 1676 | (*pc)++; |
| 1677 | gen_expr (pc, ax, value); |
| 1678 | gen_usual_unary (ax, value); |
| 1679 | if (TYPE_CODE (value->type) != TYPE_CODE_PTR) |
| 1680 | error (_("Argument of unary `*' is not a pointer.")); |
| 1681 | gen_deref (ax, value); |
| 1682 | break; |
| 1683 | |
| 1684 | case UNOP_ADDR: |
| 1685 | (*pc)++; |
| 1686 | gen_expr (pc, ax, value); |
| 1687 | gen_address_of (ax, value); |
| 1688 | break; |
| 1689 | |
| 1690 | case UNOP_SIZEOF: |
| 1691 | (*pc)++; |
| 1692 | /* Notice that gen_sizeof handles its own operand, unlike most |
| 1693 | of the other unary operator functions. This is because we |
| 1694 | have to throw away the code we generate. */ |
| 1695 | gen_sizeof (pc, ax, value); |
| 1696 | break; |
| 1697 | |
| 1698 | case STRUCTOP_STRUCT: |
| 1699 | case STRUCTOP_PTR: |
| 1700 | { |
| 1701 | int length = (*pc)[1].longconst; |
| 1702 | char *name = &(*pc)[2].string; |
| 1703 | |
| 1704 | (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1); |
| 1705 | gen_expr (pc, ax, value); |
| 1706 | if (op == STRUCTOP_STRUCT) |
| 1707 | gen_struct_ref (ax, value, name, ".", "structure or union"); |
| 1708 | else if (op == STRUCTOP_PTR) |
| 1709 | gen_struct_ref (ax, value, name, "->", |
| 1710 | "pointer to a structure or union"); |
| 1711 | else |
| 1712 | /* If this `if' chain doesn't handle it, then the case list |
| 1713 | shouldn't mention it, and we shouldn't be here. */ |
| 1714 | internal_error (__FILE__, __LINE__, |
| 1715 | _("gen_expr: unhandled struct case")); |
| 1716 | } |
| 1717 | break; |
| 1718 | |
| 1719 | case OP_TYPE: |
| 1720 | error (_("Attempt to use a type name as an expression.")); |
| 1721 | |
| 1722 | default: |
| 1723 | error (_("Unsupported operator in expression.")); |
| 1724 | } |
| 1725 | } |
| 1726 | \f |
| 1727 | |
| 1728 | |
| 1729 | /* Generating bytecode from GDB expressions: driver */ |
| 1730 | |
| 1731 | /* Given a GDB expression EXPR, produce a string of agent bytecode |
| 1732 | which computes its value. Return the agent expression, and set |
| 1733 | *VALUE to describe its type, and whether it's an lvalue or rvalue. */ |
| 1734 | struct agent_expr * |
| 1735 | expr_to_agent (struct expression *expr, struct axs_value *value) |
| 1736 | { |
| 1737 | struct cleanup *old_chain = 0; |
| 1738 | struct agent_expr *ax = new_agent_expr (0); |
| 1739 | union exp_element *pc; |
| 1740 | |
| 1741 | old_chain = make_cleanup_free_agent_expr (ax); |
| 1742 | |
| 1743 | pc = expr->elts; |
| 1744 | trace_kludge = 0; |
| 1745 | gen_expr (&pc, ax, value); |
| 1746 | |
| 1747 | /* We have successfully built the agent expr, so cancel the cleanup |
| 1748 | request. If we add more cleanups that we always want done, this |
| 1749 | will have to get more complicated. */ |
| 1750 | discard_cleanups (old_chain); |
| 1751 | return ax; |
| 1752 | } |
| 1753 | |
| 1754 | |
| 1755 | #if 0 /* not used */ |
| 1756 | /* Given a GDB expression EXPR denoting an lvalue in memory, produce a |
| 1757 | string of agent bytecode which will leave its address and size on |
| 1758 | the top of stack. Return the agent expression. |
| 1759 | |
| 1760 | Not sure this function is useful at all. */ |
| 1761 | struct agent_expr * |
| 1762 | expr_to_address_and_size (struct expression *expr) |
| 1763 | { |
| 1764 | struct axs_value value; |
| 1765 | struct agent_expr *ax = expr_to_agent (expr, &value); |
| 1766 | |
| 1767 | /* Complain if the result is not a memory lvalue. */ |
| 1768 | if (value.kind != axs_lvalue_memory) |
| 1769 | { |
| 1770 | free_agent_expr (ax); |
| 1771 | error (_("Expression does not denote an object in memory.")); |
| 1772 | } |
| 1773 | |
| 1774 | /* Push the object's size on the stack. */ |
| 1775 | ax_const_l (ax, TYPE_LENGTH (value.type)); |
| 1776 | |
| 1777 | return ax; |
| 1778 | } |
| 1779 | #endif |
| 1780 | |
| 1781 | /* Given a GDB expression EXPR, return bytecode to trace its value. |
| 1782 | The result will use the `trace' and `trace_quick' bytecodes to |
| 1783 | record the value of all memory touched by the expression. The |
| 1784 | caller can then use the ax_reqs function to discover which |
| 1785 | registers it relies upon. */ |
| 1786 | struct agent_expr * |
| 1787 | gen_trace_for_expr (CORE_ADDR scope, struct expression *expr) |
| 1788 | { |
| 1789 | struct cleanup *old_chain = 0; |
| 1790 | struct agent_expr *ax = new_agent_expr (scope); |
| 1791 | union exp_element *pc; |
| 1792 | struct axs_value value; |
| 1793 | |
| 1794 | old_chain = make_cleanup_free_agent_expr (ax); |
| 1795 | |
| 1796 | pc = expr->elts; |
| 1797 | trace_kludge = 1; |
| 1798 | gen_expr (&pc, ax, &value); |
| 1799 | |
| 1800 | /* Make sure we record the final object, and get rid of it. */ |
| 1801 | gen_traced_pop (ax, &value); |
| 1802 | |
| 1803 | /* Oh, and terminate. */ |
| 1804 | ax_simple (ax, aop_end); |
| 1805 | |
| 1806 | /* We have successfully built the agent expr, so cancel the cleanup |
| 1807 | request. If we add more cleanups that we always want done, this |
| 1808 | will have to get more complicated. */ |
| 1809 | discard_cleanups (old_chain); |
| 1810 | return ax; |
| 1811 | } |
| 1812 | |
| 1813 | static void |
| 1814 | agent_command (char *exp, int from_tty) |
| 1815 | { |
| 1816 | struct cleanup *old_chain = 0; |
| 1817 | struct expression *expr; |
| 1818 | struct agent_expr *agent; |
| 1819 | struct frame_info *fi = get_current_frame (); /* need current scope */ |
| 1820 | |
| 1821 | /* We don't deal with overlay debugging at the moment. We need to |
| 1822 | think more carefully about this. If you copy this code into |
| 1823 | another command, change the error message; the user shouldn't |
| 1824 | have to know anything about agent expressions. */ |
| 1825 | if (overlay_debugging) |
| 1826 | error (_("GDB can't do agent expression translation with overlays.")); |
| 1827 | |
| 1828 | if (exp == 0) |
| 1829 | error_no_arg (_("expression to translate")); |
| 1830 | |
| 1831 | expr = parse_expression (exp); |
| 1832 | old_chain = make_cleanup (free_current_contents, &expr); |
| 1833 | agent = gen_trace_for_expr (get_frame_pc (fi), expr); |
| 1834 | make_cleanup_free_agent_expr (agent); |
| 1835 | ax_print (gdb_stdout, agent); |
| 1836 | |
| 1837 | /* It would be nice to call ax_reqs here to gather some general info |
| 1838 | about the expression, and then print out the result. */ |
| 1839 | |
| 1840 | do_cleanups (old_chain); |
| 1841 | dont_repeat (); |
| 1842 | } |
| 1843 | \f |
| 1844 | |
| 1845 | /* Initialization code. */ |
| 1846 | |
| 1847 | void _initialize_ax_gdb (void); |
| 1848 | void |
| 1849 | _initialize_ax_gdb (void) |
| 1850 | { |
| 1851 | add_cmd ("agent", class_maintenance, agent_command, |
| 1852 | _("Translate an expression into remote agent bytecode."), |
| 1853 | &maintenancelist); |
| 1854 | } |