1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998-2024 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/>. */
26 #include "expression.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"
45 #include "typeprint.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
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
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
,
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
,
123 enum agent_op op_unsigned
, int may_carry
,
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
,
139 static void gen_struct_ref (struct agent_expr
*ax
,
140 struct axs_value
*value
,
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
,
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
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
187 - All values on the stack should always be fully zero- or
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. */
203 gen_trace_static_fields (struct agent_expr
*ax
,
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 (type
->field (i
).is_static ())
215 gen_static_field (ax
, &value
, type
, i
);
216 if (value
.optimized_out
)
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
, value
.type
->length ());
225 ax_simple (ax
, aop_trace
);
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
);
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. */
253 gen_traced_pop (struct agent_expr
*ax
, struct axs_value
*value
)
255 int string_trace
= 0;
257 && value
->type
->code () == TYPE_CODE_PTR
258 && c_textual_element_type (check_typedef (value
->type
->target_type ()),
268 ax_const_l (ax
, ax
->trace_string
);
269 ax_simple (ax
, aop_tracenz
);
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
);
277 case axs_lvalue_memory
:
279 /* Initialize the TYPE_LENGTH if it is a typedef. */
280 check_typedef (value
->type
);
284 gen_fetch (ax
, value
->type
);
285 ax_const_l (ax
, ax
->trace_string
);
286 ax_simple (ax
, aop_tracenz
);
290 /* There's no point in trying to use a trace_quick bytecode
291 here, since "trace_quick SIZE pop" is three bytes, whereas
292 "const8 SIZE trace" is also three bytes, does the same
293 thing, and the simplest code which generates that will also
294 work correctly for objects with large sizes. */
295 ax_const_l (ax
, value
->type
->length ());
296 ax_simple (ax
, aop_trace
);
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. */
312 ax_reg (ax
, value
->u
.reg
);
313 ax_const_l (ax
, ax
->trace_string
);
314 ax_simple (ax
, aop_tracenz
);
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. */
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. */
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 () * 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
348 gen_extend (struct agent_expr
*ax
, struct type
*type
)
350 int bits
= type
->length () * TARGET_CHAR_BIT
;
353 ((type
->is_unsigned () ? ax_zero_ext
: ax_ext
) (ax
, bits
));
356 /* A helper that returns the target type if TYPE is a range type, or
357 otherwise just returns TYPE. */
360 strip_range_type (struct type
*type
)
362 if (type
->code () == TYPE_CODE_RANGE
)
363 return type
->target_type ();
367 /* Assume that the top of the stack contains a value of type "pointer
368 to TYPE"; generate code to fetch its value. Note that TYPE is the
369 target type, not the pointer type. */
371 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
375 /* Record the area of memory we're about to fetch. */
376 ax_trace_quick (ax
, type
->length ());
379 type
= strip_range_type (type
);
381 switch (type
->code ())
385 case TYPE_CODE_RVALUE_REF
:
390 /* It's a scalar value, so we know how to dereference it. How
391 many bytes long is it? */
392 switch (type
->length ())
394 case 8 / TARGET_CHAR_BIT
:
395 ax_simple (ax
, aop_ref8
);
397 case 16 / TARGET_CHAR_BIT
:
398 ax_simple (ax
, aop_ref16
);
400 case 32 / TARGET_CHAR_BIT
:
401 ax_simple (ax
, aop_ref32
);
403 case 64 / TARGET_CHAR_BIT
:
404 ax_simple (ax
, aop_ref64
);
407 /* Either our caller shouldn't have asked us to dereference
408 that pointer (other code's fault), or we're not
409 implementing something we should be (this code's fault).
410 In any case, it's a bug the user shouldn't see. */
412 internal_error (_("gen_fetch: strange size"));
415 gen_sign_extend (ax
, type
);
419 /* Our caller requested us to dereference a pointer from an unsupported
420 type. Error out and give callers a chance to handle the failure
422 error (_("gen_fetch: Unsupported type code `%s'."),
428 /* Generate code to left shift the top of the stack by DISTANCE bits, or
429 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
430 unsigned (logical) right shifts. */
432 gen_left_shift (struct agent_expr
*ax
, int distance
)
436 ax_const_l (ax
, distance
);
437 ax_simple (ax
, aop_lsh
);
439 else if (distance
< 0)
441 ax_const_l (ax
, -distance
);
442 ax_simple (ax
, aop_rsh_unsigned
);
448 /* Generating bytecode from GDB expressions: symbol references */
450 /* Generate code to push the base address of the argument portion of
451 the top stack frame. */
453 gen_frame_args_address (struct agent_expr
*ax
)
456 LONGEST frame_offset
;
458 gdbarch_virtual_frame_pointer (ax
->gdbarch
,
459 ax
->scope
, &frame_reg
, &frame_offset
);
460 ax_reg (ax
, frame_reg
);
461 gen_offset (ax
, frame_offset
);
465 /* Generate code to push the base address of the locals portion of the
468 gen_frame_locals_address (struct agent_expr
*ax
)
471 LONGEST frame_offset
;
473 gdbarch_virtual_frame_pointer (ax
->gdbarch
,
474 ax
->scope
, &frame_reg
, &frame_offset
);
475 ax_reg (ax
, frame_reg
);
476 gen_offset (ax
, frame_offset
);
480 /* Generate code to add OFFSET to the top of the stack. Try to
481 generate short and readable code. We use this for getting to
482 variables on the stack, and structure members. If we were
483 programming in ML, it would be clearer why these are the same
486 gen_offset (struct agent_expr
*ax
, int offset
)
488 /* It would suffice to simply push the offset and add it, but this
489 makes it easier to read positive and negative offsets in the
493 ax_const_l (ax
, offset
);
494 ax_simple (ax
, aop_add
);
498 ax_const_l (ax
, -offset
);
499 ax_simple (ax
, aop_sub
);
504 /* In many cases, a symbol's value is the offset from some other
505 address (stack frame, base register, etc.) Generate code to add
506 VAR's value to the top of the stack. */
508 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
510 gen_offset (ax
, var
->value_longest ());
514 /* Generate code for a variable reference to AX. The variable is the
515 symbol VAR. Set VALUE to describe the result. */
518 gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
, struct symbol
*var
)
520 /* Dereference any typedefs. */
521 value
->type
= check_typedef (var
->type ());
522 value
->optimized_out
= 0;
524 if (const symbol_computed_ops
*computed_ops
= var
->computed_ops ();
525 computed_ops
!= nullptr)
526 return computed_ops
->tracepoint_var_ref (var
, ax
, value
);
528 /* I'm imitating the code in read_var_value. */
529 switch (var
->aclass ())
531 case LOC_CONST
: /* A constant, like an enum value. */
532 ax_const_l (ax
, (LONGEST
) var
->value_longest ());
533 value
->kind
= axs_rvalue
;
536 case LOC_LABEL
: /* A goto label, being used as a value. */
537 ax_const_l (ax
, (LONGEST
) var
->value_address ());
538 value
->kind
= axs_rvalue
;
541 case LOC_CONST_BYTES
:
542 internal_error (_("gen_var_ref: LOC_CONST_BYTES "
543 "symbols are not supported"));
545 /* Variable at a fixed location in memory. Easy. */
547 /* Push the address of the variable. */
548 ax_const_l (ax
, var
->value_address ());
549 value
->kind
= axs_lvalue_memory
;
552 case LOC_ARG
: /* var lives in argument area of frame */
553 gen_frame_args_address (ax
);
554 gen_sym_offset (ax
, var
);
555 value
->kind
= axs_lvalue_memory
;
558 case LOC_REF_ARG
: /* As above, but the frame slot really
559 holds the address of the variable. */
560 gen_frame_args_address (ax
);
561 gen_sym_offset (ax
, var
);
562 /* Don't assume any particular pointer size. */
563 gen_fetch (ax
, builtin_type (ax
->gdbarch
)->builtin_data_ptr
);
564 value
->kind
= axs_lvalue_memory
;
567 case LOC_LOCAL
: /* var lives in locals area of frame */
568 gen_frame_locals_address (ax
);
569 gen_sym_offset (ax
, var
);
570 value
->kind
= axs_lvalue_memory
;
574 error (_("Cannot compute value of typedef `%s'."),
579 ax_const_l (ax
, var
->value_block ()->entry_pc ());
580 value
->kind
= axs_rvalue
;
584 /* Don't generate any code at all; in the process of treating
585 this as an lvalue or rvalue, the caller will generate the
587 value
->kind
= axs_lvalue_register
;
588 value
->u
.reg
= var
->register_ops ()->register_number (var
, ax
->gdbarch
);
591 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
592 register, not on the stack. Simpler than LOC_REGISTER
593 because it's just like any other case where the thing
594 has a real address. */
595 case LOC_REGPARM_ADDR
:
596 ax_reg (ax
, var
->register_ops ()->register_number (var
, ax
->gdbarch
));
597 value
->kind
= axs_lvalue_memory
;
602 struct bound_minimal_symbol msym
603 = lookup_minimal_symbol (var
->linkage_name (), NULL
, NULL
);
606 error (_("Couldn't resolve symbol `%s'."), var
->print_name ());
608 /* Push the address of the variable. */
609 ax_const_l (ax
, msym
.value_address ());
610 value
->kind
= axs_lvalue_memory
;
615 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
617 case LOC_OPTIMIZED_OUT
:
618 /* Flag this, but don't say anything; leave it up to callers to
620 value
->optimized_out
= 1;
624 error (_("Cannot find value of botched symbol `%s'."),
630 /* Generate code for a minimal symbol variable reference to AX. The
631 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
635 gen_msym_var_ref (agent_expr
*ax
, axs_value
*value
,
636 minimal_symbol
*msymbol
, objfile
*objf
)
639 type
*t
= find_minsym_type_and_address (msymbol
, objf
, &address
);
641 value
->optimized_out
= false;
642 ax_const_l (ax
, address
);
643 value
->kind
= axs_lvalue_memory
;
649 /* Generating bytecode from GDB expressions: literals */
652 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
656 value
->kind
= axs_rvalue
;
657 value
->type
= check_typedef (type
);
662 /* Generating bytecode from GDB expressions: unary conversions, casts */
664 /* Take what's on the top of the stack (as described by VALUE), and
665 try to make an rvalue out of it. Signal an error if we can't do
668 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
670 /* Only deal with scalars, structs and such may be too large
671 to fit in a stack entry. */
672 value
->type
= check_typedef (value
->type
);
673 if (value
->type
->code () == TYPE_CODE_ARRAY
674 || value
->type
->code () == TYPE_CODE_STRUCT
675 || value
->type
->code () == TYPE_CODE_UNION
676 || value
->type
->code () == TYPE_CODE_FUNC
)
677 error (_("Value not scalar: cannot be an rvalue."));
682 /* It's already an rvalue. */
685 case axs_lvalue_memory
:
686 /* The top of stack is the address of the object. Dereference. */
687 gen_fetch (ax
, value
->type
);
690 case axs_lvalue_register
:
691 /* There's nothing on the stack, but value->u.reg is the
692 register number containing the value.
694 When we add floating-point support, this is going to have to
695 change. What about SPARC register pairs, for example? */
696 ax_reg (ax
, value
->u
.reg
);
697 gen_extend (ax
, value
->type
);
701 value
->kind
= axs_rvalue
;
705 /* Assume the top of the stack is described by VALUE, and perform the
706 usual unary conversions. This is motivated by ANSI 6.2.2, but of
707 course GDB expressions are not ANSI; they're the mishmash union of
708 a bunch of languages. Rah.
710 NOTE! This function promises to produce an rvalue only when the
711 incoming value is of an appropriate type. In other words, the
712 consumer of the value this function produces may assume the value
713 is an rvalue only after checking its type.
715 The immediate issue is that if the user tries to use a structure or
716 union as an operand of, say, the `+' operator, we don't want to try
717 to convert that structure to an rvalue; require_rvalue will bomb on
718 structs and unions. Rather, we want to simply pass the struct
719 lvalue through unchanged, and let `+' raise an error. */
722 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
724 /* We don't have to generate any code for the usual integral
725 conversions, since values are always represented as full-width on
726 the stack. Should we tweak the type? */
728 /* Some types require special handling. */
729 switch (value
->type
->code ())
731 /* Functions get converted to a pointer to the function. */
733 value
->type
= lookup_pointer_type (value
->type
);
734 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
737 /* Arrays get converted to a pointer to their first element, and
738 are no longer an lvalue. */
739 case TYPE_CODE_ARRAY
:
741 struct type
*elements
= value
->type
->target_type ();
743 value
->type
= lookup_pointer_type (elements
);
744 value
->kind
= axs_rvalue
;
745 /* We don't need to generate any code; the address of the array
746 is also the address of its first element. */
750 /* Don't try to convert structures and unions to rvalues. Let the
751 consumer signal an error. */
752 case TYPE_CODE_STRUCT
:
753 case TYPE_CODE_UNION
:
757 /* If the value is an lvalue, dereference it. */
758 require_rvalue (ax
, value
);
762 /* Return non-zero iff the type TYPE1 is considered "wider" than the
763 type TYPE2, according to the rules described in gen_usual_arithmetic. */
765 type_wider_than (struct type
*type1
, struct type
*type2
)
767 return (type1
->length () > type2
->length ()
768 || (type1
->length () == type2
->length ()
769 && type1
->is_unsigned ()
770 && !type2
->is_unsigned ()));
774 /* Return the "wider" of the two types TYPE1 and TYPE2. */
776 max_type (struct type
*type1
, struct type
*type2
)
778 return type_wider_than (type1
, type2
) ? type1
: type2
;
782 /* Generate code to convert a scalar value of type FROM to type TO. */
784 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
786 /* Perhaps there is a more graceful way to state these rules. */
788 /* If we're converting to a narrower type, then we need to clear out
790 if (to
->length () < from
->length ())
793 /* If the two values have equal width, but different signednesses,
794 then we need to extend. */
795 else if (to
->length () == from
->length ())
797 if (from
->is_unsigned () != to
->is_unsigned ())
801 /* If we're converting to a wider type, and becoming unsigned, then
802 we need to zero out any possible sign bits. */
803 else if (to
->length () > from
->length ())
805 if (to
->is_unsigned ())
811 /* Return non-zero iff the type FROM will require any bytecodes to be
812 emitted to be converted to the type TO. */
814 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
816 agent_expr_up
ax (new agent_expr (NULL
, 0));
818 /* Actually generate the code, and see if anything came out. At the
819 moment, it would be trivial to replicate the code in
820 gen_conversion here, but in the future, when we're supporting
821 floating point and the like, it may not be. Doing things this
822 way allows this function to be independent of the logic in
824 gen_conversion (ax
.get (), from
, to
);
825 return !ax
->buf
.empty ();
829 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
830 6.2.1.5) for the two operands of an arithmetic operator. This
831 effectively finds a "least upper bound" type for the two arguments,
832 and promotes each argument to that type. *VALUE1 and *VALUE2
833 describe the values as they are passed in, and as they are left. */
835 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
836 struct axs_value
*value2
)
838 struct type
*type1
= strip_range_type (value1
->type
);
839 struct type
*type2
= strip_range_type (value2
->type
);
841 /* Do the usual binary conversions. */
842 if (type1
->code () == TYPE_CODE_INT
843 && type2
->code () == TYPE_CODE_INT
)
845 /* The ANSI integral promotions seem to work this way: Order the
846 integer types by size, and then by signedness: an n-bit
847 unsigned type is considered "wider" than an n-bit signed
848 type. Promote to the "wider" of the two types, and always
849 promote at least to int. */
850 struct type
*target
= max_type (builtin_type (ax
->gdbarch
)->builtin_int
,
851 max_type (type1
, type2
));
853 /* Deal with value2, on the top of the stack. */
854 gen_conversion (ax
, type2
, target
);
856 /* Deal with value1, not on the top of the stack. Don't
857 generate the `swap' instructions if we're not actually going
859 if (is_nontrivial_conversion (type1
, target
))
861 ax_simple (ax
, aop_swap
);
862 gen_conversion (ax
, type1
, target
);
863 ax_simple (ax
, aop_swap
);
866 value1
->type
= value2
->type
= check_typedef (target
);
871 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
872 the value on the top of the stack, as described by VALUE. Assume
873 the value has integral type. */
875 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
877 const struct builtin_type
*builtin
= builtin_type (ax
->gdbarch
);
879 if (!type_wider_than (value
->type
, builtin
->builtin_int
))
881 gen_conversion (ax
, value
->type
, builtin
->builtin_int
);
882 value
->type
= builtin
->builtin_int
;
884 else if (!type_wider_than (value
->type
, builtin
->builtin_unsigned_int
))
886 gen_conversion (ax
, value
->type
, builtin
->builtin_unsigned_int
);
887 value
->type
= builtin
->builtin_unsigned_int
;
892 /* Generate code for a cast to TYPE. */
894 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
896 /* GCC does allow casts to yield lvalues, so this should be fixed
897 before merging these changes into the trunk. */
898 require_rvalue (ax
, value
);
899 /* Dereference typedefs. */
900 type
= check_typedef (type
);
901 type
= strip_range_type (type
);
903 switch (type
->code ())
907 case TYPE_CODE_RVALUE_REF
:
908 /* It's implementation-defined, and I'll bet this is what GCC
912 case TYPE_CODE_ARRAY
:
913 case TYPE_CODE_STRUCT
:
914 case TYPE_CODE_UNION
:
916 error (_("Invalid type cast: intended type must be scalar."));
920 /* We don't have to worry about the size of the value, because
921 all our integral values are fully sign-extended, and when
922 casting pointers we can do anything we like. Is there any
923 way for us to know what GCC actually does with a cast like
928 gen_conversion (ax
, value
->type
, type
);
932 /* We could pop the value, and rely on everyone else to check
933 the type and notice that this value doesn't occupy a stack
934 slot. But for now, leave the value on the stack, and
935 preserve the "value == stack element" assumption. */
939 error (_("Casts to requested type are not yet implemented."));
947 /* Generating bytecode from GDB expressions: arithmetic */
949 /* Scale the integer on the top of the stack by the size of the target
950 of the pointer type TYPE. */
952 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
954 struct type
*element
= type
->target_type ();
956 if (element
->length () != 1)
958 ax_const_l (ax
, element
->length ());
964 /* Generate code for pointer arithmetic PTR + INT. */
966 gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
967 struct axs_value
*value1
, struct axs_value
*value2
)
969 gdb_assert (value1
->type
->is_pointer_or_reference ());
970 gdb_assert (strip_range_type (value2
->type
)->code () == TYPE_CODE_INT
);
972 gen_scale (ax
, aop_mul
, value1
->type
);
973 ax_simple (ax
, aop_add
);
974 gen_extend (ax
, value1
->type
); /* Catch overflow. */
975 value
->type
= value1
->type
;
976 value
->kind
= axs_rvalue
;
980 /* Generate code for pointer arithmetic PTR - INT. */
982 gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
983 struct axs_value
*value1
, struct axs_value
*value2
)
985 gdb_assert (value1
->type
->is_pointer_or_reference ());
986 gdb_assert (strip_range_type (value2
->type
)->code () == TYPE_CODE_INT
);
988 gen_scale (ax
, aop_mul
, value1
->type
);
989 ax_simple (ax
, aop_sub
);
990 gen_extend (ax
, value1
->type
); /* Catch overflow. */
991 value
->type
= value1
->type
;
992 value
->kind
= axs_rvalue
;
996 /* Generate code for pointer arithmetic PTR - PTR. */
998 gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
999 struct axs_value
*value1
, struct axs_value
*value2
,
1000 struct type
*result_type
)
1002 gdb_assert (value1
->type
->is_pointer_or_reference ());
1003 gdb_assert (value2
->type
->is_pointer_or_reference ());
1005 if (value1
->type
->target_type ()->length ()
1006 != value2
->type
->target_type ()->length ())
1008 First argument of `-' is a pointer, but second argument is neither\n\
1009 an integer nor a pointer of the same type."));
1011 ax_simple (ax
, aop_sub
);
1012 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1013 value
->type
= result_type
;
1014 value
->kind
= axs_rvalue
;
1018 gen_equal (struct agent_expr
*ax
, struct axs_value
*value
,
1019 struct axs_value
*value1
, struct axs_value
*value2
,
1020 struct type
*result_type
)
1022 if (value1
->type
->is_pointer_or_reference () || value2
->type
->is_pointer_or_reference ())
1023 ax_simple (ax
, aop_equal
);
1025 gen_binop (ax
, value
, value1
, value2
,
1026 aop_equal
, aop_equal
, 0, "equal");
1027 value
->type
= result_type
;
1028 value
->kind
= axs_rvalue
;
1032 gen_less (struct agent_expr
*ax
, struct axs_value
*value
,
1033 struct axs_value
*value1
, struct axs_value
*value2
,
1034 struct type
*result_type
)
1036 if (value1
->type
->is_pointer_or_reference () || value2
->type
->is_pointer_or_reference ())
1037 ax_simple (ax
, aop_less_unsigned
);
1039 gen_binop (ax
, value
, value1
, value2
,
1040 aop_less_signed
, aop_less_unsigned
, 0, "less than");
1041 value
->type
= result_type
;
1042 value
->kind
= axs_rvalue
;
1045 /* Generate code for a binary operator that doesn't do pointer magic.
1046 We set VALUE to describe the result value; we assume VALUE1 and
1047 VALUE2 describe the two operands, and that they've undergone the
1048 usual binary conversions. MAY_CARRY should be non-zero iff the
1049 result needs to be extended. NAME is the English name of the
1050 operator, used in error messages */
1052 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1053 struct axs_value
*value1
, struct axs_value
*value2
,
1054 enum agent_op op
, enum agent_op op_unsigned
,
1055 int may_carry
, const char *name
)
1057 /* We only handle INT op INT. */
1058 struct type
*type1
= strip_range_type (value1
->type
);
1059 if ((type1
->code () != TYPE_CODE_INT
)
1060 || (strip_range_type (value2
->type
)->code () != TYPE_CODE_INT
))
1061 error (_("Invalid combination of types in %s."), name
);
1063 ax_simple (ax
, type1
->is_unsigned () ? op_unsigned
: op
);
1065 gen_extend (ax
, type1
); /* catch overflow */
1066 value
->type
= type1
;
1067 value
->kind
= axs_rvalue
;
1072 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
1073 struct type
*result_type
)
1075 struct type
*type
= strip_range_type (value
->type
);
1076 if (type
->code () != TYPE_CODE_INT
1077 && type
->code () != TYPE_CODE_PTR
)
1078 error (_("Invalid type of operand to `!'."));
1080 ax_simple (ax
, aop_log_not
);
1081 value
->type
= result_type
;
1086 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1088 struct type
*type
= strip_range_type (value
->type
);
1089 if (type
->code () != TYPE_CODE_INT
)
1090 error (_("Invalid type of operand to `~'."));
1092 ax_simple (ax
, aop_bit_not
);
1093 gen_extend (ax
, type
);
1098 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1100 /* Dereference the value on the top of the stack. */
1102 gen_deref (struct axs_value
*value
)
1104 /* The caller should check the type, because several operators use
1105 this, and we don't know what error message to generate. */
1106 if (!value
->type
->is_pointer_or_reference ())
1107 internal_error (_("gen_deref: expected a pointer"));
1109 /* We've got an rvalue now, which is a pointer. We want to yield an
1110 lvalue, whose address is exactly that pointer. So we don't
1111 actually emit any code; we just change the type from "Pointer to
1112 T" to "T", and mark the value as an lvalue in memory. Leave it
1113 to the consumer to actually dereference it. */
1114 value
->type
= check_typedef (value
->type
->target_type ());
1115 if (value
->type
->code () == TYPE_CODE_VOID
)
1116 error (_("Attempt to dereference a generic pointer."));
1117 value
->kind
= ((value
->type
->code () == TYPE_CODE_FUNC
)
1118 ? axs_rvalue
: axs_lvalue_memory
);
1122 /* Produce the address of the lvalue on the top of the stack. */
1124 gen_address_of (struct axs_value
*value
)
1126 /* Special case for taking the address of a function. The ANSI
1127 standard describes this as a special case, too, so this
1128 arrangement is not without motivation. */
1129 if (value
->type
->code () == TYPE_CODE_FUNC
)
1130 /* The value's already an rvalue on the stack, so we just need to
1132 value
->type
= lookup_pointer_type (value
->type
);
1134 switch (value
->kind
)
1137 error (_("Operand of `&' is an rvalue, which has no address."));
1139 case axs_lvalue_register
:
1140 error (_("Operand of `&' is in a register, and has no address."));
1142 case axs_lvalue_memory
:
1143 value
->kind
= axs_rvalue
;
1144 value
->type
= lookup_pointer_type (value
->type
);
1149 /* Generate code to push the value of a bitfield of a structure whose
1150 address is on the top of the stack. START and END give the
1151 starting and one-past-ending *bit* numbers of the field within the
1154 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1155 struct type
*type
, int start
, int end
)
1157 /* Note that ops[i] fetches 8 << i bits. */
1158 static enum agent_op ops
[]
1159 = {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1160 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1162 /* We don't want to touch any byte that the bitfield doesn't
1163 actually occupy; we shouldn't make any accesses we're not
1164 explicitly permitted to. We rely here on the fact that the
1165 bytecode `ref' operators work on unaligned addresses.
1167 It takes some fancy footwork to get the stack to work the way
1168 we'd like. Say we're retrieving a bitfield that requires three
1169 fetches. Initially, the stack just contains the address:
1171 For the first fetch, we duplicate the address
1173 then add the byte offset, do the fetch, and shift and mask as
1174 needed, yielding a fragment of the value, properly aligned for
1175 the final bitwise or:
1177 then we swap, and repeat the process:
1178 frag1 addr --- address on top
1179 frag1 addr addr --- duplicate it
1180 frag1 addr frag2 --- get second fragment
1181 frag1 frag2 addr --- swap again
1182 frag1 frag2 frag3 --- get third fragment
1183 Notice that, since the third fragment is the last one, we don't
1184 bother duplicating the address this time. Now we have all the
1185 fragments on the stack, and we can simply `or' them together,
1186 yielding the final value of the bitfield. */
1188 /* The first and one-after-last bits in the field, but rounded down
1189 and up to byte boundaries. */
1190 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1191 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1195 /* current bit offset within the structure */
1198 /* The index in ops of the opcode we're considering. */
1201 /* The number of fragments we generated in the process. Probably
1202 equal to the number of `one' bits in bytesize, but who cares? */
1205 /* Dereference any typedefs. */
1206 type
= check_typedef (type
);
1208 /* Can we fetch the number of bits requested at all? */
1209 if ((end
- start
) > ((1 << num_ops
) * 8))
1210 internal_error (_("gen_bitfield_ref: bitfield too wide"));
1212 /* Note that we know here that we only need to try each opcode once.
1213 That may not be true on machines with weird byte sizes. */
1214 offset
= bound_start
;
1216 for (op
= num_ops
- 1; op
>= 0; op
--)
1218 /* number of bits that ops[op] would fetch */
1219 int op_size
= 8 << op
;
1221 /* The stack at this point, from bottom to top, contains zero or
1222 more fragments, then the address. */
1224 /* Does this fetch fit within the bitfield? */
1225 if (offset
+ op_size
<= bound_end
)
1227 /* Is this the last fragment? */
1228 int last_frag
= (offset
+ op_size
== bound_end
);
1231 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1233 /* Add the offset. */
1234 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1238 /* Record the area of memory we're about to fetch. */
1239 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1242 /* Perform the fetch. */
1243 ax_simple (ax
, ops
[op
]);
1245 /* Shift the bits we have to their proper position.
1246 gen_left_shift will generate right shifts when the operand
1249 A big-endian field diagram to ponder:
1250 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1251 +------++------++------++------++------++------++------++------+
1252 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1254 bit number 16 32 48 53
1255 These are bit numbers as supplied by GDB. Note that the
1256 bit numbers run from right to left once you've fetched the
1259 A little-endian field diagram to ponder:
1260 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1261 +------++------++------++------++------++------++------++------+
1262 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1264 bit number 48 32 16 4 0
1266 In both cases, the most significant end is on the left
1267 (i.e. normal numeric writing order), which means that you
1268 don't go crazy thinking about `left' and `right' shifts.
1270 We don't have to worry about masking yet:
1271 - If they contain garbage off the least significant end, then we
1272 must be looking at the low end of the field, and the right
1273 shift will wipe them out.
1274 - If they contain garbage off the most significant end, then we
1275 must be looking at the most significant end of the word, and
1276 the sign/zero extension will wipe them out.
1277 - If we're in the interior of the word, then there is no garbage
1278 on either end, because the ref operators zero-extend. */
1279 if (gdbarch_byte_order (ax
->gdbarch
) == BFD_ENDIAN_BIG
)
1280 gen_left_shift (ax
, end
- (offset
+ op_size
));
1282 gen_left_shift (ax
, offset
- start
);
1285 /* Bring the copy of the address up to the top. */
1286 ax_simple (ax
, aop_swap
);
1293 /* Generate enough bitwise `or' operations to combine all the
1294 fragments we left on the stack. */
1295 while (fragment_count
-- > 1)
1296 ax_simple (ax
, aop_bit_or
);
1298 /* Sign- or zero-extend the value as appropriate. */
1299 ((type
->is_unsigned () ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1301 /* This is *not* an lvalue. Ugh. */
1302 value
->kind
= axs_rvalue
;
1306 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1307 is an accumulated offset (in bytes), will be nonzero for objects
1308 embedded in other objects, like C++ base classes. Behavior should
1309 generally follow value_primitive_field. */
1312 gen_primitive_field (struct agent_expr
*ax
, struct axs_value
*value
,
1313 int offset
, int fieldno
, struct type
*type
)
1315 /* Is this a bitfield? */
1316 if (type
->field (fieldno
).is_packed ())
1317 gen_bitfield_ref (ax
, value
, type
->field (fieldno
).type (),
1318 (offset
* TARGET_CHAR_BIT
1319 + type
->field (fieldno
).loc_bitpos ()),
1320 (offset
* TARGET_CHAR_BIT
1321 + type
->field (fieldno
).loc_bitpos ()
1322 + type
->field (fieldno
).bitsize ()));
1325 gen_offset (ax
, offset
1326 + type
->field (fieldno
).loc_bitpos () / TARGET_CHAR_BIT
);
1327 value
->kind
= axs_lvalue_memory
;
1328 value
->type
= type
->field (fieldno
).type ();
1332 /* Search for the given field in either the given type or one of its
1333 base classes. Return 1 if found, 0 if not. */
1336 gen_struct_ref_recursive (struct agent_expr
*ax
, struct axs_value
*value
,
1337 const char *field
, int offset
, struct type
*type
)
1340 int nbases
= TYPE_N_BASECLASSES (type
);
1342 type
= check_typedef (type
);
1344 for (i
= type
->num_fields () - 1; i
>= nbases
; i
--)
1346 const char *this_name
= type
->field (i
).name ();
1350 if (strcmp (field
, this_name
) == 0)
1352 /* Note that bytecodes for the struct's base (aka
1353 "this") will have been generated already, which will
1354 be unnecessary but not harmful if the static field is
1355 being handled as a global. */
1356 if (type
->field (i
).is_static ())
1358 gen_static_field (ax
, value
, type
, i
);
1359 if (value
->optimized_out
)
1360 error (_("static field `%s' has been "
1361 "optimized out, cannot use"),
1366 gen_primitive_field (ax
, value
, offset
, i
, type
);
1369 #if 0 /* is this right? */
1370 if (this_name
[0] == '\0')
1371 internal_error (_("find_field: anonymous unions not supported"));
1376 /* Now scan through base classes recursively. */
1377 for (i
= 0; i
< nbases
; i
++)
1379 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1381 rslt
= gen_struct_ref_recursive (ax
, value
, field
,
1382 offset
+ TYPE_BASECLASS_BITPOS (type
, i
)
1389 /* Not found anywhere, flag so caller can complain. */
1393 /* Generate code to reference the member named FIELD of a structure or
1394 union. The top of the stack, as described by VALUE, should have
1395 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1396 the operator being compiled, and OPERAND_NAME is the kind of thing
1397 it operates on; we use them in error messages. */
1399 gen_struct_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1400 const char *field
, const char *operator_name
,
1401 const char *operand_name
)
1406 /* Follow pointers until we reach a non-pointer. These aren't the C
1407 semantics, but they're what the normal GDB evaluator does, so we
1408 should at least be consistent. */
1409 while (value
->type
->is_pointer_or_reference ())
1411 require_rvalue (ax
, value
);
1414 type
= check_typedef (value
->type
);
1416 /* This must yield a structure or a union. */
1417 if (type
->code () != TYPE_CODE_STRUCT
1418 && type
->code () != TYPE_CODE_UNION
)
1419 error (_("The left operand of `%s' is not a %s."),
1420 operator_name
, operand_name
);
1422 /* And it must be in memory; we don't deal with structure rvalues,
1423 or structures living in registers. */
1424 if (value
->kind
!= axs_lvalue_memory
)
1425 error (_("Structure does not live in memory."));
1427 /* Search through fields and base classes recursively. */
1428 found
= gen_struct_ref_recursive (ax
, value
, field
, 0, type
);
1431 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1432 field
, type
->name ());
1436 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1437 const struct type
*curtype
, const char *name
);
1439 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1440 const struct type
*curtype
, const char *name
);
1443 gen_static_field (struct agent_expr
*ax
, struct axs_value
*value
,
1444 struct type
*type
, int fieldno
)
1446 if (type
->field (fieldno
).loc_kind () == FIELD_LOC_KIND_PHYSADDR
)
1448 ax_const_l (ax
, type
->field (fieldno
).loc_physaddr ());
1449 value
->kind
= axs_lvalue_memory
;
1450 value
->type
= type
->field (fieldno
).type ();
1451 value
->optimized_out
= 0;
1455 const char *phys_name
= type
->field (fieldno
).loc_physname ();
1456 struct symbol
*sym
= lookup_symbol (phys_name
, 0,
1457 SEARCH_VAR_DOMAIN
, 0).symbol
;
1461 gen_var_ref (ax
, value
, sym
);
1463 /* Don't error if the value was optimized out, we may be
1464 scanning all static fields and just want to pass over this
1465 and continue with the rest. */
1469 /* Silently assume this was optimized out; class printing
1470 will let the user know why the data is missing. */
1471 value
->optimized_out
= 1;
1477 gen_struct_elt_for_reference (struct agent_expr
*ax
, struct axs_value
*value
,
1478 struct type
*type
, const char *fieldname
)
1480 struct type
*t
= type
;
1483 if (t
->code () != TYPE_CODE_STRUCT
1484 && t
->code () != TYPE_CODE_UNION
)
1485 internal_error (_("non-aggregate type to gen_struct_elt_for_reference"));
1487 for (i
= t
->num_fields () - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1489 const char *t_field_name
= t
->field (i
).name ();
1491 if (t_field_name
&& strcmp (t_field_name
, fieldname
) == 0)
1493 if (t
->field (i
).is_static ())
1495 gen_static_field (ax
, value
, t
, i
);
1496 if (value
->optimized_out
)
1497 error (_("static field `%s' has been "
1498 "optimized out, cannot use"),
1502 if (t
->field (i
).is_packed ())
1503 error (_("pointers to bitfield members not allowed"));
1505 /* FIXME we need a way to do "want_address" equivalent */
1507 error (_("Cannot reference non-static field \"%s\""), fieldname
);
1511 /* FIXME add other scoped-reference cases here */
1513 /* Do a last-ditch lookup. */
1514 return gen_maybe_namespace_elt (ax
, value
, type
, fieldname
);
1517 /* C++: Return the member NAME of the namespace given by the type
1521 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1522 const struct type
*curtype
, const char *name
)
1524 int found
= gen_maybe_namespace_elt (ax
, value
, curtype
, name
);
1527 error (_("No symbol \"%s\" in namespace \"%s\"."),
1528 name
, curtype
->name ());
1533 /* A helper function used by value_namespace_elt and
1534 value_struct_elt_for_reference. It looks up NAME inside the
1535 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1536 is a class and NAME refers to a type in CURTYPE itself (as opposed
1537 to, say, some base class of CURTYPE). */
1540 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1541 const struct type
*curtype
, const char *name
)
1543 const char *namespace_name
= curtype
->name ();
1544 struct block_symbol sym
;
1546 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
1547 block_for_pc (ax
->scope
),
1550 if (sym
.symbol
== NULL
)
1553 gen_var_ref (ax
, value
, sym
.symbol
);
1555 if (value
->optimized_out
)
1556 error (_("`%s' has been optimized out, cannot use"),
1557 sym
.symbol
->print_name ());
1564 gen_aggregate_elt_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1565 struct type
*type
, const char *field
)
1567 switch (type
->code ())
1569 case TYPE_CODE_STRUCT
:
1570 case TYPE_CODE_UNION
:
1571 return gen_struct_elt_for_reference (ax
, value
, type
, field
);
1573 case TYPE_CODE_NAMESPACE
:
1574 return gen_namespace_elt (ax
, value
, type
, field
);
1577 internal_error (_("non-aggregate type in gen_aggregate_elt_ref"));
1589 operation::generate_ax (struct expression
*exp
,
1590 struct agent_expr
*ax
,
1591 struct axs_value
*value
,
1592 struct type
*cast_type
)
1596 struct value
*v
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
1597 ax_const_l (ax
, value_as_long (v
));
1598 value
->kind
= axs_rvalue
;
1599 value
->type
= check_typedef (v
->type ());
1603 do_generate_ax (exp
, ax
, value
, cast_type
);
1604 if (cast_type
!= nullptr)
1605 gen_cast (ax
, value
, cast_type
);
1610 scope_operation::do_generate_ax (struct expression
*exp
,
1611 struct agent_expr
*ax
,
1612 struct axs_value
*value
,
1613 struct type
*cast_type
)
1615 struct type
*type
= std::get
<0> (m_storage
);
1616 const std::string
&name
= std::get
<1> (m_storage
);
1617 int found
= gen_aggregate_elt_ref (ax
, value
, type
, name
.c_str ());
1619 error (_("There is no field named %s"), name
.c_str ());
1623 long_const_operation::do_generate_ax (struct expression
*exp
,
1624 struct agent_expr
*ax
,
1625 struct axs_value
*value
,
1626 struct type
*cast_type
)
1628 LONGEST val
= as_longest ();
1629 gen_int_literal (ax
, value
, val
, std::get
<0> (m_storage
));
1633 var_msym_value_operation::do_generate_ax (struct expression
*exp
,
1634 struct agent_expr
*ax
,
1635 struct axs_value
*value
,
1636 struct type
*cast_type
)
1638 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
1639 gen_msym_var_ref (ax
, value
, b
.minsym
, b
.objfile
);
1641 if (value
->type
->code () == TYPE_CODE_ERROR
)
1643 if (cast_type
== nullptr)
1644 error_unknown_type (b
.minsym
->linkage_name ());
1645 value
->type
= cast_type
;
1650 register_operation::do_generate_ax (struct expression
*exp
,
1651 struct agent_expr
*ax
,
1652 struct axs_value
*value
,
1653 struct type
*cast_type
)
1655 const char *name
= std::get
<0> (m_storage
).c_str ();
1656 int len
= std::get
<0> (m_storage
).size ();
1659 reg
= user_reg_map_name_to_regnum (ax
->gdbarch
, name
, len
);
1661 internal_error (_("Register $%s not available"), name
);
1662 /* No support for tracing user registers yet. */
1663 if (reg
>= gdbarch_num_cooked_regs (ax
->gdbarch
))
1664 error (_("'%s' is a user-register; "
1665 "GDB cannot yet trace user-register contents."),
1667 value
->kind
= axs_lvalue_register
;
1669 value
->type
= register_type (ax
->gdbarch
, reg
);
1673 internalvar_operation::do_generate_ax (struct expression
*exp
,
1674 struct agent_expr
*ax
,
1675 struct axs_value
*value
,
1676 struct type
*cast_type
)
1678 struct internalvar
*var
= std::get
<0> (m_storage
);
1679 const char *name
= internalvar_name (var
);
1680 struct trace_state_variable
*tsv
;
1682 tsv
= find_trace_state_variable (name
);
1685 ax_tsv (ax
, aop_getv
, tsv
->number
);
1687 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1688 /* Trace state variables are always 64-bit integers. */
1689 value
->kind
= axs_rvalue
;
1690 value
->type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
1692 else if (! compile_internalvar_to_ax (var
, ax
, value
))
1693 error (_("$%s is not a trace state variable; GDB agent "
1694 "expressions cannot use convenience variables."), name
);
1698 ternop_cond_operation::do_generate_ax (struct expression
*exp
,
1699 struct agent_expr
*ax
,
1700 struct axs_value
*value
,
1701 struct type
*cast_type
)
1703 struct axs_value value1
, value2
, value3
;
1706 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
1707 gen_usual_unary (ax
, &value1
);
1708 /* For (A ? B : C), it's easiest to generate subexpression
1709 bytecodes in order, but if_goto jumps on true, so we invert
1710 the sense of A. Then we can do B by dropping through, and
1712 gen_logical_not (ax
, &value1
, builtin_type (ax
->gdbarch
)->builtin_int
);
1713 if1
= ax_goto (ax
, aop_if_goto
);
1714 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, &value2
);
1715 gen_usual_unary (ax
, &value2
);
1716 end
= ax_goto (ax
, aop_goto
);
1717 ax_label (ax
, if1
, ax
->buf
.size ());
1718 std::get
<2> (m_storage
)->generate_ax (exp
, ax
, &value3
);
1719 gen_usual_unary (ax
, &value3
);
1720 ax_label (ax
, end
, ax
->buf
.size ());
1721 /* This is arbitrary - what if B and C are incompatible types? */
1722 value
->type
= value2
.type
;
1723 value
->kind
= value2
.kind
;
1726 /* Generate code for GDB's magical `repeat' operator.
1727 LVALUE @ INT creates an array INT elements long, and whose elements
1728 have the same type as LVALUE, located in memory so that LVALUE is
1729 its first element. For example, argv[0]@argc gives you the array
1730 of command-line arguments.
1732 Unfortunately, because we have to know the types before we actually
1733 have a value for the expression, we can't implement this perfectly
1734 without changing the type system, having values that occupy two
1735 stack slots, doing weird things with sizeof, etc. So we require
1736 the right operand to be a constant expression. */
1738 repeat_operation::do_generate_ax (struct expression
*exp
,
1739 struct agent_expr
*ax
,
1740 struct axs_value
*value
,
1741 struct type
*cast_type
)
1743 struct axs_value value1
;
1745 /* We don't want to turn this into an rvalue, so no conversions
1747 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
1748 if (value1
.kind
!= axs_lvalue_memory
)
1749 error (_("Left operand of `@' must be an object in memory."));
1751 /* Evaluate the length; it had better be a constant. */
1752 if (!std::get
<1> (m_storage
)->constant_p ())
1753 error (_("Right operand of `@' must be a "
1754 "constant, in agent expressions."));
1757 = std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
1758 EVAL_AVOID_SIDE_EFFECTS
);
1759 if (v
->type ()->code () != TYPE_CODE_INT
)
1760 error (_("Right operand of `@' must be an integer."));
1761 int length
= value_as_long (v
);
1763 error (_("Right operand of `@' must be positive."));
1765 /* The top of the stack is already the address of the object, so
1766 all we need to do is frob the type of the lvalue. */
1767 /* FIXME-type-allocation: need a way to free this type when we are
1770 = lookup_array_range_type (value1
.type
, 0, length
- 1);
1772 value
->kind
= axs_lvalue_memory
;
1773 value
->type
= array
;
1777 comma_operation::do_generate_ax (struct expression
*exp
,
1778 struct agent_expr
*ax
,
1779 struct axs_value
*value
,
1780 struct type
*cast_type
)
1782 /* Note that we need to be a little subtle about generating code
1783 for comma. In C, we can do some optimizations here because
1784 we know the left operand is only being evaluated for effect.
1785 However, if the tracing kludge is in effect, then we always
1786 need to evaluate the left hand side fully, so that all the
1787 variables it mentions get traced. */
1788 struct axs_value value1
;
1789 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
1790 /* Don't just dispose of the left operand. We might be tracing,
1791 in which case we want to emit code to trace it if it's an
1793 gen_traced_pop (ax
, &value1
);
1794 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
1795 /* It's the consumer's responsibility to trace the right operand. */
1799 unop_sizeof_operation::do_generate_ax (struct expression
*exp
,
1800 struct agent_expr
*ax
,
1801 struct axs_value
*value
,
1802 struct type
*cast_type
)
1804 /* We don't care about the value of the operand expression; we only
1805 care about its type. However, in the current arrangement, the
1806 only way to find an expression's type is to generate code for it.
1807 So we generate code for the operand, and then throw it away,
1808 replacing it with code that simply pushes its size. */
1809 int start
= ax
->buf
.size ();
1811 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
1813 /* Throw away the code we just generated. */
1814 ax
->buf
.resize (start
);
1816 ax_const_l (ax
, value
->type
->length ());
1817 value
->kind
= axs_rvalue
;
1818 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
1822 unop_cast_operation::do_generate_ax (struct expression
*exp
,
1823 struct agent_expr
*ax
,
1824 struct axs_value
*value
,
1825 struct type
*cast_type
)
1827 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
,
1828 std::get
<1> (m_storage
));
1832 unop_extract_operation::do_generate_ax (struct expression
*exp
,
1833 struct agent_expr
*ax
,
1834 struct axs_value
*value
,
1835 struct type
*cast_type
)
1837 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
1839 struct type
*to_type
= get_type ();
1841 if (!is_scalar_type (to_type
))
1842 error (_("can't generate agent expression to extract non-scalar type"));
1844 if (to_type
->is_unsigned ())
1845 gen_extend (ax
, to_type
);
1847 gen_sign_extend (ax
, to_type
);
1851 unop_memval_operation::do_generate_ax (struct expression
*exp
,
1852 struct agent_expr
*ax
,
1853 struct axs_value
*value
,
1854 struct type
*cast_type
)
1856 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
1857 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
1858 already have the right value on the stack. For
1859 axs_lvalue_register, we must convert. */
1860 if (value
->kind
== axs_lvalue_register
)
1861 require_rvalue (ax
, value
);
1863 value
->type
= std::get
<1> (m_storage
);
1864 value
->kind
= axs_lvalue_memory
;
1868 unop_memval_type_operation::do_generate_ax (struct expression
*exp
,
1869 struct agent_expr
*ax
,
1870 struct axs_value
*value
,
1871 struct type
*cast_type
)
1874 = std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1875 EVAL_AVOID_SIDE_EFFECTS
);
1876 struct type
*type
= val
->type ();
1878 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
1880 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
1881 already have the right value on the stack. For
1882 axs_lvalue_register, we must convert. */
1883 if (value
->kind
== axs_lvalue_register
)
1884 require_rvalue (ax
, value
);
1887 value
->kind
= axs_lvalue_memory
;
1891 op_this_operation::do_generate_ax (struct expression
*exp
,
1892 struct agent_expr
*ax
,
1893 struct axs_value
*value
,
1894 struct type
*cast_type
)
1896 struct symbol
*sym
, *func
;
1897 const struct block
*b
;
1898 const struct language_defn
*lang
;
1900 b
= block_for_pc (ax
->scope
);
1901 func
= b
->linkage_function ();
1902 lang
= language_def (func
->language ());
1904 sym
= lookup_language_this (lang
, b
).symbol
;
1906 error (_("no `%s' found"), lang
->name_of_this ());
1908 gen_var_ref (ax
, value
, sym
);
1910 if (value
->optimized_out
)
1911 error (_("`%s' has been optimized out, cannot use"),
1912 sym
->print_name ());
1916 assign_operation::do_generate_ax (struct expression
*exp
,
1917 struct agent_expr
*ax
,
1918 struct axs_value
*value
,
1919 struct type
*cast_type
)
1921 operation
*subop
= std::get
<0> (m_storage
).get ();
1922 if (subop
->opcode () != OP_INTERNALVAR
)
1923 error (_("May only assign to trace state variables"));
1925 internalvar_operation
*ivarop
1926 = gdb::checked_static_cast
<internalvar_operation
*> (subop
);
1928 const char *name
= internalvar_name (ivarop
->get_internalvar ());
1929 struct trace_state_variable
*tsv
;
1931 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
1932 tsv
= find_trace_state_variable (name
);
1935 ax_tsv (ax
, aop_setv
, tsv
->number
);
1937 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1940 error (_("$%s is not a trace state variable, "
1941 "may not assign to it"), name
);
1945 assign_modify_operation::do_generate_ax (struct expression
*exp
,
1946 struct agent_expr
*ax
,
1947 struct axs_value
*value
,
1948 struct type
*cast_type
)
1950 operation
*subop
= std::get
<1> (m_storage
).get ();
1951 if (subop
->opcode () != OP_INTERNALVAR
)
1952 error (_("May only assign to trace state variables"));
1954 internalvar_operation
*ivarop
1955 = gdb::checked_static_cast
<internalvar_operation
*> (subop
);
1957 const char *name
= internalvar_name (ivarop
->get_internalvar ());
1958 struct trace_state_variable
*tsv
;
1960 tsv
= find_trace_state_variable (name
);
1963 /* The tsv will be the left half of the binary operation. */
1964 ax_tsv (ax
, aop_getv
, tsv
->number
);
1966 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1967 /* Trace state variables are always 64-bit integers. */
1968 struct axs_value value1
, value2
;
1969 value1
.kind
= axs_rvalue
;
1970 value1
.type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
1971 /* Now do right half of expression. */
1972 std::get
<2> (m_storage
)->generate_ax (exp
, ax
, &value2
);
1973 gen_expr_binop_rest (exp
, std::get
<0> (m_storage
), ax
,
1974 value
, &value1
, &value2
);
1975 /* We have a result of the binary op, set the tsv. */
1976 ax_tsv (ax
, aop_setv
, tsv
->number
);
1978 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1981 error (_("$%s is not a trace state variable, "
1982 "may not assign to it"), name
);
1986 unop_cast_type_operation::do_generate_ax (struct expression
*exp
,
1987 struct agent_expr
*ax
,
1988 struct axs_value
*value
,
1989 struct type
*cast_type
)
1992 = std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1993 EVAL_AVOID_SIDE_EFFECTS
);
1994 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
, val
->type ());
1998 var_value_operation::do_generate_ax (struct expression
*exp
,
1999 struct agent_expr
*ax
,
2000 struct axs_value
*value
,
2001 struct type
*cast_type
)
2003 gen_var_ref (ax
, value
, std::get
<0> (m_storage
).symbol
);
2005 if (value
->optimized_out
)
2006 error (_("`%s' has been optimized out, cannot use"),
2007 std::get
<0> (m_storage
).symbol
->print_name ());
2009 if (value
->type
->code () == TYPE_CODE_ERROR
)
2011 if (cast_type
== nullptr)
2012 error_unknown_type (std::get
<0> (m_storage
).symbol
->print_name ());
2013 value
->type
= cast_type
;
2018 logical_and_operation::do_generate_ax (struct expression
*exp
,
2019 struct agent_expr
*ax
,
2020 struct axs_value
*value
,
2021 struct type
*cast_type
)
2023 struct axs_value value1
, value2
;
2024 int if1
, go1
, if2
, go2
, end
;
2026 /* Generate the obvious sequence of tests and jumps. */
2027 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
2028 gen_usual_unary (ax
, &value1
);
2029 if1
= ax_goto (ax
, aop_if_goto
);
2030 go1
= ax_goto (ax
, aop_goto
);
2031 ax_label (ax
, if1
, ax
->buf
.size ());
2032 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, &value2
);
2033 gen_usual_unary (ax
, &value2
);
2034 if2
= ax_goto (ax
, aop_if_goto
);
2035 go2
= ax_goto (ax
, aop_goto
);
2036 ax_label (ax
, if2
, ax
->buf
.size ());
2038 end
= ax_goto (ax
, aop_goto
);
2039 ax_label (ax
, go1
, ax
->buf
.size ());
2040 ax_label (ax
, go2
, ax
->buf
.size ());
2042 ax_label (ax
, end
, ax
->buf
.size ());
2043 value
->kind
= axs_rvalue
;
2044 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2048 logical_or_operation::do_generate_ax (struct expression
*exp
,
2049 struct agent_expr
*ax
,
2050 struct axs_value
*value
,
2051 struct type
*cast_type
)
2053 struct axs_value value1
, value2
;
2056 /* Generate the obvious sequence of tests and jumps. */
2057 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
2058 gen_usual_unary (ax
, &value1
);
2059 if1
= ax_goto (ax
, aop_if_goto
);
2060 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, &value2
);
2061 gen_usual_unary (ax
, &value2
);
2062 if2
= ax_goto (ax
, aop_if_goto
);
2064 end
= ax_goto (ax
, aop_goto
);
2065 ax_label (ax
, if1
, ax
->buf
.size ());
2066 ax_label (ax
, if2
, ax
->buf
.size ());
2068 ax_label (ax
, end
, ax
->buf
.size ());
2069 value
->kind
= axs_rvalue
;
2070 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2075 /* This handles the middle-to-right-side of code generation for binary
2076 expressions, which is shared between regular binary operations and
2077 assign-modify (+= and friends) expressions. */
2080 gen_expr_binop_rest (struct expression
*exp
,
2082 struct agent_expr
*ax
, struct axs_value
*value
,
2083 struct axs_value
*value1
, struct axs_value
*value2
)
2085 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2087 gen_usual_unary (ax
, value2
);
2088 gen_usual_arithmetic (ax
, value1
, value2
);
2092 if (strip_range_type (value1
->type
)->code () == TYPE_CODE_INT
2093 && value2
->type
->is_pointer_or_reference ())
2095 /* Swap the values and proceed normally. */
2096 ax_simple (ax
, aop_swap
);
2097 gen_ptradd (ax
, value
, value2
, value1
);
2099 else if (value1
->type
->is_pointer_or_reference ()
2100 && strip_range_type (value2
->type
)->code () == TYPE_CODE_INT
)
2101 gen_ptradd (ax
, value
, value1
, value2
);
2103 gen_binop (ax
, value
, value1
, value2
,
2104 aop_add
, aop_add
, 1, "addition");
2107 if (value1
->type
->is_pointer_or_reference ()
2108 && strip_range_type (value2
->type
)->code () == TYPE_CODE_INT
)
2109 gen_ptrsub (ax
,value
, value1
, value2
);
2110 else if (value1
->type
->is_pointer_or_reference ()
2111 && value2
->type
->is_pointer_or_reference ())
2112 /* FIXME --- result type should be ptrdiff_t */
2113 gen_ptrdiff (ax
, value
, value1
, value2
,
2114 builtin_type (ax
->gdbarch
)->builtin_long
);
2116 gen_binop (ax
, value
, value1
, value2
,
2117 aop_sub
, aop_sub
, 1, "subtraction");
2120 gen_binop (ax
, value
, value1
, value2
,
2121 aop_mul
, aop_mul
, 1, "multiplication");
2124 gen_binop (ax
, value
, value1
, value2
,
2125 aop_div_signed
, aop_div_unsigned
, 1, "division");
2128 gen_binop (ax
, value
, value1
, value2
,
2129 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
2132 gen_binop (ax
, value
, value1
, value2
,
2133 aop_lsh
, aop_lsh
, 1, "left shift");
2136 gen_binop (ax
, value
, value1
, value2
,
2137 aop_rsh_signed
, aop_rsh_unsigned
, 1, "right shift");
2139 case BINOP_SUBSCRIPT
:
2143 if (binop_types_user_defined_p (op
, value1
->type
, value2
->type
))
2145 error (_("cannot subscript requested type: "
2146 "cannot call user defined functions"));
2150 /* If the user attempts to subscript something that is not
2151 an array or pointer type (like a plain int variable for
2152 example), then report this as an error. */
2153 type
= check_typedef (value1
->type
);
2154 if (type
->code () != TYPE_CODE_ARRAY
2155 && type
->code () != TYPE_CODE_PTR
)
2158 error (_("cannot subscript something of type `%s'"),
2161 error (_("cannot subscript requested type"));
2165 if (!is_integral_type (value2
->type
))
2166 error (_("Argument to arithmetic operation "
2167 "not a number or boolean."));
2169 gen_ptradd (ax
, value
, value1
, value2
);
2173 case BINOP_BITWISE_AND
:
2174 gen_binop (ax
, value
, value1
, value2
,
2175 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
2178 case BINOP_BITWISE_IOR
:
2179 gen_binop (ax
, value
, value1
, value2
,
2180 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
2183 case BINOP_BITWISE_XOR
:
2184 gen_binop (ax
, value
, value1
, value2
,
2185 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
2189 gen_equal (ax
, value
, value1
, value2
, int_type
);
2192 case BINOP_NOTEQUAL
:
2193 gen_equal (ax
, value
, value1
, value2
, int_type
);
2194 gen_logical_not (ax
, value
, int_type
);
2198 gen_less (ax
, value
, value1
, value2
, int_type
);
2202 ax_simple (ax
, aop_swap
);
2203 gen_less (ax
, value
, value1
, value2
, int_type
);
2207 ax_simple (ax
, aop_swap
);
2208 gen_less (ax
, value
, value1
, value2
, int_type
);
2209 gen_logical_not (ax
, value
, int_type
);
2213 gen_less (ax
, value
, value1
, value2
, int_type
);
2214 gen_logical_not (ax
, value
, int_type
);
2218 /* We should only list operators in the outer case statement
2219 that we actually handle in the inner case statement. */
2220 internal_error (_("gen_expr: op case sets don't match"));
2224 /* A helper function that emits a binop based on two operations. */
2227 gen_expr_binop (struct expression
*exp
,
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
2244 gen_expr_structop (struct expression
*exp
,
2246 expr::operation
*lhs
,
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");
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 (_("gen_expr: unhandled struct case"));
2262 /* A helper function that emits a unary operation. */
2265 gen_expr_unop (struct expression
*exp
,
2267 expr::operation
*lhs
,
2268 struct agent_expr
*ax
, struct axs_value
*value
)
2270 struct axs_value value1
, value2
;
2275 gen_int_literal (ax
, &value1
, 0,
2276 builtin_type (ax
->gdbarch
)->builtin_int
);
2277 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
2278 lhs
->generate_ax (exp
, ax
, &value2
);
2279 gen_usual_unary (ax
, &value2
);
2280 gen_usual_arithmetic (ax
, &value1
, &value2
);
2281 gen_binop (ax
, value
, &value1
, &value2
, aop_sub
, aop_sub
, 1, "negation");
2285 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2286 lhs
->generate_ax (exp
, ax
, value
);
2287 gen_usual_unary (ax
, value
);
2290 case UNOP_LOGICAL_NOT
:
2291 lhs
->generate_ax (exp
, ax
, value
);
2292 gen_usual_unary (ax
, value
);
2293 gen_logical_not (ax
, value
, builtin_type (ax
->gdbarch
)->builtin_int
);
2296 case UNOP_COMPLEMENT
:
2297 lhs
->generate_ax (exp
, ax
, value
);
2298 gen_usual_unary (ax
, value
);
2299 gen_integral_promotions (ax
, value
);
2300 gen_complement (ax
, value
);
2304 lhs
->generate_ax (exp
, ax
, value
);
2305 gen_usual_unary (ax
, value
);
2306 if (!value
->type
->is_pointer_or_reference ())
2307 error (_("Argument of unary `*' is not a pointer."));
2312 lhs
->generate_ax (exp
, ax
, value
);
2313 gen_address_of (value
);
2317 gdb_assert_not_reached ("invalid case in gen_expr_unop");
2323 /* Given a single variable and a scope, generate bytecodes to trace
2324 its value. This is for use in situations where we have only a
2325 variable's name, and no parsed expression; for instance, when the
2326 name comes from a list of local variables of a function. */
2329 gen_trace_for_var (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2330 struct symbol
*var
, int trace_string
)
2332 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2333 struct axs_value value
;
2336 ax
->trace_string
= trace_string
;
2337 gen_var_ref (ax
.get (), &value
, var
);
2339 /* If there is no actual variable to trace, flag it by returning
2340 an empty agent expression. */
2341 if (value
.optimized_out
)
2342 return agent_expr_up ();
2344 /* Make sure we record the final object, and get rid of it. */
2345 gen_traced_pop (ax
.get (), &value
);
2347 /* Oh, and terminate. */
2348 ax_simple (ax
.get (), aop_end
);
2353 /* Generating bytecode from GDB expressions: driver */
2355 /* Given a GDB expression EXPR, return bytecode to trace its value.
2356 The result will use the `trace' and `trace_quick' bytecodes to
2357 record the value of all memory touched by the expression. The
2358 caller can then use the ax_reqs function to discover which
2359 registers it relies upon. */
2362 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
,
2365 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2366 struct axs_value value
;
2369 ax
->trace_string
= trace_string
;
2370 value
.optimized_out
= 0;
2371 expr
->op
->generate_ax (expr
, ax
.get (), &value
);
2373 /* Make sure we record the final object, and get rid of it. */
2374 gen_traced_pop (ax
.get (), &value
);
2376 /* Oh, and terminate. */
2377 ax_simple (ax
.get (), aop_end
);
2382 /* Given a GDB expression EXPR, return a bytecode sequence that will
2383 evaluate and return a result. The bytecodes will do a direct
2384 evaluation, using the current data on the target, rather than
2385 recording blocks of memory and registers for later use, as
2386 gen_trace_for_expr does. The generated bytecode sequence leaves
2387 the result of expression evaluation on the top of the stack. */
2390 gen_eval_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2392 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2393 struct axs_value value
;
2395 ax
->tracing
= false;
2396 value
.optimized_out
= 0;
2397 expr
->op
->generate_ax (expr
, ax
.get (), &value
);
2399 require_rvalue (ax
.get (), &value
);
2401 /* Oh, and terminate. */
2402 ax_simple (ax
.get (), aop_end
);
2408 gen_trace_for_return_address (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2411 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2412 struct axs_value value
;
2415 ax
->trace_string
= trace_string
;
2417 gdbarch_gen_return_address (gdbarch
, ax
.get (), &value
, scope
);
2419 /* Make sure we record the final object, and get rid of it. */
2420 gen_traced_pop (ax
.get (), &value
);
2422 /* Oh, and terminate. */
2423 ax_simple (ax
.get (), aop_end
);
2428 /* Given a collection of printf-style arguments, generate code to
2429 evaluate the arguments and pass everything to a special
2433 gen_printf (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2434 CORE_ADDR function
, LONGEST channel
,
2435 const char *format
, int fmtlen
,
2436 int nargs
, struct expression
**exprs
)
2438 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2439 struct axs_value value
;
2442 /* We're computing values, not doing side effects. */
2443 ax
->tracing
= false;
2445 /* Evaluate and push the args on the stack in reverse order,
2446 for simplicity of collecting them on the target side. */
2447 for (tem
= nargs
- 1; tem
>= 0; --tem
)
2449 value
.optimized_out
= 0;
2450 exprs
[tem
]->op
->generate_ax (exprs
[tem
], ax
.get (), &value
);
2451 require_rvalue (ax
.get (), &value
);
2454 /* Push function and channel. */
2455 ax_const_l (ax
.get (), channel
);
2456 ax_const_l (ax
.get (), function
);
2458 /* Issue the printf bytecode proper. */
2459 ax_simple (ax
.get (), aop_printf
);
2460 ax_raw_byte (ax
.get (), nargs
);
2461 ax_string (ax
.get (), format
, fmtlen
);
2463 /* And terminate. */
2464 ax_simple (ax
.get (), aop_end
);
2470 agent_eval_command_one (const char *exp
, int eval
, CORE_ADDR pc
)
2473 int trace_string
= 0;
2478 exp
= decode_agent_options (exp
, &trace_string
);
2481 agent_expr_up agent
;
2484 if (!eval
&& strcmp (arg
, "$_ret") == 0)
2486 agent
= gen_trace_for_return_address (pc
, get_current_arch (),
2491 expression_up expr
= parse_exp_1 (&arg
, pc
, block_for_pc (pc
), 0);
2495 gdb_assert (trace_string
== 0);
2496 agent
= gen_eval_for_expr (pc
, expr
.get ());
2499 agent
= gen_trace_for_expr (pc
, expr
.get (), trace_string
);
2502 ax_reqs (agent
.get ());
2503 ax_print (gdb_stdout
, agent
.get ());
2505 /* It would be nice to call ax_reqs here to gather some general info
2506 about the expression, and then print out the result. */
2512 maint_agent_command_1 (const char *exp
, int eval
)
2514 /* We don't deal with overlay debugging at the moment. We need to
2515 think more carefully about this. If you copy this code into
2516 another command, change the error message; the user shouldn't
2517 have to know anything about agent expressions. */
2518 if (overlay_debugging
)
2519 error (_("GDB can't do agent expression translation with overlays."));
2522 error_no_arg (_("expression to translate"));
2524 if (check_for_argument (&exp
, "-at", sizeof ("-at") - 1))
2526 struct linespec_result canonical
;
2528 location_spec_up locspec
2529 = new_linespec_location_spec (&exp
, symbol_name_match_type::WILD
);
2530 decode_line_full (locspec
.get (), DECODE_LINE_FUNFIRSTLINE
, NULL
,
2531 NULL
, 0, &canonical
,
2533 exp
= skip_spaces (exp
);
2537 exp
= skip_spaces (exp
);
2539 for (const auto &lsal
: canonical
.lsals
)
2540 for (const auto &sal
: lsal
.sals
)
2541 agent_eval_command_one (exp
, eval
, sal
.pc
);
2544 agent_eval_command_one (exp
, eval
, get_frame_pc (get_current_frame ()));
2550 maint_agent_command (const char *exp
, int from_tty
)
2552 maint_agent_command_1 (exp
, 0);
2555 /* Parse the given expression, compile it into an agent expression
2556 that does direct evaluation, and display the resulting
2560 maint_agent_eval_command (const char *exp
, int from_tty
)
2562 maint_agent_command_1 (exp
, 1);
2565 /* Parse the given expression, compile it into an agent expression
2566 that does a printf, and display the resulting expression. */
2569 maint_agent_printf_command (const char *cmdrest
, int from_tty
)
2571 frame_info_ptr fi
= get_current_frame (); /* need current scope */
2572 const char *format_start
, *format_end
;
2574 /* We don't deal with overlay debugging at the moment. We need to
2575 think more carefully about this. If you copy this code into
2576 another command, change the error message; the user shouldn't
2577 have to know anything about agent expressions. */
2578 if (overlay_debugging
)
2579 error (_("GDB can't do agent expression translation with overlays."));
2582 error_no_arg (_("expression to translate"));
2584 cmdrest
= skip_spaces (cmdrest
);
2586 if (*cmdrest
++ != '"')
2587 error (_("Must start with a format string."));
2589 format_start
= cmdrest
;
2591 format_pieces
fpieces (&cmdrest
);
2593 format_end
= cmdrest
;
2595 if (*cmdrest
++ != '"')
2596 error (_("Bad format string, non-terminated '\"'."));
2598 cmdrest
= skip_spaces (cmdrest
);
2600 if (*cmdrest
!= ',' && *cmdrest
!= 0)
2601 error (_("Invalid argument syntax"));
2603 if (*cmdrest
== ',')
2605 cmdrest
= skip_spaces (cmdrest
);
2607 std::vector
<struct expression
*> argvec
;
2608 while (*cmdrest
!= '\0')
2613 expression_up expr
= parse_exp_1 (&cmd1
, 0, (struct block
*) 0,
2614 PARSER_COMMA_TERMINATES
);
2615 argvec
.push_back (expr
.release ());
2617 if (*cmdrest
== ',')
2619 /* else complain? */
2623 agent_expr_up agent
= gen_printf (get_frame_pc (fi
), get_current_arch (),
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. */
2636 /* Initialization code. */
2638 void _initialize_ax_gdb ();
2640 _initialize_ax_gdb ()
2642 add_cmd ("agent", class_maintenance
, maint_agent_command
,
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."),
2650 add_cmd ("agent-eval", class_maintenance
, maint_agent_eval_command
,
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."),
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."),