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