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