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