1 /* GDB-specific functions for operating on agent expressions. 2 3 Copyright (C) 1998-2023 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "symtab.h" 22 #include "symfile.h" 23 #include "gdbtypes.h" 24 #include "language.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 "block.h" 34 #include "regcache.h" 35 #include "user-regs.h" 36 #include "dictionary.h" 37 #include "breakpoint.h" 38 #include "tracepoint.h" 39 #include "cp-support.h" 40 #include "arch-utils.h" 41 #include "cli/cli-utils.h" 42 #include "linespec.h" 43 #include "location.h" 44 #include "objfiles.h" 45 #include "typeprint.h" 46 #include "valprint.h" 47 #include "c-lang.h" 48 #include "expop.h" 49 50 #include "gdbsupport/format.h" 51 52 /* To make sense of this file, you should read doc/agentexpr.texi. 53 Then look at the types and enums in ax-gdb.h. For the code itself, 54 look at gen_expr, towards the bottom; that's the main function that 55 looks at the GDB expressions and calls everything else to generate 56 code. 57 58 I'm beginning to wonder whether it wouldn't be nicer to internally 59 generate trees, with types, and then spit out the bytecode in 60 linear form afterwards; we could generate fewer `swap', `ext', and 61 `zero_ext' bytecodes that way; it would make good constant folding 62 easier, too. But at the moment, I think we should be willing to 63 pay for the simplicity of this code with less-than-optimal bytecode 64 strings. 65 66 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */ 67 68 69 70 /* Prototypes for local functions. */ 71 72 /* There's a standard order to the arguments of these functions: 73 struct agent_expr * --- agent expression buffer to generate code into 74 struct axs_value * --- describes value left on top of stack */ 75 76 static void gen_traced_pop (struct agent_expr *, struct axs_value *); 77 78 static void gen_sign_extend (struct agent_expr *, struct type *); 79 static void gen_extend (struct agent_expr *, struct type *); 80 static void gen_fetch (struct agent_expr *, struct type *); 81 static void gen_left_shift (struct agent_expr *, int); 82 83 84 static void gen_frame_args_address (struct agent_expr *); 85 static void gen_frame_locals_address (struct agent_expr *); 86 static void gen_offset (struct agent_expr *ax, int offset); 87 static void gen_sym_offset (struct agent_expr *, struct symbol *); 88 static void gen_var_ref (struct agent_expr *ax, struct axs_value *value, 89 struct symbol *var); 90 91 92 static void gen_int_literal (struct agent_expr *ax, 93 struct axs_value *value, 94 LONGEST k, struct type *type); 95 96 static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value); 97 static int type_wider_than (struct type *type1, struct type *type2); 98 static struct type *max_type (struct type *type1, struct type *type2); 99 static void gen_conversion (struct agent_expr *ax, 100 struct type *from, struct type *to); 101 static int is_nontrivial_conversion (struct type *from, struct type *to); 102 static void gen_usual_arithmetic (struct agent_expr *ax, 103 struct axs_value *value1, 104 struct axs_value *value2); 105 static void gen_integral_promotions (struct agent_expr *ax, 106 struct axs_value *value); 107 static void gen_cast (struct agent_expr *ax, 108 struct axs_value *value, struct type *type); 109 static void gen_scale (struct agent_expr *ax, 110 enum agent_op op, struct type *type); 111 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value, 112 struct axs_value *value1, struct axs_value *value2); 113 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value, 114 struct axs_value *value1, struct axs_value *value2); 115 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value, 116 struct axs_value *value1, struct axs_value *value2, 117 struct type *result_type); 118 static void gen_binop (struct agent_expr *ax, 119 struct axs_value *value, 120 struct axs_value *value1, 121 struct axs_value *value2, 122 enum agent_op op, 123 enum agent_op op_unsigned, int may_carry, 124 const char *name); 125 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value, 126 struct type *result_type); 127 static void gen_complement (struct agent_expr *ax, struct axs_value *value); 128 static void gen_deref (struct axs_value *); 129 static void gen_address_of (struct axs_value *); 130 static void gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value, 131 struct type *type, int start, int end); 132 static void gen_primitive_field (struct agent_expr *ax, 133 struct axs_value *value, 134 int offset, int fieldno, struct type *type); 135 static int gen_struct_ref_recursive (struct agent_expr *ax, 136 struct axs_value *value, 137 const char *field, int offset, 138 struct type *type); 139 static void gen_struct_ref (struct agent_expr *ax, 140 struct axs_value *value, 141 const char *field, 142 const char *operator_name, 143 const char *operand_name); 144 static void gen_static_field (struct agent_expr *ax, struct axs_value *value, 145 struct type *type, int fieldno); 146 static void gen_expr_binop_rest (struct expression *exp, 147 enum exp_opcode op, 148 struct agent_expr *ax, 149 struct axs_value *value, 150 struct axs_value *value1, 151 struct axs_value *value2); 152 153 154 155 /* Generating bytecode from GDB expressions: general assumptions */ 156 157 /* Here are a few general assumptions made throughout the code; if you 158 want to make a change that contradicts one of these, then you'd 159 better scan things pretty thoroughly. 160 161 - We assume that all values occupy one stack element. For example, 162 sometimes we'll swap to get at the left argument to a binary 163 operator. If we decide that void values should occupy no stack 164 elements, or that synthetic arrays (whose size is determined at 165 run time, created by the `@' operator) should occupy two stack 166 elements (address and length), then this will cause trouble. 167 168 - We assume the stack elements are infinitely wide, and that we 169 don't have to worry what happens if the user requests an 170 operation that is wider than the actual interpreter's stack. 171 That is, it's up to the interpreter to handle directly all the 172 integer widths the user has access to. (Woe betide the language 173 with bignums!) 174 175 - We don't support side effects. Thus, we don't have to worry about 176 GCC's generalized lvalues, function calls, etc. 177 178 - We don't support floating point. Many places where we switch on 179 some type don't bother to include cases for floating point; there 180 may be even more subtle ways this assumption exists. For 181 example, the arguments to % must be integers. 182 183 - We assume all subexpressions have a static, unchanging type. If 184 we tried to support convenience variables, this would be a 185 problem. 186 187 - All values on the stack should always be fully zero- or 188 sign-extended. 189 190 (I wasn't sure whether to choose this or its opposite --- that 191 only addresses are assumed extended --- but it turns out that 192 neither convention completely eliminates spurious extend 193 operations (if everything is always extended, then you have to 194 extend after add, because it could overflow; if nothing is 195 extended, then you end up producing extends whenever you change 196 sizes), and this is simpler.) */ 197 198 199 /* Scan for all static fields in the given class, including any base 200 classes, and generate tracing bytecodes for each. */ 201 202 static void 203 gen_trace_static_fields (struct agent_expr *ax, 204 struct type *type) 205 { 206 int i, nbases = TYPE_N_BASECLASSES (type); 207 struct axs_value value; 208 209 type = check_typedef (type); 210 211 for (i = type->num_fields () - 1; i >= nbases; i--) 212 { 213 if (field_is_static (&type->field (i))) 214 { 215 gen_static_field (ax, &value, type, i); 216 if (value.optimized_out) 217 continue; 218 switch (value.kind) 219 { 220 case axs_lvalue_memory: 221 { 222 /* Initialize the TYPE_LENGTH if it is a typedef. */ 223 check_typedef (value.type); 224 ax_const_l (ax, value.type->length ()); 225 ax_simple (ax, aop_trace); 226 } 227 break; 228 229 case axs_lvalue_register: 230 /* We don't actually need the register's value to be pushed, 231 just note that we need it to be collected. */ 232 ax_reg_mask (ax, value.u.reg); 233 234 default: 235 break; 236 } 237 } 238 } 239 240 /* Now scan through base classes recursively. */ 241 for (i = 0; i < nbases; i++) 242 { 243 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i)); 244 245 gen_trace_static_fields (ax, basetype); 246 } 247 } 248 249 /* Trace the lvalue on the stack, if it needs it. In either case, pop 250 the value. Useful on the left side of a comma, and at the end of 251 an expression being used for tracing. */ 252 static void 253 gen_traced_pop (struct agent_expr *ax, struct axs_value *value) 254 { 255 int string_trace = 0; 256 if (ax->trace_string 257 && value->type->code () == TYPE_CODE_PTR 258 && c_textual_element_type (check_typedef (value->type->target_type ()), 259 's')) 260 string_trace = 1; 261 262 if (ax->tracing) 263 switch (value->kind) 264 { 265 case axs_rvalue: 266 if (string_trace) 267 { 268 ax_const_l (ax, ax->trace_string); 269 ax_simple (ax, aop_tracenz); 270 } 271 else 272 /* We don't trace rvalues, just the lvalues necessary to 273 produce them. So just dispose of this value. */ 274 ax_simple (ax, aop_pop); 275 break; 276 277 case axs_lvalue_memory: 278 { 279 /* Initialize the TYPE_LENGTH if it is a typedef. */ 280 check_typedef (value->type); 281 282 if (string_trace) 283 { 284 gen_fetch (ax, value->type); 285 ax_const_l (ax, ax->trace_string); 286 ax_simple (ax, aop_tracenz); 287 } 288 else 289 { 290 /* There's no point in trying to use a trace_quick bytecode 291 here, since "trace_quick SIZE pop" is three bytes, whereas 292 "const8 SIZE trace" is also three bytes, does the same 293 thing, and the simplest code which generates that will also 294 work correctly for objects with large sizes. */ 295 ax_const_l (ax, value->type->length ()); 296 ax_simple (ax, aop_trace); 297 } 298 } 299 break; 300 301 case axs_lvalue_register: 302 /* We don't actually need the register's value to be on the 303 stack, and the target will get heartburn if the register is 304 larger than will fit in a stack, so just mark it for 305 collection and be done with it. */ 306 ax_reg_mask (ax, value->u.reg); 307 308 /* But if the register points to a string, assume the value 309 will fit on the stack and push it anyway. */ 310 if (string_trace) 311 { 312 ax_reg (ax, value->u.reg); 313 ax_const_l (ax, ax->trace_string); 314 ax_simple (ax, aop_tracenz); 315 } 316 break; 317 } 318 else 319 /* If we're not tracing, just pop the value. */ 320 ax_simple (ax, aop_pop); 321 322 /* To trace C++ classes with static fields stored elsewhere. */ 323 if (ax->tracing 324 && (value->type->code () == TYPE_CODE_STRUCT 325 || value->type->code () == TYPE_CODE_UNION)) 326 gen_trace_static_fields (ax, value->type); 327 } 328 329 330 331 /* Generating bytecode from GDB expressions: helper functions */ 332 333 /* Assume that the lower bits of the top of the stack is a value of 334 type TYPE, and the upper bits are zero. Sign-extend if necessary. */ 335 static void 336 gen_sign_extend (struct agent_expr *ax, struct type *type) 337 { 338 /* Do we need to sign-extend this? */ 339 if (!type->is_unsigned ()) 340 ax_ext (ax, type->length () * TARGET_CHAR_BIT); 341 } 342 343 344 /* Assume the lower bits of the top of the stack hold a value of type 345 TYPE, and the upper bits are garbage. Sign-extend or truncate as 346 needed. */ 347 static void 348 gen_extend (struct agent_expr *ax, struct type *type) 349 { 350 int bits = type->length () * TARGET_CHAR_BIT; 351 352 /* I just had to. */ 353 ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, bits)); 354 } 355 356 357 /* Assume that the top of the stack contains a value of type "pointer 358 to TYPE"; generate code to fetch its value. Note that TYPE is the 359 target type, not the pointer type. */ 360 static void 361 gen_fetch (struct agent_expr *ax, struct type *type) 362 { 363 if (ax->tracing) 364 { 365 /* Record the area of memory we're about to fetch. */ 366 ax_trace_quick (ax, type->length ()); 367 } 368 369 if (type->code () == TYPE_CODE_RANGE) 370 type = type->target_type (); 371 372 switch (type->code ()) 373 { 374 case TYPE_CODE_PTR: 375 case TYPE_CODE_REF: 376 case TYPE_CODE_RVALUE_REF: 377 case TYPE_CODE_ENUM: 378 case TYPE_CODE_INT: 379 case TYPE_CODE_CHAR: 380 case TYPE_CODE_BOOL: 381 /* It's a scalar value, so we know how to dereference it. How 382 many bytes long is it? */ 383 switch (type->length ()) 384 { 385 case 8 / TARGET_CHAR_BIT: 386 ax_simple (ax, aop_ref8); 387 break; 388 case 16 / TARGET_CHAR_BIT: 389 ax_simple (ax, aop_ref16); 390 break; 391 case 32 / TARGET_CHAR_BIT: 392 ax_simple (ax, aop_ref32); 393 break; 394 case 64 / TARGET_CHAR_BIT: 395 ax_simple (ax, aop_ref64); 396 break; 397 398 /* Either our caller shouldn't have asked us to dereference 399 that pointer (other code's fault), or we're not 400 implementing something we should be (this code's fault). 401 In any case, it's a bug the user shouldn't see. */ 402 default: 403 internal_error (_("gen_fetch: strange size")); 404 } 405 406 gen_sign_extend (ax, type); 407 break; 408 409 default: 410 /* Our caller requested us to dereference a pointer from an unsupported 411 type. Error out and give callers a chance to handle the failure 412 gracefully. */ 413 error (_("gen_fetch: Unsupported type code `%s'."), 414 type->name ()); 415 } 416 } 417 418 419 /* Generate code to left shift the top of the stack by DISTANCE bits, or 420 right shift it by -DISTANCE bits if DISTANCE < 0. This generates 421 unsigned (logical) right shifts. */ 422 static void 423 gen_left_shift (struct agent_expr *ax, int distance) 424 { 425 if (distance > 0) 426 { 427 ax_const_l (ax, distance); 428 ax_simple (ax, aop_lsh); 429 } 430 else if (distance < 0) 431 { 432 ax_const_l (ax, -distance); 433 ax_simple (ax, aop_rsh_unsigned); 434 } 435 } 436 437 438 439 /* Generating bytecode from GDB expressions: symbol references */ 440 441 /* Generate code to push the base address of the argument portion of 442 the top stack frame. */ 443 static void 444 gen_frame_args_address (struct agent_expr *ax) 445 { 446 int frame_reg; 447 LONGEST frame_offset; 448 449 gdbarch_virtual_frame_pointer (ax->gdbarch, 450 ax->scope, &frame_reg, &frame_offset); 451 ax_reg (ax, frame_reg); 452 gen_offset (ax, frame_offset); 453 } 454 455 456 /* Generate code to push the base address of the locals portion of the 457 top stack frame. */ 458 static void 459 gen_frame_locals_address (struct agent_expr *ax) 460 { 461 int frame_reg; 462 LONGEST frame_offset; 463 464 gdbarch_virtual_frame_pointer (ax->gdbarch, 465 ax->scope, &frame_reg, &frame_offset); 466 ax_reg (ax, frame_reg); 467 gen_offset (ax, frame_offset); 468 } 469 470 471 /* Generate code to add OFFSET to the top of the stack. Try to 472 generate short and readable code. We use this for getting to 473 variables on the stack, and structure members. If we were 474 programming in ML, it would be clearer why these are the same 475 thing. */ 476 static void 477 gen_offset (struct agent_expr *ax, int offset) 478 { 479 /* It would suffice to simply push the offset and add it, but this 480 makes it easier to read positive and negative offsets in the 481 bytecode. */ 482 if (offset > 0) 483 { 484 ax_const_l (ax, offset); 485 ax_simple (ax, aop_add); 486 } 487 else if (offset < 0) 488 { 489 ax_const_l (ax, -offset); 490 ax_simple (ax, aop_sub); 491 } 492 } 493 494 495 /* In many cases, a symbol's value is the offset from some other 496 address (stack frame, base register, etc.) Generate code to add 497 VAR's value to the top of the stack. */ 498 static void 499 gen_sym_offset (struct agent_expr *ax, struct symbol *var) 500 { 501 gen_offset (ax, var->value_longest ()); 502 } 503 504 505 /* Generate code for a variable reference to AX. The variable is the 506 symbol VAR. Set VALUE to describe the result. */ 507 508 static void 509 gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var) 510 { 511 /* Dereference any typedefs. */ 512 value->type = check_typedef (var->type ()); 513 value->optimized_out = 0; 514 515 if (SYMBOL_COMPUTED_OPS (var) != NULL) 516 { 517 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, ax, value); 518 return; 519 } 520 521 /* I'm imitating the code in read_var_value. */ 522 switch (var->aclass ()) 523 { 524 case LOC_CONST: /* A constant, like an enum value. */ 525 ax_const_l (ax, (LONGEST) var->value_longest ()); 526 value->kind = axs_rvalue; 527 break; 528 529 case LOC_LABEL: /* A goto label, being used as a value. */ 530 ax_const_l (ax, (LONGEST) var->value_address ()); 531 value->kind = axs_rvalue; 532 break; 533 534 case LOC_CONST_BYTES: 535 internal_error (_("gen_var_ref: LOC_CONST_BYTES " 536 "symbols are not supported")); 537 538 /* Variable at a fixed location in memory. Easy. */ 539 case LOC_STATIC: 540 /* Push the address of the variable. */ 541 ax_const_l (ax, var->value_address ()); 542 value->kind = axs_lvalue_memory; 543 break; 544 545 case LOC_ARG: /* var lives in argument area of frame */ 546 gen_frame_args_address (ax); 547 gen_sym_offset (ax, var); 548 value->kind = axs_lvalue_memory; 549 break; 550 551 case LOC_REF_ARG: /* As above, but the frame slot really 552 holds the address of the variable. */ 553 gen_frame_args_address (ax); 554 gen_sym_offset (ax, var); 555 /* Don't assume any particular pointer size. */ 556 gen_fetch (ax, builtin_type (ax->gdbarch)->builtin_data_ptr); 557 value->kind = axs_lvalue_memory; 558 break; 559 560 case LOC_LOCAL: /* var lives in locals area of frame */ 561 gen_frame_locals_address (ax); 562 gen_sym_offset (ax, var); 563 value->kind = axs_lvalue_memory; 564 break; 565 566 case LOC_TYPEDEF: 567 error (_("Cannot compute value of typedef `%s'."), 568 var->print_name ()); 569 break; 570 571 case LOC_BLOCK: 572 ax_const_l (ax, var->value_block ()->entry_pc ()); 573 value->kind = axs_rvalue; 574 break; 575 576 case LOC_REGISTER: 577 /* Don't generate any code at all; in the process of treating 578 this as an lvalue or rvalue, the caller will generate the 579 right code. */ 580 value->kind = axs_lvalue_register; 581 value->u.reg 582 = SYMBOL_REGISTER_OPS (var)->register_number (var, ax->gdbarch); 583 break; 584 585 /* A lot like LOC_REF_ARG, but the pointer lives directly in a 586 register, not on the stack. Simpler than LOC_REGISTER 587 because it's just like any other case where the thing 588 has a real address. */ 589 case LOC_REGPARM_ADDR: 590 ax_reg (ax, 591 SYMBOL_REGISTER_OPS (var)->register_number (var, ax->gdbarch)); 592 value->kind = axs_lvalue_memory; 593 break; 594 595 case LOC_UNRESOLVED: 596 { 597 struct bound_minimal_symbol msym 598 = lookup_minimal_symbol (var->linkage_name (), NULL, NULL); 599 600 if (!msym.minsym) 601 error (_("Couldn't resolve symbol `%s'."), var->print_name ()); 602 603 /* Push the address of the variable. */ 604 ax_const_l (ax, msym.value_address ()); 605 value->kind = axs_lvalue_memory; 606 } 607 break; 608 609 case LOC_COMPUTED: 610 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method"); 611 612 case LOC_OPTIMIZED_OUT: 613 /* Flag this, but don't say anything; leave it up to callers to 614 warn the user. */ 615 value->optimized_out = 1; 616 break; 617 618 default: 619 error (_("Cannot find value of botched symbol `%s'."), 620 var->print_name ()); 621 break; 622 } 623 } 624 625 /* Generate code for a minimal symbol variable reference to AX. The 626 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe 627 the result. */ 628 629 static void 630 gen_msym_var_ref (agent_expr *ax, axs_value *value, 631 minimal_symbol *msymbol, objfile *objf) 632 { 633 CORE_ADDR address; 634 type *t = find_minsym_type_and_address (msymbol, objf, &address); 635 value->type = t; 636 value->optimized_out = false; 637 ax_const_l (ax, address); 638 value->kind = axs_lvalue_memory; 639 } 640 641 642 643 644 /* Generating bytecode from GDB expressions: literals */ 645 646 static void 647 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k, 648 struct type *type) 649 { 650 ax_const_l (ax, k); 651 value->kind = axs_rvalue; 652 value->type = check_typedef (type); 653 } 654 655 656 657 /* Generating bytecode from GDB expressions: unary conversions, casts */ 658 659 /* Take what's on the top of the stack (as described by VALUE), and 660 try to make an rvalue out of it. Signal an error if we can't do 661 that. */ 662 void 663 require_rvalue (struct agent_expr *ax, struct axs_value *value) 664 { 665 /* Only deal with scalars, structs and such may be too large 666 to fit in a stack entry. */ 667 value->type = check_typedef (value->type); 668 if (value->type->code () == TYPE_CODE_ARRAY 669 || value->type->code () == TYPE_CODE_STRUCT 670 || value->type->code () == TYPE_CODE_UNION 671 || value->type->code () == TYPE_CODE_FUNC) 672 error (_("Value not scalar: cannot be an rvalue.")); 673 674 switch (value->kind) 675 { 676 case axs_rvalue: 677 /* It's already an rvalue. */ 678 break; 679 680 case axs_lvalue_memory: 681 /* The top of stack is the address of the object. Dereference. */ 682 gen_fetch (ax, value->type); 683 break; 684 685 case axs_lvalue_register: 686 /* There's nothing on the stack, but value->u.reg is the 687 register number containing the value. 688 689 When we add floating-point support, this is going to have to 690 change. What about SPARC register pairs, for example? */ 691 ax_reg (ax, value->u.reg); 692 gen_extend (ax, value->type); 693 break; 694 } 695 696 value->kind = axs_rvalue; 697 } 698 699 700 /* Assume the top of the stack is described by VALUE, and perform the 701 usual unary conversions. This is motivated by ANSI 6.2.2, but of 702 course GDB expressions are not ANSI; they're the mishmash union of 703 a bunch of languages. Rah. 704 705 NOTE! This function promises to produce an rvalue only when the 706 incoming value is of an appropriate type. In other words, the 707 consumer of the value this function produces may assume the value 708 is an rvalue only after checking its type. 709 710 The immediate issue is that if the user tries to use a structure or 711 union as an operand of, say, the `+' operator, we don't want to try 712 to convert that structure to an rvalue; require_rvalue will bomb on 713 structs and unions. Rather, we want to simply pass the struct 714 lvalue through unchanged, and let `+' raise an error. */ 715 716 static void 717 gen_usual_unary (struct agent_expr *ax, struct axs_value *value) 718 { 719 /* We don't have to generate any code for the usual integral 720 conversions, since values are always represented as full-width on 721 the stack. Should we tweak the type? */ 722 723 /* Some types require special handling. */ 724 switch (value->type->code ()) 725 { 726 /* Functions get converted to a pointer to the function. */ 727 case TYPE_CODE_FUNC: 728 value->type = lookup_pointer_type (value->type); 729 value->kind = axs_rvalue; /* Should always be true, but just in case. */ 730 break; 731 732 /* Arrays get converted to a pointer to their first element, and 733 are no longer an lvalue. */ 734 case TYPE_CODE_ARRAY: 735 { 736 struct type *elements = value->type->target_type (); 737 738 value->type = lookup_pointer_type (elements); 739 value->kind = axs_rvalue; 740 /* We don't need to generate any code; the address of the array 741 is also the address of its first element. */ 742 } 743 break; 744 745 /* Don't try to convert structures and unions to rvalues. Let the 746 consumer signal an error. */ 747 case TYPE_CODE_STRUCT: 748 case TYPE_CODE_UNION: 749 return; 750 } 751 752 /* If the value is an lvalue, dereference it. */ 753 require_rvalue (ax, value); 754 } 755 756 757 /* Return non-zero iff the type TYPE1 is considered "wider" than the 758 type TYPE2, according to the rules described in gen_usual_arithmetic. */ 759 static int 760 type_wider_than (struct type *type1, struct type *type2) 761 { 762 return (type1->length () > type2->length () 763 || (type1->length () == type2->length () 764 && type1->is_unsigned () 765 && !type2->is_unsigned ())); 766 } 767 768 769 /* Return the "wider" of the two types TYPE1 and TYPE2. */ 770 static struct type * 771 max_type (struct type *type1, struct type *type2) 772 { 773 return type_wider_than (type1, type2) ? type1 : type2; 774 } 775 776 777 /* Generate code to convert a scalar value of type FROM to type TO. */ 778 static void 779 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to) 780 { 781 /* Perhaps there is a more graceful way to state these rules. */ 782 783 /* If we're converting to a narrower type, then we need to clear out 784 the upper bits. */ 785 if (to->length () < from->length ()) 786 gen_extend (ax, to); 787 788 /* If the two values have equal width, but different signednesses, 789 then we need to extend. */ 790 else if (to->length () == from->length ()) 791 { 792 if (from->is_unsigned () != to->is_unsigned ()) 793 gen_extend (ax, to); 794 } 795 796 /* If we're converting to a wider type, and becoming unsigned, then 797 we need to zero out any possible sign bits. */ 798 else if (to->length () > from->length ()) 799 { 800 if (to->is_unsigned ()) 801 gen_extend (ax, to); 802 } 803 } 804 805 806 /* Return non-zero iff the type FROM will require any bytecodes to be 807 emitted to be converted to the type TO. */ 808 static int 809 is_nontrivial_conversion (struct type *from, struct type *to) 810 { 811 agent_expr_up ax (new agent_expr (NULL, 0)); 812 int nontrivial; 813 814 /* Actually generate the code, and see if anything came out. At the 815 moment, it would be trivial to replicate the code in 816 gen_conversion here, but in the future, when we're supporting 817 floating point and the like, it may not be. Doing things this 818 way allows this function to be independent of the logic in 819 gen_conversion. */ 820 gen_conversion (ax.get (), from, to); 821 nontrivial = ax->len > 0; 822 return nontrivial; 823 } 824 825 826 /* Generate code to perform the "usual arithmetic conversions" (ANSI C 827 6.2.1.5) for the two operands of an arithmetic operator. This 828 effectively finds a "least upper bound" type for the two arguments, 829 and promotes each argument to that type. *VALUE1 and *VALUE2 830 describe the values as they are passed in, and as they are left. */ 831 static void 832 gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1, 833 struct axs_value *value2) 834 { 835 /* Do the usual binary conversions. */ 836 if (value1->type->code () == TYPE_CODE_INT 837 && value2->type->code () == TYPE_CODE_INT) 838 { 839 /* The ANSI integral promotions seem to work this way: Order the 840 integer types by size, and then by signedness: an n-bit 841 unsigned type is considered "wider" than an n-bit signed 842 type. Promote to the "wider" of the two types, and always 843 promote at least to int. */ 844 struct type *target = max_type (builtin_type (ax->gdbarch)->builtin_int, 845 max_type (value1->type, value2->type)); 846 847 /* Deal with value2, on the top of the stack. */ 848 gen_conversion (ax, value2->type, target); 849 850 /* Deal with value1, not on the top of the stack. Don't 851 generate the `swap' instructions if we're not actually going 852 to do anything. */ 853 if (is_nontrivial_conversion (value1->type, target)) 854 { 855 ax_simple (ax, aop_swap); 856 gen_conversion (ax, value1->type, target); 857 ax_simple (ax, aop_swap); 858 } 859 860 value1->type = value2->type = check_typedef (target); 861 } 862 } 863 864 865 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on 866 the value on the top of the stack, as described by VALUE. Assume 867 the value has integral type. */ 868 static void 869 gen_integral_promotions (struct agent_expr *ax, struct axs_value *value) 870 { 871 const struct builtin_type *builtin = builtin_type (ax->gdbarch); 872 873 if (!type_wider_than (value->type, builtin->builtin_int)) 874 { 875 gen_conversion (ax, value->type, builtin->builtin_int); 876 value->type = builtin->builtin_int; 877 } 878 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int)) 879 { 880 gen_conversion (ax, value->type, builtin->builtin_unsigned_int); 881 value->type = builtin->builtin_unsigned_int; 882 } 883 } 884 885 886 /* Generate code for a cast to TYPE. */ 887 static void 888 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type) 889 { 890 /* GCC does allow casts to yield lvalues, so this should be fixed 891 before merging these changes into the trunk. */ 892 require_rvalue (ax, value); 893 /* Dereference typedefs. */ 894 type = check_typedef (type); 895 896 switch (type->code ()) 897 { 898 case TYPE_CODE_PTR: 899 case TYPE_CODE_REF: 900 case TYPE_CODE_RVALUE_REF: 901 /* It's implementation-defined, and I'll bet this is what GCC 902 does. */ 903 break; 904 905 case TYPE_CODE_ARRAY: 906 case TYPE_CODE_STRUCT: 907 case TYPE_CODE_UNION: 908 case TYPE_CODE_FUNC: 909 error (_("Invalid type cast: intended type must be scalar.")); 910 911 case TYPE_CODE_ENUM: 912 case TYPE_CODE_BOOL: 913 /* We don't have to worry about the size of the value, because 914 all our integral values are fully sign-extended, and when 915 casting pointers we can do anything we like. Is there any 916 way for us to know what GCC actually does with a cast like 917 this? */ 918 break; 919 920 case TYPE_CODE_INT: 921 gen_conversion (ax, value->type, type); 922 break; 923 924 case TYPE_CODE_VOID: 925 /* We could pop the value, and rely on everyone else to check 926 the type and notice that this value doesn't occupy a stack 927 slot. But for now, leave the value on the stack, and 928 preserve the "value == stack element" assumption. */ 929 break; 930 931 default: 932 error (_("Casts to requested type are not yet implemented.")); 933 } 934 935 value->type = type; 936 } 937 938 939 940 /* Generating bytecode from GDB expressions: arithmetic */ 941 942 /* Scale the integer on the top of the stack by the size of the target 943 of the pointer type TYPE. */ 944 static void 945 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type) 946 { 947 struct type *element = type->target_type (); 948 949 if (element->length () != 1) 950 { 951 ax_const_l (ax, element->length ()); 952 ax_simple (ax, op); 953 } 954 } 955 956 957 /* Generate code for pointer arithmetic PTR + INT. */ 958 static void 959 gen_ptradd (struct agent_expr *ax, struct axs_value *value, 960 struct axs_value *value1, struct axs_value *value2) 961 { 962 gdb_assert (value1->type->is_pointer_or_reference ()); 963 gdb_assert (value2->type->code () == TYPE_CODE_INT); 964 965 gen_scale (ax, aop_mul, value1->type); 966 ax_simple (ax, aop_add); 967 gen_extend (ax, value1->type); /* Catch overflow. */ 968 value->type = value1->type; 969 value->kind = axs_rvalue; 970 } 971 972 973 /* Generate code for pointer arithmetic PTR - INT. */ 974 static void 975 gen_ptrsub (struct agent_expr *ax, struct axs_value *value, 976 struct axs_value *value1, struct axs_value *value2) 977 { 978 gdb_assert (value1->type->is_pointer_or_reference ()); 979 gdb_assert (value2->type->code () == TYPE_CODE_INT); 980 981 gen_scale (ax, aop_mul, value1->type); 982 ax_simple (ax, aop_sub); 983 gen_extend (ax, value1->type); /* Catch overflow. */ 984 value->type = value1->type; 985 value->kind = axs_rvalue; 986 } 987 988 989 /* Generate code for pointer arithmetic PTR - PTR. */ 990 static void 991 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value, 992 struct axs_value *value1, struct axs_value *value2, 993 struct type *result_type) 994 { 995 gdb_assert (value1->type->is_pointer_or_reference ()); 996 gdb_assert (value2->type->is_pointer_or_reference ()); 997 998 if (value1->type->target_type ()->length () 999 != value2->type->target_type ()->length ()) 1000 error (_("\ 1001 First argument of `-' is a pointer, but second argument is neither\n\ 1002 an integer nor a pointer of the same type.")); 1003 1004 ax_simple (ax, aop_sub); 1005 gen_scale (ax, aop_div_unsigned, value1->type); 1006 value->type = result_type; 1007 value->kind = axs_rvalue; 1008 } 1009 1010 static void 1011 gen_equal (struct agent_expr *ax, struct axs_value *value, 1012 struct axs_value *value1, struct axs_value *value2, 1013 struct type *result_type) 1014 { 1015 if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ()) 1016 ax_simple (ax, aop_equal); 1017 else 1018 gen_binop (ax, value, value1, value2, 1019 aop_equal, aop_equal, 0, "equal"); 1020 value->type = result_type; 1021 value->kind = axs_rvalue; 1022 } 1023 1024 static void 1025 gen_less (struct agent_expr *ax, struct axs_value *value, 1026 struct axs_value *value1, struct axs_value *value2, 1027 struct type *result_type) 1028 { 1029 if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ()) 1030 ax_simple (ax, aop_less_unsigned); 1031 else 1032 gen_binop (ax, value, value1, value2, 1033 aop_less_signed, aop_less_unsigned, 0, "less than"); 1034 value->type = result_type; 1035 value->kind = axs_rvalue; 1036 } 1037 1038 /* Generate code for a binary operator that doesn't do pointer magic. 1039 We set VALUE to describe the result value; we assume VALUE1 and 1040 VALUE2 describe the two operands, and that they've undergone the 1041 usual binary conversions. MAY_CARRY should be non-zero iff the 1042 result needs to be extended. NAME is the English name of the 1043 operator, used in error messages */ 1044 static void 1045 gen_binop (struct agent_expr *ax, struct axs_value *value, 1046 struct axs_value *value1, struct axs_value *value2, 1047 enum agent_op op, enum agent_op op_unsigned, 1048 int may_carry, const char *name) 1049 { 1050 /* We only handle INT op INT. */ 1051 if ((value1->type->code () != TYPE_CODE_INT) 1052 || (value2->type->code () != TYPE_CODE_INT)) 1053 error (_("Invalid combination of types in %s."), name); 1054 1055 ax_simple (ax, value1->type->is_unsigned () ? op_unsigned : op); 1056 if (may_carry) 1057 gen_extend (ax, value1->type); /* catch overflow */ 1058 value->type = value1->type; 1059 value->kind = axs_rvalue; 1060 } 1061 1062 1063 static void 1064 gen_logical_not (struct agent_expr *ax, struct axs_value *value, 1065 struct type *result_type) 1066 { 1067 if (value->type->code () != TYPE_CODE_INT 1068 && value->type->code () != TYPE_CODE_PTR) 1069 error (_("Invalid type of operand to `!'.")); 1070 1071 ax_simple (ax, aop_log_not); 1072 value->type = result_type; 1073 } 1074 1075 1076 static void 1077 gen_complement (struct agent_expr *ax, struct axs_value *value) 1078 { 1079 if (value->type->code () != TYPE_CODE_INT) 1080 error (_("Invalid type of operand to `~'.")); 1081 1082 ax_simple (ax, aop_bit_not); 1083 gen_extend (ax, value->type); 1084 } 1085 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 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 (!value->type->is_pointer_or_reference ()) 1097 internal_error (_("gen_deref: expected a pointer")); 1098 1099 /* We've got an rvalue now, which is a pointer. We want to yield an 1100 lvalue, whose address is exactly that pointer. So we don't 1101 actually emit any code; we just change the type from "Pointer to 1102 T" to "T", and mark the value as an lvalue in memory. Leave it 1103 to the consumer to actually dereference it. */ 1104 value->type = check_typedef (value->type->target_type ()); 1105 if (value->type->code () == TYPE_CODE_VOID) 1106 error (_("Attempt to dereference a generic pointer.")); 1107 value->kind = ((value->type->code () == TYPE_CODE_FUNC) 1108 ? axs_rvalue : axs_lvalue_memory); 1109 } 1110 1111 1112 /* Produce the address of the lvalue on the top of the stack. */ 1113 static void 1114 gen_address_of (struct axs_value *value) 1115 { 1116 /* Special case for taking the address of a function. The ANSI 1117 standard describes this as a special case, too, so this 1118 arrangement is not without motivation. */ 1119 if (value->type->code () == TYPE_CODE_FUNC) 1120 /* The value's already an rvalue on the stack, so we just need to 1121 change the type. */ 1122 value->type = lookup_pointer_type (value->type); 1123 else 1124 switch (value->kind) 1125 { 1126 case axs_rvalue: 1127 error (_("Operand of `&' is an rvalue, which has no address.")); 1128 1129 case axs_lvalue_register: 1130 error (_("Operand of `&' is in a register, and has no address.")); 1131 1132 case axs_lvalue_memory: 1133 value->kind = axs_rvalue; 1134 value->type = lookup_pointer_type (value->type); 1135 break; 1136 } 1137 } 1138 1139 /* Generate code to push the value of a bitfield of a structure whose 1140 address is on the top of the stack. START and END give the 1141 starting and one-past-ending *bit* numbers of the field within the 1142 structure. */ 1143 static void 1144 gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value, 1145 struct type *type, int start, int end) 1146 { 1147 /* Note that ops[i] fetches 8 << i bits. */ 1148 static enum agent_op ops[] 1149 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64}; 1150 static int num_ops = (sizeof (ops) / sizeof (ops[0])); 1151 1152 /* We don't want to touch any byte that the bitfield doesn't 1153 actually occupy; we shouldn't make any accesses we're not 1154 explicitly permitted to. We rely here on the fact that the 1155 bytecode `ref' operators work on unaligned addresses. 1156 1157 It takes some fancy footwork to get the stack to work the way 1158 we'd like. Say we're retrieving a bitfield that requires three 1159 fetches. Initially, the stack just contains the address: 1160 addr 1161 For the first fetch, we duplicate the address 1162 addr addr 1163 then add the byte offset, do the fetch, and shift and mask as 1164 needed, yielding a fragment of the value, properly aligned for 1165 the final bitwise or: 1166 addr frag1 1167 then we swap, and repeat the process: 1168 frag1 addr --- address on top 1169 frag1 addr addr --- duplicate it 1170 frag1 addr frag2 --- get second fragment 1171 frag1 frag2 addr --- swap again 1172 frag1 frag2 frag3 --- get third fragment 1173 Notice that, since the third fragment is the last one, we don't 1174 bother duplicating the address this time. Now we have all the 1175 fragments on the stack, and we can simply `or' them together, 1176 yielding the final value of the bitfield. */ 1177 1178 /* The first and one-after-last bits in the field, but rounded down 1179 and up to byte boundaries. */ 1180 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT; 1181 int bound_end = (((end + TARGET_CHAR_BIT - 1) 1182 / TARGET_CHAR_BIT) 1183 * TARGET_CHAR_BIT); 1184 1185 /* current bit offset within the structure */ 1186 int offset; 1187 1188 /* The index in ops of the opcode we're considering. */ 1189 int op; 1190 1191 /* The number of fragments we generated in the process. Probably 1192 equal to the number of `one' bits in bytesize, but who cares? */ 1193 int fragment_count; 1194 1195 /* Dereference any typedefs. */ 1196 type = check_typedef (type); 1197 1198 /* Can we fetch the number of bits requested at all? */ 1199 if ((end - start) > ((1 << num_ops) * 8)) 1200 internal_error (_("gen_bitfield_ref: bitfield too wide")); 1201 1202 /* Note that we know here that we only need to try each opcode once. 1203 That may not be true on machines with weird byte sizes. */ 1204 offset = bound_start; 1205 fragment_count = 0; 1206 for (op = num_ops - 1; op >= 0; op--) 1207 { 1208 /* number of bits that ops[op] would fetch */ 1209 int op_size = 8 << op; 1210 1211 /* The stack at this point, from bottom to top, contains zero or 1212 more fragments, then the address. */ 1213 1214 /* Does this fetch fit within the bitfield? */ 1215 if (offset + op_size <= bound_end) 1216 { 1217 /* Is this the last fragment? */ 1218 int last_frag = (offset + op_size == bound_end); 1219 1220 if (!last_frag) 1221 ax_simple (ax, aop_dup); /* keep a copy of the address */ 1222 1223 /* Add the offset. */ 1224 gen_offset (ax, offset / TARGET_CHAR_BIT); 1225 1226 if (ax->tracing) 1227 { 1228 /* Record the area of memory we're about to fetch. */ 1229 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT); 1230 } 1231 1232 /* Perform the fetch. */ 1233 ax_simple (ax, ops[op]); 1234 1235 /* Shift the bits we have to their proper position. 1236 gen_left_shift will generate right shifts when the operand 1237 is negative. 1238 1239 A big-endian field diagram to ponder: 1240 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7 1241 +------++------++------++------++------++------++------++------+ 1242 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx 1243 ^ ^ ^ ^ 1244 bit number 16 32 48 53 1245 These are bit numbers as supplied by GDB. Note that the 1246 bit numbers run from right to left once you've fetched the 1247 value! 1248 1249 A little-endian field diagram to ponder: 1250 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0 1251 +------++------++------++------++------++------++------++------+ 1252 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx 1253 ^ ^ ^ ^ ^ 1254 bit number 48 32 16 4 0 1255 1256 In both cases, the most significant end is on the left 1257 (i.e. normal numeric writing order), which means that you 1258 don't go crazy thinking about `left' and `right' shifts. 1259 1260 We don't have to worry about masking yet: 1261 - If they contain garbage off the least significant end, then we 1262 must be looking at the low end of the field, and the right 1263 shift will wipe them out. 1264 - If they contain garbage off the most significant end, then we 1265 must be looking at the most significant end of the word, and 1266 the sign/zero extension will wipe them out. 1267 - If we're in the interior of the word, then there is no garbage 1268 on either end, because the ref operators zero-extend. */ 1269 if (gdbarch_byte_order (ax->gdbarch) == BFD_ENDIAN_BIG) 1270 gen_left_shift (ax, end - (offset + op_size)); 1271 else 1272 gen_left_shift (ax, offset - start); 1273 1274 if (!last_frag) 1275 /* Bring the copy of the address up to the top. */ 1276 ax_simple (ax, aop_swap); 1277 1278 offset += op_size; 1279 fragment_count++; 1280 } 1281 } 1282 1283 /* Generate enough bitwise `or' operations to combine all the 1284 fragments we left on the stack. */ 1285 while (fragment_count-- > 1) 1286 ax_simple (ax, aop_bit_or); 1287 1288 /* Sign- or zero-extend the value as appropriate. */ 1289 ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, end - start)); 1290 1291 /* This is *not* an lvalue. Ugh. */ 1292 value->kind = axs_rvalue; 1293 value->type = type; 1294 } 1295 1296 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET 1297 is an accumulated offset (in bytes), will be nonzero for objects 1298 embedded in other objects, like C++ base classes. Behavior should 1299 generally follow value_primitive_field. */ 1300 1301 static void 1302 gen_primitive_field (struct agent_expr *ax, struct axs_value *value, 1303 int offset, int fieldno, struct type *type) 1304 { 1305 /* Is this a bitfield? */ 1306 if (TYPE_FIELD_PACKED (type, fieldno)) 1307 gen_bitfield_ref (ax, value, type->field (fieldno).type (), 1308 (offset * TARGET_CHAR_BIT 1309 + type->field (fieldno).loc_bitpos ()), 1310 (offset * TARGET_CHAR_BIT 1311 + type->field (fieldno).loc_bitpos () 1312 + TYPE_FIELD_BITSIZE (type, fieldno))); 1313 else 1314 { 1315 gen_offset (ax, offset 1316 + type->field (fieldno).loc_bitpos () / TARGET_CHAR_BIT); 1317 value->kind = axs_lvalue_memory; 1318 value->type = type->field (fieldno).type (); 1319 } 1320 } 1321 1322 /* Search for the given field in either the given type or one of its 1323 base classes. Return 1 if found, 0 if not. */ 1324 1325 static int 1326 gen_struct_ref_recursive (struct agent_expr *ax, struct axs_value *value, 1327 const char *field, int offset, struct type *type) 1328 { 1329 int i, rslt; 1330 int nbases = TYPE_N_BASECLASSES (type); 1331 1332 type = check_typedef (type); 1333 1334 for (i = type->num_fields () - 1; i >= nbases; i--) 1335 { 1336 const char *this_name = type->field (i).name (); 1337 1338 if (this_name) 1339 { 1340 if (strcmp (field, this_name) == 0) 1341 { 1342 /* Note that bytecodes for the struct's base (aka 1343 "this") will have been generated already, which will 1344 be unnecessary but not harmful if the static field is 1345 being handled as a global. */ 1346 if (field_is_static (&type->field (i))) 1347 { 1348 gen_static_field (ax, value, type, i); 1349 if (value->optimized_out) 1350 error (_("static field `%s' has been " 1351 "optimized out, cannot use"), 1352 field); 1353 return 1; 1354 } 1355 1356 gen_primitive_field (ax, value, offset, i, type); 1357 return 1; 1358 } 1359 #if 0 /* is this right? */ 1360 if (this_name[0] == '\0') 1361 internal_error (_("find_field: anonymous unions not supported")); 1362 #endif 1363 } 1364 } 1365 1366 /* Now scan through base classes recursively. */ 1367 for (i = 0; i < nbases; i++) 1368 { 1369 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i)); 1370 1371 rslt = gen_struct_ref_recursive (ax, value, field, 1372 offset + TYPE_BASECLASS_BITPOS (type, i) 1373 / TARGET_CHAR_BIT, 1374 basetype); 1375 if (rslt) 1376 return 1; 1377 } 1378 1379 /* Not found anywhere, flag so caller can complain. */ 1380 return 0; 1381 } 1382 1383 /* Generate code to reference the member named FIELD of a structure or 1384 union. The top of the stack, as described by VALUE, should have 1385 type (pointer to a)* struct/union. OPERATOR_NAME is the name of 1386 the operator being compiled, and OPERAND_NAME is the kind of thing 1387 it operates on; we use them in error messages. */ 1388 static void 1389 gen_struct_ref (struct agent_expr *ax, struct axs_value *value, 1390 const char *field, const char *operator_name, 1391 const char *operand_name) 1392 { 1393 struct type *type; 1394 int found; 1395 1396 /* Follow pointers until we reach a non-pointer. These aren't the C 1397 semantics, but they're what the normal GDB evaluator does, so we 1398 should at least be consistent. */ 1399 while (value->type->is_pointer_or_reference ()) 1400 { 1401 require_rvalue (ax, value); 1402 gen_deref (value); 1403 } 1404 type = check_typedef (value->type); 1405 1406 /* This must yield a structure or a union. */ 1407 if (type->code () != TYPE_CODE_STRUCT 1408 && type->code () != TYPE_CODE_UNION) 1409 error (_("The left operand of `%s' is not a %s."), 1410 operator_name, operand_name); 1411 1412 /* And it must be in memory; we don't deal with structure rvalues, 1413 or structures living in registers. */ 1414 if (value->kind != axs_lvalue_memory) 1415 error (_("Structure does not live in memory.")); 1416 1417 /* Search through fields and base classes recursively. */ 1418 found = gen_struct_ref_recursive (ax, value, field, 0, type); 1419 1420 if (!found) 1421 error (_("Couldn't find member named `%s' in struct/union/class `%s'"), 1422 field, type->name ()); 1423 } 1424 1425 static int 1426 gen_namespace_elt (struct agent_expr *ax, struct axs_value *value, 1427 const struct type *curtype, const char *name); 1428 static int 1429 gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value, 1430 const struct type *curtype, const char *name); 1431 1432 static void 1433 gen_static_field (struct agent_expr *ax, struct axs_value *value, 1434 struct type *type, int fieldno) 1435 { 1436 if (type->field (fieldno).loc_kind () == FIELD_LOC_KIND_PHYSADDR) 1437 { 1438 ax_const_l (ax, type->field (fieldno).loc_physaddr ()); 1439 value->kind = axs_lvalue_memory; 1440 value->type = type->field (fieldno).type (); 1441 value->optimized_out = 0; 1442 } 1443 else 1444 { 1445 const char *phys_name = type->field (fieldno).loc_physname (); 1446 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0).symbol; 1447 1448 if (sym) 1449 { 1450 gen_var_ref (ax, value, sym); 1451 1452 /* Don't error if the value was optimized out, we may be 1453 scanning all static fields and just want to pass over this 1454 and continue with the rest. */ 1455 } 1456 else 1457 { 1458 /* Silently assume this was optimized out; class printing 1459 will let the user know why the data is missing. */ 1460 value->optimized_out = 1; 1461 } 1462 } 1463 } 1464 1465 static int 1466 gen_struct_elt_for_reference (struct agent_expr *ax, struct axs_value *value, 1467 struct type *type, const char *fieldname) 1468 { 1469 struct type *t = type; 1470 int i; 1471 1472 if (t->code () != TYPE_CODE_STRUCT 1473 && t->code () != TYPE_CODE_UNION) 1474 internal_error (_("non-aggregate type to gen_struct_elt_for_reference")); 1475 1476 for (i = t->num_fields () - 1; i >= TYPE_N_BASECLASSES (t); i--) 1477 { 1478 const char *t_field_name = t->field (i).name (); 1479 1480 if (t_field_name && strcmp (t_field_name, fieldname) == 0) 1481 { 1482 if (field_is_static (&t->field (i))) 1483 { 1484 gen_static_field (ax, value, t, i); 1485 if (value->optimized_out) 1486 error (_("static field `%s' has been " 1487 "optimized out, cannot use"), 1488 fieldname); 1489 return 1; 1490 } 1491 if (TYPE_FIELD_PACKED (t, i)) 1492 error (_("pointers to bitfield members not allowed")); 1493 1494 /* FIXME we need a way to do "want_address" equivalent */ 1495 1496 error (_("Cannot reference non-static field \"%s\""), fieldname); 1497 } 1498 } 1499 1500 /* FIXME add other scoped-reference cases here */ 1501 1502 /* Do a last-ditch lookup. */ 1503 return gen_maybe_namespace_elt (ax, value, type, fieldname); 1504 } 1505 1506 /* C++: Return the member NAME of the namespace given by the type 1507 CURTYPE. */ 1508 1509 static int 1510 gen_namespace_elt (struct agent_expr *ax, struct axs_value *value, 1511 const struct type *curtype, const char *name) 1512 { 1513 int found = gen_maybe_namespace_elt (ax, value, curtype, name); 1514 1515 if (!found) 1516 error (_("No symbol \"%s\" in namespace \"%s\"."), 1517 name, curtype->name ()); 1518 1519 return found; 1520 } 1521 1522 /* A helper function used by value_namespace_elt and 1523 value_struct_elt_for_reference. It looks up NAME inside the 1524 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE 1525 is a class and NAME refers to a type in CURTYPE itself (as opposed 1526 to, say, some base class of CURTYPE). */ 1527 1528 static int 1529 gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value, 1530 const struct type *curtype, const char *name) 1531 { 1532 const char *namespace_name = curtype->name (); 1533 struct block_symbol sym; 1534 1535 sym = cp_lookup_symbol_namespace (namespace_name, name, 1536 block_for_pc (ax->scope), 1537 VAR_DOMAIN); 1538 1539 if (sym.symbol == NULL) 1540 return 0; 1541 1542 gen_var_ref (ax, value, sym.symbol); 1543 1544 if (value->optimized_out) 1545 error (_("`%s' has been optimized out, cannot use"), 1546 sym.symbol->print_name ()); 1547 1548 return 1; 1549 } 1550 1551 1552 static int 1553 gen_aggregate_elt_ref (struct agent_expr *ax, struct axs_value *value, 1554 struct type *type, const char *field) 1555 { 1556 switch (type->code ()) 1557 { 1558 case TYPE_CODE_STRUCT: 1559 case TYPE_CODE_UNION: 1560 return gen_struct_elt_for_reference (ax, value, type, field); 1561 break; 1562 case TYPE_CODE_NAMESPACE: 1563 return gen_namespace_elt (ax, value, type, field); 1564 break; 1565 default: 1566 internal_error (_("non-aggregate type in gen_aggregate_elt_ref")); 1567 } 1568 1569 return 0; 1570 } 1571 1572 1573 1574 namespace expr 1575 { 1576 1577 void 1578 operation::generate_ax (struct expression *exp, 1579 struct agent_expr *ax, 1580 struct axs_value *value, 1581 struct type *cast_type) 1582 { 1583 if (constant_p ()) 1584 { 1585 struct value *v = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS); 1586 ax_const_l (ax, value_as_long (v)); 1587 value->kind = axs_rvalue; 1588 value->type = check_typedef (value_type (v)); 1589 } 1590 else 1591 { 1592 do_generate_ax (exp, ax, value, cast_type); 1593 if (cast_type != nullptr) 1594 gen_cast (ax, value, cast_type); 1595 } 1596 } 1597 1598 void 1599 scope_operation::do_generate_ax (struct expression *exp, 1600 struct agent_expr *ax, 1601 struct axs_value *value, 1602 struct type *cast_type) 1603 { 1604 struct type *type = std::get<0> (m_storage); 1605 const std::string &name = std::get<1> (m_storage); 1606 int found = gen_aggregate_elt_ref (ax, value, type, name.c_str ()); 1607 if (!found) 1608 error (_("There is no field named %s"), name.c_str ()); 1609 } 1610 1611 void 1612 long_const_operation::do_generate_ax (struct expression *exp, 1613 struct agent_expr *ax, 1614 struct axs_value *value, 1615 struct type *cast_type) 1616 { 1617 gen_int_literal (ax, value, std::get<1> (m_storage), 1618 std::get<0> (m_storage)); 1619 } 1620 1621 void 1622 var_msym_value_operation::do_generate_ax (struct expression *exp, 1623 struct agent_expr *ax, 1624 struct axs_value *value, 1625 struct type *cast_type) 1626 { 1627 const bound_minimal_symbol &b = std::get<0> (m_storage); 1628 gen_msym_var_ref (ax, value, b.minsym, b.objfile); 1629 1630 if (value->type->code () == TYPE_CODE_ERROR) 1631 { 1632 if (cast_type == nullptr) 1633 error_unknown_type (b.minsym->linkage_name ()); 1634 value->type = cast_type; 1635 } 1636 } 1637 1638 void 1639 register_operation::do_generate_ax (struct expression *exp, 1640 struct agent_expr *ax, 1641 struct axs_value *value, 1642 struct type *cast_type) 1643 { 1644 const char *name = std::get<0> (m_storage).c_str (); 1645 int len = std::get<0> (m_storage).size (); 1646 int reg; 1647 1648 reg = user_reg_map_name_to_regnum (ax->gdbarch, name, len); 1649 if (reg == -1) 1650 internal_error (_("Register $%s not available"), name); 1651 /* No support for tracing user registers yet. */ 1652 if (reg >= gdbarch_num_cooked_regs (ax->gdbarch)) 1653 error (_("'%s' is a user-register; " 1654 "GDB cannot yet trace user-register contents."), 1655 name); 1656 value->kind = axs_lvalue_register; 1657 value->u.reg = reg; 1658 value->type = register_type (ax->gdbarch, reg); 1659 } 1660 1661 void 1662 internalvar_operation::do_generate_ax (struct expression *exp, 1663 struct agent_expr *ax, 1664 struct axs_value *value, 1665 struct type *cast_type) 1666 { 1667 struct internalvar *var = std::get<0> (m_storage); 1668 const char *name = internalvar_name (var); 1669 struct trace_state_variable *tsv; 1670 1671 tsv = find_trace_state_variable (name); 1672 if (tsv) 1673 { 1674 ax_tsv (ax, aop_getv, tsv->number); 1675 if (ax->tracing) 1676 ax_tsv (ax, aop_tracev, tsv->number); 1677 /* Trace state variables are always 64-bit integers. */ 1678 value->kind = axs_rvalue; 1679 value->type = builtin_type (ax->gdbarch)->builtin_long_long; 1680 } 1681 else if (! compile_internalvar_to_ax (var, ax, value)) 1682 error (_("$%s is not a trace state variable; GDB agent " 1683 "expressions cannot use convenience variables."), name); 1684 } 1685 1686 void 1687 ternop_cond_operation::do_generate_ax (struct expression *exp, 1688 struct agent_expr *ax, 1689 struct axs_value *value, 1690 struct type *cast_type) 1691 { 1692 struct axs_value value1, value2, value3; 1693 int if1, end; 1694 1695 std::get<0> (m_storage)->generate_ax (exp, ax, &value1); 1696 gen_usual_unary (ax, &value1); 1697 /* For (A ? B : C), it's easiest to generate subexpression 1698 bytecodes in order, but if_goto jumps on true, so we invert 1699 the sense of A. Then we can do B by dropping through, and 1700 jump to do C. */ 1701 gen_logical_not (ax, &value1, builtin_type (ax->gdbarch)->builtin_int); 1702 if1 = ax_goto (ax, aop_if_goto); 1703 std::get<1> (m_storage)->generate_ax (exp, ax, &value2); 1704 gen_usual_unary (ax, &value2); 1705 end = ax_goto (ax, aop_goto); 1706 ax_label (ax, if1, ax->len); 1707 std::get<2> (m_storage)->generate_ax (exp, ax, &value3); 1708 gen_usual_unary (ax, &value3); 1709 ax_label (ax, end, ax->len); 1710 /* This is arbitrary - what if B and C are incompatible types? */ 1711 value->type = value2.type; 1712 value->kind = value2.kind; 1713 } 1714 1715 /* Generate code for GDB's magical `repeat' operator. 1716 LVALUE @ INT creates an array INT elements long, and whose elements 1717 have the same type as LVALUE, located in memory so that LVALUE is 1718 its first element. For example, argv[0]@argc gives you the array 1719 of command-line arguments. 1720 1721 Unfortunately, because we have to know the types before we actually 1722 have a value for the expression, we can't implement this perfectly 1723 without changing the type system, having values that occupy two 1724 stack slots, doing weird things with sizeof, etc. So we require 1725 the right operand to be a constant expression. */ 1726 void 1727 repeat_operation::do_generate_ax (struct expression *exp, 1728 struct agent_expr *ax, 1729 struct axs_value *value, 1730 struct type *cast_type) 1731 { 1732 struct axs_value value1; 1733 1734 /* We don't want to turn this into an rvalue, so no conversions 1735 here. */ 1736 std::get<0> (m_storage)->generate_ax (exp, ax, &value1); 1737 if (value1.kind != axs_lvalue_memory) 1738 error (_("Left operand of `@' must be an object in memory.")); 1739 1740 /* Evaluate the length; it had better be a constant. */ 1741 if (!std::get<1> (m_storage)->constant_p ()) 1742 error (_("Right operand of `@' must be a " 1743 "constant, in agent expressions.")); 1744 1745 struct value *v 1746 = std::get<1> (m_storage)->evaluate (nullptr, exp, 1747 EVAL_AVOID_SIDE_EFFECTS); 1748 if (value_type (v)->code () != TYPE_CODE_INT) 1749 error (_("Right operand of `@' must be an integer.")); 1750 int length = value_as_long (v); 1751 if (length <= 0) 1752 error (_("Right operand of `@' must be positive.")); 1753 1754 /* The top of the stack is already the address of the object, so 1755 all we need to do is frob the type of the lvalue. */ 1756 /* FIXME-type-allocation: need a way to free this type when we are 1757 done with it. */ 1758 struct type *array 1759 = lookup_array_range_type (value1.type, 0, length - 1); 1760 1761 value->kind = axs_lvalue_memory; 1762 value->type = array; 1763 } 1764 1765 void 1766 comma_operation::do_generate_ax (struct expression *exp, 1767 struct agent_expr *ax, 1768 struct axs_value *value, 1769 struct type *cast_type) 1770 { 1771 /* Note that we need to be a little subtle about generating code 1772 for comma. In C, we can do some optimizations here because 1773 we know the left operand is only being evaluated for effect. 1774 However, if the tracing kludge is in effect, then we always 1775 need to evaluate the left hand side fully, so that all the 1776 variables it mentions get traced. */ 1777 struct axs_value value1; 1778 std::get<0> (m_storage)->generate_ax (exp, ax, &value1); 1779 /* Don't just dispose of the left operand. We might be tracing, 1780 in which case we want to emit code to trace it if it's an 1781 lvalue. */ 1782 gen_traced_pop (ax, &value1); 1783 std::get<1> (m_storage)->generate_ax (exp, ax, value); 1784 /* It's the consumer's responsibility to trace the right operand. */ 1785 } 1786 1787 void 1788 unop_sizeof_operation::do_generate_ax (struct expression *exp, 1789 struct agent_expr *ax, 1790 struct axs_value *value, 1791 struct type *cast_type) 1792 { 1793 /* We don't care about the value of the operand expression; we only 1794 care about its type. However, in the current arrangement, the 1795 only way to find an expression's type is to generate code for it. 1796 So we generate code for the operand, and then throw it away, 1797 replacing it with code that simply pushes its size. */ 1798 int start = ax->len; 1799 1800 std::get<0> (m_storage)->generate_ax (exp, ax, value); 1801 1802 /* Throw away the code we just generated. */ 1803 ax->len = start; 1804 1805 ax_const_l (ax, value->type->length ()); 1806 value->kind = axs_rvalue; 1807 value->type = builtin_type (ax->gdbarch)->builtin_int; 1808 } 1809 1810 void 1811 unop_cast_operation::do_generate_ax (struct expression *exp, 1812 struct agent_expr *ax, 1813 struct axs_value *value, 1814 struct type *cast_type) 1815 { 1816 std::get<0> (m_storage)->generate_ax (exp, ax, value, 1817 std::get<1> (m_storage)); 1818 } 1819 1820 void 1821 unop_extract_operation::do_generate_ax (struct expression *exp, 1822 struct agent_expr *ax, 1823 struct axs_value *value, 1824 struct type *cast_type) 1825 { 1826 std::get<0> (m_storage)->generate_ax (exp, ax, value); 1827 1828 struct type *to_type = get_type (); 1829 1830 if (!is_scalar_type (to_type)) 1831 error (_("can't generate agent expression to extract non-scalar type")); 1832 1833 if (to_type->is_unsigned ()) 1834 gen_extend (ax, to_type); 1835 else 1836 gen_sign_extend (ax, to_type); 1837 } 1838 1839 void 1840 unop_memval_operation::do_generate_ax (struct expression *exp, 1841 struct agent_expr *ax, 1842 struct axs_value *value, 1843 struct type *cast_type) 1844 { 1845 std::get<0> (m_storage)->generate_ax (exp, ax, value); 1846 /* If we have an axs_rvalue or an axs_lvalue_memory, then we 1847 already have the right value on the stack. For 1848 axs_lvalue_register, we must convert. */ 1849 if (value->kind == axs_lvalue_register) 1850 require_rvalue (ax, value); 1851 1852 value->type = std::get<1> (m_storage); 1853 value->kind = axs_lvalue_memory; 1854 } 1855 1856 void 1857 unop_memval_type_operation::do_generate_ax (struct expression *exp, 1858 struct agent_expr *ax, 1859 struct axs_value *value, 1860 struct type *cast_type) 1861 { 1862 struct value *val 1863 = std::get<0> (m_storage)->evaluate (nullptr, exp, 1864 EVAL_AVOID_SIDE_EFFECTS); 1865 struct type *type = value_type (val); 1866 1867 std::get<1> (m_storage)->generate_ax (exp, ax, value); 1868 1869 /* If we have an axs_rvalue or an axs_lvalue_memory, then we 1870 already have the right value on the stack. For 1871 axs_lvalue_register, we must convert. */ 1872 if (value->kind == axs_lvalue_register) 1873 require_rvalue (ax, value); 1874 1875 value->type = type; 1876 value->kind = axs_lvalue_memory; 1877 } 1878 1879 void 1880 op_this_operation::do_generate_ax (struct expression *exp, 1881 struct agent_expr *ax, 1882 struct axs_value *value, 1883 struct type *cast_type) 1884 { 1885 struct symbol *sym, *func; 1886 const struct block *b; 1887 const struct language_defn *lang; 1888 1889 b = block_for_pc (ax->scope); 1890 func = block_linkage_function (b); 1891 lang = language_def (func->language ()); 1892 1893 sym = lookup_language_this (lang, b).symbol; 1894 if (!sym) 1895 error (_("no `%s' found"), lang->name_of_this ()); 1896 1897 gen_var_ref (ax, value, sym); 1898 1899 if (value->optimized_out) 1900 error (_("`%s' has been optimized out, cannot use"), 1901 sym->print_name ()); 1902 } 1903 1904 void 1905 assign_operation::do_generate_ax (struct expression *exp, 1906 struct agent_expr *ax, 1907 struct axs_value *value, 1908 struct type *cast_type) 1909 { 1910 operation *subop = std::get<0> (m_storage).get (); 1911 if (subop->opcode () != OP_INTERNALVAR) 1912 error (_("May only assign to trace state variables")); 1913 1914 internalvar_operation *ivarop 1915 = gdb::checked_static_cast<internalvar_operation *> (subop); 1916 1917 const char *name = internalvar_name (ivarop->get_internalvar ()); 1918 struct trace_state_variable *tsv; 1919 1920 std::get<1> (m_storage)->generate_ax (exp, ax, value); 1921 tsv = find_trace_state_variable (name); 1922 if (tsv) 1923 { 1924 ax_tsv (ax, aop_setv, tsv->number); 1925 if (ax->tracing) 1926 ax_tsv (ax, aop_tracev, tsv->number); 1927 } 1928 else 1929 error (_("$%s is not a trace state variable, " 1930 "may not assign to it"), name); 1931 } 1932 1933 void 1934 assign_modify_operation::do_generate_ax (struct expression *exp, 1935 struct agent_expr *ax, 1936 struct axs_value *value, 1937 struct type *cast_type) 1938 { 1939 operation *subop = std::get<1> (m_storage).get (); 1940 if (subop->opcode () != OP_INTERNALVAR) 1941 error (_("May only assign to trace state variables")); 1942 1943 internalvar_operation *ivarop 1944 = gdb::checked_static_cast<internalvar_operation *> (subop); 1945 1946 const char *name = internalvar_name (ivarop->get_internalvar ()); 1947 struct trace_state_variable *tsv; 1948 1949 tsv = find_trace_state_variable (name); 1950 if (tsv) 1951 { 1952 /* The tsv will be the left half of the binary operation. */ 1953 ax_tsv (ax, aop_getv, tsv->number); 1954 if (ax->tracing) 1955 ax_tsv (ax, aop_tracev, tsv->number); 1956 /* Trace state variables are always 64-bit integers. */ 1957 struct axs_value value1, value2; 1958 value1.kind = axs_rvalue; 1959 value1.type = builtin_type (ax->gdbarch)->builtin_long_long; 1960 /* Now do right half of expression. */ 1961 std::get<2> (m_storage)->generate_ax (exp, ax, &value2); 1962 gen_expr_binop_rest (exp, std::get<0> (m_storage), ax, 1963 value, &value1, &value2); 1964 /* We have a result of the binary op, set the tsv. */ 1965 ax_tsv (ax, aop_setv, tsv->number); 1966 if (ax->tracing) 1967 ax_tsv (ax, aop_tracev, tsv->number); 1968 } 1969 else 1970 error (_("$%s is not a trace state variable, " 1971 "may not assign to it"), name); 1972 } 1973 1974 void 1975 unop_cast_type_operation::do_generate_ax (struct expression *exp, 1976 struct agent_expr *ax, 1977 struct axs_value *value, 1978 struct type *cast_type) 1979 { 1980 struct value *val 1981 = std::get<0> (m_storage)->evaluate (nullptr, exp, 1982 EVAL_AVOID_SIDE_EFFECTS); 1983 std::get<1> (m_storage)->generate_ax (exp, ax, value, value_type (val)); 1984 } 1985 1986 void 1987 var_value_operation::do_generate_ax (struct expression *exp, 1988 struct agent_expr *ax, 1989 struct axs_value *value, 1990 struct type *cast_type) 1991 { 1992 gen_var_ref (ax, value, std::get<0> (m_storage).symbol); 1993 1994 if (value->optimized_out) 1995 error (_("`%s' has been optimized out, cannot use"), 1996 std::get<0> (m_storage).symbol->print_name ()); 1997 1998 if (value->type->code () == TYPE_CODE_ERROR) 1999 { 2000 if (cast_type == nullptr) 2001 error_unknown_type (std::get<0> (m_storage).symbol->print_name ()); 2002 value->type = cast_type; 2003 } 2004 } 2005 2006 void 2007 logical_and_operation::do_generate_ax (struct expression *exp, 2008 struct agent_expr *ax, 2009 struct axs_value *value, 2010 struct type *cast_type) 2011 { 2012 struct axs_value value1, value2; 2013 int if1, go1, if2, go2, end; 2014 2015 /* Generate the obvious sequence of tests and jumps. */ 2016 std::get<0> (m_storage)->generate_ax (exp, ax, &value1); 2017 gen_usual_unary (ax, &value1); 2018 if1 = ax_goto (ax, aop_if_goto); 2019 go1 = ax_goto (ax, aop_goto); 2020 ax_label (ax, if1, ax->len); 2021 std::get<1> (m_storage)->generate_ax (exp, ax, &value2); 2022 gen_usual_unary (ax, &value2); 2023 if2 = ax_goto (ax, aop_if_goto); 2024 go2 = ax_goto (ax, aop_goto); 2025 ax_label (ax, if2, ax->len); 2026 ax_const_l (ax, 1); 2027 end = ax_goto (ax, aop_goto); 2028 ax_label (ax, go1, ax->len); 2029 ax_label (ax, go2, ax->len); 2030 ax_const_l (ax, 0); 2031 ax_label (ax, end, ax->len); 2032 value->kind = axs_rvalue; 2033 value->type = builtin_type (ax->gdbarch)->builtin_int; 2034 } 2035 2036 void 2037 logical_or_operation::do_generate_ax (struct expression *exp, 2038 struct agent_expr *ax, 2039 struct axs_value *value, 2040 struct type *cast_type) 2041 { 2042 struct axs_value value1, value2; 2043 int if1, if2, end; 2044 2045 /* Generate the obvious sequence of tests and jumps. */ 2046 std::get<0> (m_storage)->generate_ax (exp, ax, &value1); 2047 gen_usual_unary (ax, &value1); 2048 if1 = ax_goto (ax, aop_if_goto); 2049 std::get<1> (m_storage)->generate_ax (exp, ax, &value2); 2050 gen_usual_unary (ax, &value2); 2051 if2 = ax_goto (ax, aop_if_goto); 2052 ax_const_l (ax, 0); 2053 end = ax_goto (ax, aop_goto); 2054 ax_label (ax, if1, ax->len); 2055 ax_label (ax, if2, ax->len); 2056 ax_const_l (ax, 1); 2057 ax_label (ax, end, ax->len); 2058 value->kind = axs_rvalue; 2059 value->type = builtin_type (ax->gdbarch)->builtin_int; 2060 } 2061 2062 } 2063 2064 /* This handles the middle-to-right-side of code generation for binary 2065 expressions, which is shared between regular binary operations and 2066 assign-modify (+= and friends) expressions. */ 2067 2068 static void 2069 gen_expr_binop_rest (struct expression *exp, 2070 enum exp_opcode op, 2071 struct agent_expr *ax, struct axs_value *value, 2072 struct axs_value *value1, struct axs_value *value2) 2073 { 2074 struct type *int_type = builtin_type (ax->gdbarch)->builtin_int; 2075 2076 gen_usual_unary (ax, value2); 2077 gen_usual_arithmetic (ax, value1, value2); 2078 switch (op) 2079 { 2080 case BINOP_ADD: 2081 if (value1->type->code () == TYPE_CODE_INT 2082 && value2->type->is_pointer_or_reference ()) 2083 { 2084 /* Swap the values and proceed normally. */ 2085 ax_simple (ax, aop_swap); 2086 gen_ptradd (ax, value, value2, value1); 2087 } 2088 else if (value1->type->is_pointer_or_reference () 2089 && value2->type->code () == TYPE_CODE_INT) 2090 gen_ptradd (ax, value, value1, value2); 2091 else 2092 gen_binop (ax, value, value1, value2, 2093 aop_add, aop_add, 1, "addition"); 2094 break; 2095 case BINOP_SUB: 2096 if (value1->type->is_pointer_or_reference () 2097 && value2->type->code () == TYPE_CODE_INT) 2098 gen_ptrsub (ax,value, value1, value2); 2099 else if (value1->type->is_pointer_or_reference () 2100 && value2->type->is_pointer_or_reference ()) 2101 /* FIXME --- result type should be ptrdiff_t */ 2102 gen_ptrdiff (ax, value, value1, value2, 2103 builtin_type (ax->gdbarch)->builtin_long); 2104 else 2105 gen_binop (ax, value, value1, value2, 2106 aop_sub, aop_sub, 1, "subtraction"); 2107 break; 2108 case BINOP_MUL: 2109 gen_binop (ax, value, value1, value2, 2110 aop_mul, aop_mul, 1, "multiplication"); 2111 break; 2112 case BINOP_DIV: 2113 gen_binop (ax, value, value1, value2, 2114 aop_div_signed, aop_div_unsigned, 1, "division"); 2115 break; 2116 case BINOP_REM: 2117 gen_binop (ax, value, value1, value2, 2118 aop_rem_signed, aop_rem_unsigned, 1, "remainder"); 2119 break; 2120 case BINOP_LSH: 2121 gen_binop (ax, value, value1, value2, 2122 aop_lsh, aop_lsh, 1, "left shift"); 2123 break; 2124 case BINOP_RSH: 2125 gen_binop (ax, value, value1, value2, 2126 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift"); 2127 break; 2128 case BINOP_SUBSCRIPT: 2129 { 2130 struct type *type; 2131 2132 if (binop_types_user_defined_p (op, value1->type, value2->type)) 2133 { 2134 error (_("cannot subscript requested type: " 2135 "cannot call user defined functions")); 2136 } 2137 else 2138 { 2139 /* If the user attempts to subscript something that is not 2140 an array or pointer type (like a plain int variable for 2141 example), then report this as an error. */ 2142 type = check_typedef (value1->type); 2143 if (type->code () != TYPE_CODE_ARRAY 2144 && type->code () != TYPE_CODE_PTR) 2145 { 2146 if (type->name ()) 2147 error (_("cannot subscript something of type `%s'"), 2148 type->name ()); 2149 else 2150 error (_("cannot subscript requested type")); 2151 } 2152 } 2153 2154 if (!is_integral_type (value2->type)) 2155 error (_("Argument to arithmetic operation " 2156 "not a number or boolean.")); 2157 2158 gen_ptradd (ax, value, value1, value2); 2159 gen_deref (value); 2160 break; 2161 } 2162 case BINOP_BITWISE_AND: 2163 gen_binop (ax, value, value1, value2, 2164 aop_bit_and, aop_bit_and, 0, "bitwise and"); 2165 break; 2166 2167 case BINOP_BITWISE_IOR: 2168 gen_binop (ax, value, value1, value2, 2169 aop_bit_or, aop_bit_or, 0, "bitwise or"); 2170 break; 2171 2172 case BINOP_BITWISE_XOR: 2173 gen_binop (ax, value, value1, value2, 2174 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or"); 2175 break; 2176 2177 case BINOP_EQUAL: 2178 gen_equal (ax, value, value1, value2, int_type); 2179 break; 2180 2181 case BINOP_NOTEQUAL: 2182 gen_equal (ax, value, value1, value2, int_type); 2183 gen_logical_not (ax, value, int_type); 2184 break; 2185 2186 case BINOP_LESS: 2187 gen_less (ax, value, value1, value2, int_type); 2188 break; 2189 2190 case BINOP_GTR: 2191 ax_simple (ax, aop_swap); 2192 gen_less (ax, value, value1, value2, int_type); 2193 break; 2194 2195 case BINOP_LEQ: 2196 ax_simple (ax, aop_swap); 2197 gen_less (ax, value, value1, value2, int_type); 2198 gen_logical_not (ax, value, int_type); 2199 break; 2200 2201 case BINOP_GEQ: 2202 gen_less (ax, value, value1, value2, int_type); 2203 gen_logical_not (ax, value, int_type); 2204 break; 2205 2206 default: 2207 /* We should only list operators in the outer case statement 2208 that we actually handle in the inner case statement. */ 2209 internal_error (_("gen_expr: op case sets don't match")); 2210 } 2211 } 2212 2213 /* A helper function that emits a binop based on two operations. */ 2214 2215 void 2216 gen_expr_binop (struct expression *exp, 2217 enum exp_opcode op, 2218 expr::operation *lhs, expr::operation *rhs, 2219 struct agent_expr *ax, struct axs_value *value) 2220 { 2221 struct axs_value value1, value2; 2222 2223 lhs->generate_ax (exp, ax, &value1); 2224 gen_usual_unary (ax, &value1); 2225 rhs->generate_ax (exp, ax, &value2); 2226 gen_expr_binop_rest (exp, op, ax, value, &value1, &value2); 2227 } 2228 2229 /* A helper function that emits a structop based on an operation and a 2230 member name. */ 2231 2232 void 2233 gen_expr_structop (struct expression *exp, 2234 enum exp_opcode op, 2235 expr::operation *lhs, 2236 const char *name, 2237 struct agent_expr *ax, struct axs_value *value) 2238 { 2239 lhs->generate_ax (exp, ax, value); 2240 if (op == STRUCTOP_STRUCT) 2241 gen_struct_ref (ax, value, name, ".", "structure or union"); 2242 else if (op == STRUCTOP_PTR) 2243 gen_struct_ref (ax, value, name, "->", 2244 "pointer to a structure or union"); 2245 else 2246 /* If this `if' chain doesn't handle it, then the case list 2247 shouldn't mention it, and we shouldn't be here. */ 2248 internal_error (_("gen_expr: unhandled struct case")); 2249 } 2250 2251 /* A helper function that emits a unary operation. */ 2252 2253 void 2254 gen_expr_unop (struct expression *exp, 2255 enum exp_opcode op, 2256 expr::operation *lhs, 2257 struct agent_expr *ax, struct axs_value *value) 2258 { 2259 struct axs_value value1, value2; 2260 2261 switch (op) 2262 { 2263 case UNOP_NEG: 2264 gen_int_literal (ax, &value1, 0, 2265 builtin_type (ax->gdbarch)->builtin_int); 2266 gen_usual_unary (ax, &value1); /* shouldn't do much */ 2267 lhs->generate_ax (exp, ax, &value2); 2268 gen_usual_unary (ax, &value2); 2269 gen_usual_arithmetic (ax, &value1, &value2); 2270 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation"); 2271 break; 2272 2273 case UNOP_PLUS: 2274 /* + FOO is equivalent to 0 + FOO, which can be optimized. */ 2275 lhs->generate_ax (exp, ax, value); 2276 gen_usual_unary (ax, value); 2277 break; 2278 2279 case UNOP_LOGICAL_NOT: 2280 lhs->generate_ax (exp, ax, value); 2281 gen_usual_unary (ax, value); 2282 gen_logical_not (ax, value, builtin_type (ax->gdbarch)->builtin_int); 2283 break; 2284 2285 case UNOP_COMPLEMENT: 2286 lhs->generate_ax (exp, ax, value); 2287 gen_usual_unary (ax, value); 2288 gen_integral_promotions (ax, value); 2289 gen_complement (ax, value); 2290 break; 2291 2292 case UNOP_IND: 2293 lhs->generate_ax (exp, ax, value); 2294 gen_usual_unary (ax, value); 2295 if (!value->type->is_pointer_or_reference ()) 2296 error (_("Argument of unary `*' is not a pointer.")); 2297 gen_deref (value); 2298 break; 2299 2300 case UNOP_ADDR: 2301 lhs->generate_ax (exp, ax, value); 2302 gen_address_of (value); 2303 break; 2304 2305 default: 2306 gdb_assert_not_reached ("invalid case in gen_expr_unop"); 2307 } 2308 } 2309 2310 2311 2312 /* Given a single variable and a scope, generate bytecodes to trace 2313 its value. This is for use in situations where we have only a 2314 variable's name, and no parsed expression; for instance, when the 2315 name comes from a list of local variables of a function. */ 2316 2317 agent_expr_up 2318 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch, 2319 struct symbol *var, int trace_string) 2320 { 2321 agent_expr_up ax (new agent_expr (gdbarch, scope)); 2322 struct axs_value value; 2323 2324 ax->tracing = 1; 2325 ax->trace_string = trace_string; 2326 gen_var_ref (ax.get (), &value, var); 2327 2328 /* If there is no actual variable to trace, flag it by returning 2329 an empty agent expression. */ 2330 if (value.optimized_out) 2331 return agent_expr_up (); 2332 2333 /* Make sure we record the final object, and get rid of it. */ 2334 gen_traced_pop (ax.get (), &value); 2335 2336 /* Oh, and terminate. */ 2337 ax_simple (ax.get (), aop_end); 2338 2339 return ax; 2340 } 2341 2342 /* Generating bytecode from GDB expressions: driver */ 2343 2344 /* Given a GDB expression EXPR, return bytecode to trace its value. 2345 The result will use the `trace' and `trace_quick' bytecodes to 2346 record the value of all memory touched by the expression. The 2347 caller can then use the ax_reqs function to discover which 2348 registers it relies upon. */ 2349 2350 agent_expr_up 2351 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr, 2352 int trace_string) 2353 { 2354 agent_expr_up ax (new agent_expr (expr->gdbarch, scope)); 2355 struct axs_value value; 2356 2357 ax->tracing = 1; 2358 ax->trace_string = trace_string; 2359 value.optimized_out = 0; 2360 expr->op->generate_ax (expr, ax.get (), &value); 2361 2362 /* Make sure we record the final object, and get rid of it. */ 2363 gen_traced_pop (ax.get (), &value); 2364 2365 /* Oh, and terminate. */ 2366 ax_simple (ax.get (), aop_end); 2367 2368 return ax; 2369 } 2370 2371 /* Given a GDB expression EXPR, return a bytecode sequence that will 2372 evaluate and return a result. The bytecodes will do a direct 2373 evaluation, using the current data on the target, rather than 2374 recording blocks of memory and registers for later use, as 2375 gen_trace_for_expr does. The generated bytecode sequence leaves 2376 the result of expression evaluation on the top of the stack. */ 2377 2378 agent_expr_up 2379 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr) 2380 { 2381 agent_expr_up ax (new agent_expr (expr->gdbarch, scope)); 2382 struct axs_value value; 2383 2384 ax->tracing = 0; 2385 value.optimized_out = 0; 2386 expr->op->generate_ax (expr, ax.get (), &value); 2387 2388 require_rvalue (ax.get (), &value); 2389 2390 /* Oh, and terminate. */ 2391 ax_simple (ax.get (), aop_end); 2392 2393 return ax; 2394 } 2395 2396 agent_expr_up 2397 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch, 2398 int trace_string) 2399 { 2400 agent_expr_up ax (new agent_expr (gdbarch, scope)); 2401 struct axs_value value; 2402 2403 ax->tracing = 1; 2404 ax->trace_string = trace_string; 2405 2406 gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope); 2407 2408 /* Make sure we record the final object, and get rid of it. */ 2409 gen_traced_pop (ax.get (), &value); 2410 2411 /* Oh, and terminate. */ 2412 ax_simple (ax.get (), aop_end); 2413 2414 return ax; 2415 } 2416 2417 /* Given a collection of printf-style arguments, generate code to 2418 evaluate the arguments and pass everything to a special 2419 bytecode. */ 2420 2421 agent_expr_up 2422 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch, 2423 CORE_ADDR function, LONGEST channel, 2424 const char *format, int fmtlen, 2425 int nargs, struct expression **exprs) 2426 { 2427 agent_expr_up ax (new agent_expr (gdbarch, scope)); 2428 struct axs_value value; 2429 int tem; 2430 2431 /* We're computing values, not doing side effects. */ 2432 ax->tracing = 0; 2433 2434 /* Evaluate and push the args on the stack in reverse order, 2435 for simplicity of collecting them on the target side. */ 2436 for (tem = nargs - 1; tem >= 0; --tem) 2437 { 2438 value.optimized_out = 0; 2439 exprs[tem]->op->generate_ax (exprs[tem], ax.get (), &value); 2440 require_rvalue (ax.get (), &value); 2441 } 2442 2443 /* Push function and channel. */ 2444 ax_const_l (ax.get (), channel); 2445 ax_const_l (ax.get (), function); 2446 2447 /* Issue the printf bytecode proper. */ 2448 ax_simple (ax.get (), aop_printf); 2449 ax_raw_byte (ax.get (), nargs); 2450 ax_string (ax.get (), format, fmtlen); 2451 2452 /* And terminate. */ 2453 ax_simple (ax.get (), aop_end); 2454 2455 return ax; 2456 } 2457 2458 static void 2459 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc) 2460 { 2461 const char *arg; 2462 int trace_string = 0; 2463 2464 if (!eval) 2465 { 2466 if (*exp == '/') 2467 exp = decode_agent_options (exp, &trace_string); 2468 } 2469 2470 agent_expr_up agent; 2471 2472 arg = exp; 2473 if (!eval && strcmp (arg, "$_ret") == 0) 2474 { 2475 agent = gen_trace_for_return_address (pc, get_current_arch (), 2476 trace_string); 2477 } 2478 else 2479 { 2480 expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0); 2481 2482 if (eval) 2483 { 2484 gdb_assert (trace_string == 0); 2485 agent = gen_eval_for_expr (pc, expr.get ()); 2486 } 2487 else 2488 agent = gen_trace_for_expr (pc, expr.get (), trace_string); 2489 } 2490 2491 ax_reqs (agent.get ()); 2492 ax_print (gdb_stdout, agent.get ()); 2493 2494 /* It would be nice to call ax_reqs here to gather some general info 2495 about the expression, and then print out the result. */ 2496 2497 dont_repeat (); 2498 } 2499 2500 static void 2501 maint_agent_command_1 (const char *exp, int eval) 2502 { 2503 /* We don't deal with overlay debugging at the moment. We need to 2504 think more carefully about this. If you copy this code into 2505 another command, change the error message; the user shouldn't 2506 have to know anything about agent expressions. */ 2507 if (overlay_debugging) 2508 error (_("GDB can't do agent expression translation with overlays.")); 2509 2510 if (exp == 0) 2511 error_no_arg (_("expression to translate")); 2512 2513 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1)) 2514 { 2515 struct linespec_result canonical; 2516 2517 location_spec_up locspec 2518 = new_linespec_location_spec (&exp, symbol_name_match_type::WILD); 2519 decode_line_full (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL, 2520 NULL, 0, &canonical, 2521 NULL, NULL); 2522 exp = skip_spaces (exp); 2523 if (exp[0] == ',') 2524 { 2525 exp++; 2526 exp = skip_spaces (exp); 2527 } 2528 for (const auto &lsal : canonical.lsals) 2529 for (const auto &sal : lsal.sals) 2530 agent_eval_command_one (exp, eval, sal.pc); 2531 } 2532 else 2533 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ())); 2534 2535 dont_repeat (); 2536 } 2537 2538 static void 2539 maint_agent_command (const char *exp, int from_tty) 2540 { 2541 maint_agent_command_1 (exp, 0); 2542 } 2543 2544 /* Parse the given expression, compile it into an agent expression 2545 that does direct evaluation, and display the resulting 2546 expression. */ 2547 2548 static void 2549 maint_agent_eval_command (const char *exp, int from_tty) 2550 { 2551 maint_agent_command_1 (exp, 1); 2552 } 2553 2554 /* Parse the given expression, compile it into an agent expression 2555 that does a printf, and display the resulting expression. */ 2556 2557 static void 2558 maint_agent_printf_command (const char *cmdrest, int from_tty) 2559 { 2560 frame_info_ptr fi = get_current_frame (); /* need current scope */ 2561 const char *format_start, *format_end; 2562 2563 /* We don't deal with overlay debugging at the moment. We need to 2564 think more carefully about this. If you copy this code into 2565 another command, change the error message; the user shouldn't 2566 have to know anything about agent expressions. */ 2567 if (overlay_debugging) 2568 error (_("GDB can't do agent expression translation with overlays.")); 2569 2570 if (cmdrest == 0) 2571 error_no_arg (_("expression to translate")); 2572 2573 cmdrest = skip_spaces (cmdrest); 2574 2575 if (*cmdrest++ != '"') 2576 error (_("Must start with a format string.")); 2577 2578 format_start = cmdrest; 2579 2580 format_pieces fpieces (&cmdrest); 2581 2582 format_end = cmdrest; 2583 2584 if (*cmdrest++ != '"') 2585 error (_("Bad format string, non-terminated '\"'.")); 2586 2587 cmdrest = skip_spaces (cmdrest); 2588 2589 if (*cmdrest != ',' && *cmdrest != 0) 2590 error (_("Invalid argument syntax")); 2591 2592 if (*cmdrest == ',') 2593 cmdrest++; 2594 cmdrest = skip_spaces (cmdrest); 2595 2596 std::vector<struct expression *> argvec; 2597 while (*cmdrest != '\0') 2598 { 2599 const char *cmd1; 2600 2601 cmd1 = cmdrest; 2602 expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1); 2603 argvec.push_back (expr.release ()); 2604 cmdrest = cmd1; 2605 if (*cmdrest == ',') 2606 ++cmdrest; 2607 /* else complain? */ 2608 } 2609 2610 2611 agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (), 2612 0, 0, 2613 format_start, format_end - format_start, 2614 argvec.size (), argvec.data ()); 2615 ax_reqs (agent.get ()); 2616 ax_print (gdb_stdout, agent.get ()); 2617 2618 /* It would be nice to call ax_reqs here to gather some general info 2619 about the expression, and then print out the result. */ 2620 2621 dont_repeat (); 2622 } 2623 2624 /* Initialization code. */ 2625 2626 void _initialize_ax_gdb (); 2627 void 2628 _initialize_ax_gdb () 2629 { 2630 add_cmd ("agent", class_maintenance, maint_agent_command, 2631 _("\ 2632 Translate an expression into remote agent bytecode for tracing.\n\ 2633 Usage: maint agent [-at LOCATION,] EXPRESSION\n\ 2634 If -at is given, generate remote agent bytecode for this location.\n\ 2635 If not, generate remote agent bytecode for current frame pc address."), 2636 &maintenancelist); 2637 2638 add_cmd ("agent-eval", class_maintenance, maint_agent_eval_command, 2639 _("\ 2640 Translate an expression into remote agent bytecode for evaluation.\n\ 2641 Usage: maint agent-eval [-at LOCATION,] EXPRESSION\n\ 2642 If -at is given, generate remote agent bytecode for this location.\n\ 2643 If not, generate remote agent bytecode for current frame pc address."), 2644 &maintenancelist); 2645 2646 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command, 2647 _("Translate an expression into remote " 2648 "agent bytecode for evaluation and display the bytecodes."), 2649 &maintenancelist); 2650 } 2651