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/>. */
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 (field_is_static (&type
->field (i
)))
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
));
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. */
361 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
365 /* Record the area of memory we're about to fetch. */
366 ax_trace_quick (ax
, type
->length ());
369 if (type
->code () == TYPE_CODE_RANGE
)
370 type
= type
->target_type ();
372 switch (type
->code ())
376 case TYPE_CODE_RVALUE_REF
:
381 /* It's a scalar value, so we know how to dereference it. How
382 many bytes long is it? */
383 switch (type
->length ())
385 case 8 / TARGET_CHAR_BIT
:
386 ax_simple (ax
, aop_ref8
);
388 case 16 / TARGET_CHAR_BIT
:
389 ax_simple (ax
, aop_ref16
);
391 case 32 / TARGET_CHAR_BIT
:
392 ax_simple (ax
, aop_ref32
);
394 case 64 / TARGET_CHAR_BIT
:
395 ax_simple (ax
, aop_ref64
);
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. */
403 internal_error (_("gen_fetch: strange size"));
406 gen_sign_extend (ax
, type
);
410 /* Our caller requested us to dereference a pointer from an unsupported
411 type. Error out and give callers a chance to handle the failure
413 error (_("gen_fetch: Unsupported type code `%s'."),
419 /* Generate code to left shift the top of the stack by DISTANCE bits, or
420 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
421 unsigned (logical) right shifts. */
423 gen_left_shift (struct agent_expr
*ax
, int distance
)
427 ax_const_l (ax
, distance
);
428 ax_simple (ax
, aop_lsh
);
430 else if (distance
< 0)
432 ax_const_l (ax
, -distance
);
433 ax_simple (ax
, aop_rsh_unsigned
);
439 /* Generating bytecode from GDB expressions: symbol references */
441 /* Generate code to push the base address of the argument portion of
442 the top stack frame. */
444 gen_frame_args_address (struct agent_expr
*ax
)
447 LONGEST frame_offset
;
449 gdbarch_virtual_frame_pointer (ax
->gdbarch
,
450 ax
->scope
, &frame_reg
, &frame_offset
);
451 ax_reg (ax
, frame_reg
);
452 gen_offset (ax
, frame_offset
);
456 /* Generate code to push the base address of the locals portion of the
459 gen_frame_locals_address (struct agent_expr
*ax
)
462 LONGEST frame_offset
;
464 gdbarch_virtual_frame_pointer (ax
->gdbarch
,
465 ax
->scope
, &frame_reg
, &frame_offset
);
466 ax_reg (ax
, frame_reg
);
467 gen_offset (ax
, frame_offset
);
471 /* Generate code to add OFFSET to the top of the stack. Try to
472 generate short and readable code. We use this for getting to
473 variables on the stack, and structure members. If we were
474 programming in ML, it would be clearer why these are the same
477 gen_offset (struct agent_expr
*ax
, int offset
)
479 /* It would suffice to simply push the offset and add it, but this
480 makes it easier to read positive and negative offsets in the
484 ax_const_l (ax
, offset
);
485 ax_simple (ax
, aop_add
);
489 ax_const_l (ax
, -offset
);
490 ax_simple (ax
, aop_sub
);
495 /* In many cases, a symbol's value is the offset from some other
496 address (stack frame, base register, etc.) Generate code to add
497 VAR's value to the top of the stack. */
499 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
501 gen_offset (ax
, var
->value_longest ());
505 /* Generate code for a variable reference to AX. The variable is the
506 symbol VAR. Set VALUE to describe the result. */
509 gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
, struct symbol
*var
)
511 /* Dereference any typedefs. */
512 value
->type
= check_typedef (var
->type ());
513 value
->optimized_out
= 0;
515 if (SYMBOL_COMPUTED_OPS (var
) != NULL
)
517 SYMBOL_COMPUTED_OPS (var
)->tracepoint_var_ref (var
, ax
, value
);
521 /* I'm imitating the code in read_var_value. */
522 switch (var
->aclass ())
524 case LOC_CONST
: /* A constant, like an enum value. */
525 ax_const_l (ax
, (LONGEST
) var
->value_longest ());
526 value
->kind
= axs_rvalue
;
529 case LOC_LABEL
: /* A goto label, being used as a value. */
530 ax_const_l (ax
, (LONGEST
) var
->value_address ());
531 value
->kind
= axs_rvalue
;
534 case LOC_CONST_BYTES
:
535 internal_error (_("gen_var_ref: LOC_CONST_BYTES "
536 "symbols are not supported"));
538 /* Variable at a fixed location in memory. Easy. */
540 /* Push the address of the variable. */
541 ax_const_l (ax
, var
->value_address ());
542 value
->kind
= axs_lvalue_memory
;
545 case LOC_ARG
: /* var lives in argument area of frame */
546 gen_frame_args_address (ax
);
547 gen_sym_offset (ax
, var
);
548 value
->kind
= axs_lvalue_memory
;
551 case LOC_REF_ARG
: /* As above, but the frame slot really
552 holds the address of the variable. */
553 gen_frame_args_address (ax
);
554 gen_sym_offset (ax
, var
);
555 /* Don't assume any particular pointer size. */
556 gen_fetch (ax
, builtin_type (ax
->gdbarch
)->builtin_data_ptr
);
557 value
->kind
= axs_lvalue_memory
;
560 case LOC_LOCAL
: /* var lives in locals area of frame */
561 gen_frame_locals_address (ax
);
562 gen_sym_offset (ax
, var
);
563 value
->kind
= axs_lvalue_memory
;
567 error (_("Cannot compute value of typedef `%s'."),
572 ax_const_l (ax
, var
->value_block ()->entry_pc ());
573 value
->kind
= axs_rvalue
;
577 /* Don't generate any code at all; in the process of treating
578 this as an lvalue or rvalue, the caller will generate the
580 value
->kind
= axs_lvalue_register
;
582 = SYMBOL_REGISTER_OPS (var
)->register_number (var
, ax
->gdbarch
);
585 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
586 register, not on the stack. Simpler than LOC_REGISTER
587 because it's just like any other case where the thing
588 has a real address. */
589 case LOC_REGPARM_ADDR
:
591 SYMBOL_REGISTER_OPS (var
)->register_number (var
, ax
->gdbarch
));
592 value
->kind
= axs_lvalue_memory
;
597 struct bound_minimal_symbol msym
598 = lookup_minimal_symbol (var
->linkage_name (), NULL
, NULL
);
601 error (_("Couldn't resolve symbol `%s'."), var
->print_name ());
603 /* Push the address of the variable. */
604 ax_const_l (ax
, msym
.value_address ());
605 value
->kind
= axs_lvalue_memory
;
610 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
612 case LOC_OPTIMIZED_OUT
:
613 /* Flag this, but don't say anything; leave it up to callers to
615 value
->optimized_out
= 1;
619 error (_("Cannot find value of botched symbol `%s'."),
625 /* Generate code for a minimal symbol variable reference to AX. The
626 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
630 gen_msym_var_ref (agent_expr
*ax
, axs_value
*value
,
631 minimal_symbol
*msymbol
, objfile
*objf
)
634 type
*t
= find_minsym_type_and_address (msymbol
, objf
, &address
);
636 value
->optimized_out
= false;
637 ax_const_l (ax
, address
);
638 value
->kind
= axs_lvalue_memory
;
644 /* Generating bytecode from GDB expressions: literals */
647 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
651 value
->kind
= axs_rvalue
;
652 value
->type
= check_typedef (type
);
657 /* Generating bytecode from GDB expressions: unary conversions, casts */
659 /* Take what's on the top of the stack (as described by VALUE), and
660 try to make an rvalue out of it. Signal an error if we can't do
663 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
665 /* Only deal with scalars, structs and such may be too large
666 to fit in a stack entry. */
667 value
->type
= check_typedef (value
->type
);
668 if (value
->type
->code () == TYPE_CODE_ARRAY
669 || value
->type
->code () == TYPE_CODE_STRUCT
670 || value
->type
->code () == TYPE_CODE_UNION
671 || value
->type
->code () == TYPE_CODE_FUNC
)
672 error (_("Value not scalar: cannot be an rvalue."));
677 /* It's already an rvalue. */
680 case axs_lvalue_memory
:
681 /* The top of stack is the address of the object. Dereference. */
682 gen_fetch (ax
, value
->type
);
685 case axs_lvalue_register
:
686 /* There's nothing on the stack, but value->u.reg is the
687 register number containing the value.
689 When we add floating-point support, this is going to have to
690 change. What about SPARC register pairs, for example? */
691 ax_reg (ax
, value
->u
.reg
);
692 gen_extend (ax
, value
->type
);
696 value
->kind
= axs_rvalue
;
700 /* Assume the top of the stack is described by VALUE, and perform the
701 usual unary conversions. This is motivated by ANSI 6.2.2, but of
702 course GDB expressions are not ANSI; they're the mishmash union of
703 a bunch of languages. Rah.
705 NOTE! This function promises to produce an rvalue only when the
706 incoming value is of an appropriate type. In other words, the
707 consumer of the value this function produces may assume the value
708 is an rvalue only after checking its type.
710 The immediate issue is that if the user tries to use a structure or
711 union as an operand of, say, the `+' operator, we don't want to try
712 to convert that structure to an rvalue; require_rvalue will bomb on
713 structs and unions. Rather, we want to simply pass the struct
714 lvalue through unchanged, and let `+' raise an error. */
717 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
719 /* We don't have to generate any code for the usual integral
720 conversions, since values are always represented as full-width on
721 the stack. Should we tweak the type? */
723 /* Some types require special handling. */
724 switch (value
->type
->code ())
726 /* Functions get converted to a pointer to the function. */
728 value
->type
= lookup_pointer_type (value
->type
);
729 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
732 /* Arrays get converted to a pointer to their first element, and
733 are no longer an lvalue. */
734 case TYPE_CODE_ARRAY
:
736 struct type
*elements
= value
->type
->target_type ();
738 value
->type
= lookup_pointer_type (elements
);
739 value
->kind
= axs_rvalue
;
740 /* We don't need to generate any code; the address of the array
741 is also the address of its first element. */
745 /* Don't try to convert structures and unions to rvalues. Let the
746 consumer signal an error. */
747 case TYPE_CODE_STRUCT
:
748 case TYPE_CODE_UNION
:
752 /* If the value is an lvalue, dereference it. */
753 require_rvalue (ax
, value
);
757 /* Return non-zero iff the type TYPE1 is considered "wider" than the
758 type TYPE2, according to the rules described in gen_usual_arithmetic. */
760 type_wider_than (struct type
*type1
, struct type
*type2
)
762 return (type1
->length () > type2
->length ()
763 || (type1
->length () == type2
->length ()
764 && type1
->is_unsigned ()
765 && !type2
->is_unsigned ()));
769 /* Return the "wider" of the two types TYPE1 and TYPE2. */
771 max_type (struct type
*type1
, struct type
*type2
)
773 return type_wider_than (type1
, type2
) ? type1
: type2
;
777 /* Generate code to convert a scalar value of type FROM to type TO. */
779 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
781 /* Perhaps there is a more graceful way to state these rules. */
783 /* If we're converting to a narrower type, then we need to clear out
785 if (to
->length () < from
->length ())
788 /* If the two values have equal width, but different signednesses,
789 then we need to extend. */
790 else if (to
->length () == from
->length ())
792 if (from
->is_unsigned () != to
->is_unsigned ())
796 /* If we're converting to a wider type, and becoming unsigned, then
797 we need to zero out any possible sign bits. */
798 else if (to
->length () > from
->length ())
800 if (to
->is_unsigned ())
806 /* Return non-zero iff the type FROM will require any bytecodes to be
807 emitted to be converted to the type TO. */
809 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
811 agent_expr_up
ax (new agent_expr (NULL
, 0));
814 /* Actually generate the code, and see if anything came out. At the
815 moment, it would be trivial to replicate the code in
816 gen_conversion here, but in the future, when we're supporting
817 floating point and the like, it may not be. Doing things this
818 way allows this function to be independent of the logic in
820 gen_conversion (ax
.get (), from
, to
);
821 nontrivial
= ax
->len
> 0;
826 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
827 6.2.1.5) for the two operands of an arithmetic operator. This
828 effectively finds a "least upper bound" type for the two arguments,
829 and promotes each argument to that type. *VALUE1 and *VALUE2
830 describe the values as they are passed in, and as they are left. */
832 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
833 struct axs_value
*value2
)
835 /* Do the usual binary conversions. */
836 if (value1
->type
->code () == TYPE_CODE_INT
837 && value2
->type
->code () == TYPE_CODE_INT
)
839 /* The ANSI integral promotions seem to work this way: Order the
840 integer types by size, and then by signedness: an n-bit
841 unsigned type is considered "wider" than an n-bit signed
842 type. Promote to the "wider" of the two types, and always
843 promote at least to int. */
844 struct type
*target
= max_type (builtin_type (ax
->gdbarch
)->builtin_int
,
845 max_type (value1
->type
, value2
->type
));
847 /* Deal with value2, on the top of the stack. */
848 gen_conversion (ax
, value2
->type
, target
);
850 /* Deal with value1, not on the top of the stack. Don't
851 generate the `swap' instructions if we're not actually going
853 if (is_nontrivial_conversion (value1
->type
, target
))
855 ax_simple (ax
, aop_swap
);
856 gen_conversion (ax
, value1
->type
, target
);
857 ax_simple (ax
, aop_swap
);
860 value1
->type
= value2
->type
= check_typedef (target
);
865 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
866 the value on the top of the stack, as described by VALUE. Assume
867 the value has integral type. */
869 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
871 const struct builtin_type
*builtin
= builtin_type (ax
->gdbarch
);
873 if (!type_wider_than (value
->type
, builtin
->builtin_int
))
875 gen_conversion (ax
, value
->type
, builtin
->builtin_int
);
876 value
->type
= builtin
->builtin_int
;
878 else if (!type_wider_than (value
->type
, builtin
->builtin_unsigned_int
))
880 gen_conversion (ax
, value
->type
, builtin
->builtin_unsigned_int
);
881 value
->type
= builtin
->builtin_unsigned_int
;
886 /* Generate code for a cast to TYPE. */
888 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
890 /* GCC does allow casts to yield lvalues, so this should be fixed
891 before merging these changes into the trunk. */
892 require_rvalue (ax
, value
);
893 /* Dereference typedefs. */
894 type
= check_typedef (type
);
896 switch (type
->code ())
900 case TYPE_CODE_RVALUE_REF
:
901 /* It's implementation-defined, and I'll bet this is what GCC
905 case TYPE_CODE_ARRAY
:
906 case TYPE_CODE_STRUCT
:
907 case TYPE_CODE_UNION
:
909 error (_("Invalid type cast: intended type must be scalar."));
913 /* We don't have to worry about the size of the value, because
914 all our integral values are fully sign-extended, and when
915 casting pointers we can do anything we like. Is there any
916 way for us to know what GCC actually does with a cast like
921 gen_conversion (ax
, value
->type
, type
);
925 /* We could pop the value, and rely on everyone else to check
926 the type and notice that this value doesn't occupy a stack
927 slot. But for now, leave the value on the stack, and
928 preserve the "value == stack element" assumption. */
932 error (_("Casts to requested type are not yet implemented."));
940 /* Generating bytecode from GDB expressions: arithmetic */
942 /* Scale the integer on the top of the stack by the size of the target
943 of the pointer type TYPE. */
945 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
947 struct type
*element
= type
->target_type ();
949 if (element
->length () != 1)
951 ax_const_l (ax
, element
->length ());
957 /* Generate code for pointer arithmetic PTR + INT. */
959 gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
960 struct axs_value
*value1
, struct axs_value
*value2
)
962 gdb_assert (value1
->type
->is_pointer_or_reference ());
963 gdb_assert (value2
->type
->code () == TYPE_CODE_INT
);
965 gen_scale (ax
, aop_mul
, value1
->type
);
966 ax_simple (ax
, aop_add
);
967 gen_extend (ax
, value1
->type
); /* Catch overflow. */
968 value
->type
= value1
->type
;
969 value
->kind
= axs_rvalue
;
973 /* Generate code for pointer arithmetic PTR - INT. */
975 gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
976 struct axs_value
*value1
, struct axs_value
*value2
)
978 gdb_assert (value1
->type
->is_pointer_or_reference ());
979 gdb_assert (value2
->type
->code () == TYPE_CODE_INT
);
981 gen_scale (ax
, aop_mul
, value1
->type
);
982 ax_simple (ax
, aop_sub
);
983 gen_extend (ax
, value1
->type
); /* Catch overflow. */
984 value
->type
= value1
->type
;
985 value
->kind
= axs_rvalue
;
989 /* Generate code for pointer arithmetic PTR - PTR. */
991 gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
992 struct axs_value
*value1
, struct axs_value
*value2
,
993 struct type
*result_type
)
995 gdb_assert (value1
->type
->is_pointer_or_reference ());
996 gdb_assert (value2
->type
->is_pointer_or_reference ());
998 if (value1
->type
->target_type ()->length ()
999 != value2
->type
->target_type ()->length ())
1001 First argument of `-' is a pointer, but second argument is neither\n\
1002 an integer nor a pointer of the same type."));
1004 ax_simple (ax
, aop_sub
);
1005 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1006 value
->type
= result_type
;
1007 value
->kind
= axs_rvalue
;
1011 gen_equal (struct agent_expr
*ax
, struct axs_value
*value
,
1012 struct axs_value
*value1
, struct axs_value
*value2
,
1013 struct type
*result_type
)
1015 if (value1
->type
->is_pointer_or_reference () || value2
->type
->is_pointer_or_reference ())
1016 ax_simple (ax
, aop_equal
);
1018 gen_binop (ax
, value
, value1
, value2
,
1019 aop_equal
, aop_equal
, 0, "equal");
1020 value
->type
= result_type
;
1021 value
->kind
= axs_rvalue
;
1025 gen_less (struct agent_expr
*ax
, struct axs_value
*value
,
1026 struct axs_value
*value1
, struct axs_value
*value2
,
1027 struct type
*result_type
)
1029 if (value1
->type
->is_pointer_or_reference () || value2
->type
->is_pointer_or_reference ())
1030 ax_simple (ax
, aop_less_unsigned
);
1032 gen_binop (ax
, value
, value1
, value2
,
1033 aop_less_signed
, aop_less_unsigned
, 0, "less than");
1034 value
->type
= result_type
;
1035 value
->kind
= axs_rvalue
;
1038 /* Generate code for a binary operator that doesn't do pointer magic.
1039 We set VALUE to describe the result value; we assume VALUE1 and
1040 VALUE2 describe the two operands, and that they've undergone the
1041 usual binary conversions. MAY_CARRY should be non-zero iff the
1042 result needs to be extended. NAME is the English name of the
1043 operator, used in error messages */
1045 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1046 struct axs_value
*value1
, struct axs_value
*value2
,
1047 enum agent_op op
, enum agent_op op_unsigned
,
1048 int may_carry
, const char *name
)
1050 /* We only handle INT op INT. */
1051 if ((value1
->type
->code () != TYPE_CODE_INT
)
1052 || (value2
->type
->code () != TYPE_CODE_INT
))
1053 error (_("Invalid combination of types in %s."), name
);
1055 ax_simple (ax
, value1
->type
->is_unsigned () ? op_unsigned
: op
);
1057 gen_extend (ax
, value1
->type
); /* catch overflow */
1058 value
->type
= value1
->type
;
1059 value
->kind
= axs_rvalue
;
1064 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
1065 struct type
*result_type
)
1067 if (value
->type
->code () != TYPE_CODE_INT
1068 && value
->type
->code () != TYPE_CODE_PTR
)
1069 error (_("Invalid type of operand to `!'."));
1071 ax_simple (ax
, aop_log_not
);
1072 value
->type
= result_type
;
1077 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1079 if (value
->type
->code () != TYPE_CODE_INT
)
1080 error (_("Invalid type of operand to `~'."));
1082 ax_simple (ax
, aop_bit_not
);
1083 gen_extend (ax
, value
->type
);
1088 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1090 /* Dereference the value on the top of the stack. */
1092 gen_deref (struct axs_value
*value
)
1094 /* The caller should check the type, because several operators use
1095 this, and we don't know what error message to generate. */
1096 if (!value
->type
->is_pointer_or_reference ())
1097 internal_error (_("gen_deref: expected a pointer"));
1099 /* We've got an rvalue now, which is a pointer. We want to yield an
1100 lvalue, whose address is exactly that pointer. So we don't
1101 actually emit any code; we just change the type from "Pointer to
1102 T" to "T", and mark the value as an lvalue in memory. Leave it
1103 to the consumer to actually dereference it. */
1104 value
->type
= check_typedef (value
->type
->target_type ());
1105 if (value
->type
->code () == TYPE_CODE_VOID
)
1106 error (_("Attempt to dereference a generic pointer."));
1107 value
->kind
= ((value
->type
->code () == TYPE_CODE_FUNC
)
1108 ? axs_rvalue
: axs_lvalue_memory
);
1112 /* Produce the address of the lvalue on the top of the stack. */
1114 gen_address_of (struct axs_value
*value
)
1116 /* Special case for taking the address of a function. The ANSI
1117 standard describes this as a special case, too, so this
1118 arrangement is not without motivation. */
1119 if (value
->type
->code () == TYPE_CODE_FUNC
)
1120 /* The value's already an rvalue on the stack, so we just need to
1122 value
->type
= lookup_pointer_type (value
->type
);
1124 switch (value
->kind
)
1127 error (_("Operand of `&' is an rvalue, which has no address."));
1129 case axs_lvalue_register
:
1130 error (_("Operand of `&' is in a register, and has no address."));
1132 case axs_lvalue_memory
:
1133 value
->kind
= axs_rvalue
;
1134 value
->type
= lookup_pointer_type (value
->type
);
1139 /* Generate code to push the value of a bitfield of a structure whose
1140 address is on the top of the stack. START and END give the
1141 starting and one-past-ending *bit* numbers of the field within the
1144 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1145 struct type
*type
, int start
, int end
)
1147 /* Note that ops[i] fetches 8 << i bits. */
1148 static enum agent_op ops
[]
1149 = {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1150 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1152 /* We don't want to touch any byte that the bitfield doesn't
1153 actually occupy; we shouldn't make any accesses we're not
1154 explicitly permitted to. We rely here on the fact that the
1155 bytecode `ref' operators work on unaligned addresses.
1157 It takes some fancy footwork to get the stack to work the way
1158 we'd like. Say we're retrieving a bitfield that requires three
1159 fetches. Initially, the stack just contains the address:
1161 For the first fetch, we duplicate the address
1163 then add the byte offset, do the fetch, and shift and mask as
1164 needed, yielding a fragment of the value, properly aligned for
1165 the final bitwise or:
1167 then we swap, and repeat the process:
1168 frag1 addr --- address on top
1169 frag1 addr addr --- duplicate it
1170 frag1 addr frag2 --- get second fragment
1171 frag1 frag2 addr --- swap again
1172 frag1 frag2 frag3 --- get third fragment
1173 Notice that, since the third fragment is the last one, we don't
1174 bother duplicating the address this time. Now we have all the
1175 fragments on the stack, and we can simply `or' them together,
1176 yielding the final value of the bitfield. */
1178 /* The first and one-after-last bits in the field, but rounded down
1179 and up to byte boundaries. */
1180 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1181 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1185 /* current bit offset within the structure */
1188 /* The index in ops of the opcode we're considering. */
1191 /* The number of fragments we generated in the process. Probably
1192 equal to the number of `one' bits in bytesize, but who cares? */
1195 /* Dereference any typedefs. */
1196 type
= check_typedef (type
);
1198 /* Can we fetch the number of bits requested at all? */
1199 if ((end
- start
) > ((1 << num_ops
) * 8))
1200 internal_error (_("gen_bitfield_ref: bitfield too wide"));
1202 /* Note that we know here that we only need to try each opcode once.
1203 That may not be true on machines with weird byte sizes. */
1204 offset
= bound_start
;
1206 for (op
= num_ops
- 1; op
>= 0; op
--)
1208 /* number of bits that ops[op] would fetch */
1209 int op_size
= 8 << op
;
1211 /* The stack at this point, from bottom to top, contains zero or
1212 more fragments, then the address. */
1214 /* Does this fetch fit within the bitfield? */
1215 if (offset
+ op_size
<= bound_end
)
1217 /* Is this the last fragment? */
1218 int last_frag
= (offset
+ op_size
== bound_end
);
1221 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1223 /* Add the offset. */
1224 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1228 /* Record the area of memory we're about to fetch. */
1229 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1232 /* Perform the fetch. */
1233 ax_simple (ax
, ops
[op
]);
1235 /* Shift the bits we have to their proper position.
1236 gen_left_shift will generate right shifts when the operand
1239 A big-endian field diagram to ponder:
1240 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1241 +------++------++------++------++------++------++------++------+
1242 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1244 bit number 16 32 48 53
1245 These are bit numbers as supplied by GDB. Note that the
1246 bit numbers run from right to left once you've fetched the
1249 A little-endian field diagram to ponder:
1250 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1251 +------++------++------++------++------++------++------++------+
1252 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1254 bit number 48 32 16 4 0
1256 In both cases, the most significant end is on the left
1257 (i.e. normal numeric writing order), which means that you
1258 don't go crazy thinking about `left' and `right' shifts.
1260 We don't have to worry about masking yet:
1261 - If they contain garbage off the least significant end, then we
1262 must be looking at the low end of the field, and the right
1263 shift will wipe them out.
1264 - If they contain garbage off the most significant end, then we
1265 must be looking at the most significant end of the word, and
1266 the sign/zero extension will wipe them out.
1267 - If we're in the interior of the word, then there is no garbage
1268 on either end, because the ref operators zero-extend. */
1269 if (gdbarch_byte_order (ax
->gdbarch
) == BFD_ENDIAN_BIG
)
1270 gen_left_shift (ax
, end
- (offset
+ op_size
));
1272 gen_left_shift (ax
, offset
- start
);
1275 /* Bring the copy of the address up to the top. */
1276 ax_simple (ax
, aop_swap
);
1283 /* Generate enough bitwise `or' operations to combine all the
1284 fragments we left on the stack. */
1285 while (fragment_count
-- > 1)
1286 ax_simple (ax
, aop_bit_or
);
1288 /* Sign- or zero-extend the value as appropriate. */
1289 ((type
->is_unsigned () ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1291 /* This is *not* an lvalue. Ugh. */
1292 value
->kind
= axs_rvalue
;
1296 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1297 is an accumulated offset (in bytes), will be nonzero for objects
1298 embedded in other objects, like C++ base classes. Behavior should
1299 generally follow value_primitive_field. */
1302 gen_primitive_field (struct agent_expr
*ax
, struct axs_value
*value
,
1303 int offset
, int fieldno
, struct type
*type
)
1305 /* Is this a bitfield? */
1306 if (TYPE_FIELD_PACKED (type
, fieldno
))
1307 gen_bitfield_ref (ax
, value
, type
->field (fieldno
).type (),
1308 (offset
* TARGET_CHAR_BIT
1309 + type
->field (fieldno
).loc_bitpos ()),
1310 (offset
* TARGET_CHAR_BIT
1311 + type
->field (fieldno
).loc_bitpos ()
1312 + TYPE_FIELD_BITSIZE (type
, fieldno
)));
1315 gen_offset (ax
, offset
1316 + type
->field (fieldno
).loc_bitpos () / TARGET_CHAR_BIT
);
1317 value
->kind
= axs_lvalue_memory
;
1318 value
->type
= type
->field (fieldno
).type ();
1322 /* Search for the given field in either the given type or one of its
1323 base classes. Return 1 if found, 0 if not. */
1326 gen_struct_ref_recursive (struct agent_expr
*ax
, struct axs_value
*value
,
1327 const char *field
, int offset
, struct type
*type
)
1330 int nbases
= TYPE_N_BASECLASSES (type
);
1332 type
= check_typedef (type
);
1334 for (i
= type
->num_fields () - 1; i
>= nbases
; i
--)
1336 const char *this_name
= type
->field (i
).name ();
1340 if (strcmp (field
, this_name
) == 0)
1342 /* Note that bytecodes for the struct's base (aka
1343 "this") will have been generated already, which will
1344 be unnecessary but not harmful if the static field is
1345 being handled as a global. */
1346 if (field_is_static (&type
->field (i
)))
1348 gen_static_field (ax
, value
, type
, i
);
1349 if (value
->optimized_out
)
1350 error (_("static field `%s' has been "
1351 "optimized out, cannot use"),
1356 gen_primitive_field (ax
, value
, offset
, i
, type
);
1359 #if 0 /* is this right? */
1360 if (this_name
[0] == '\0')
1361 internal_error (_("find_field: anonymous unions not supported"));
1366 /* Now scan through base classes recursively. */
1367 for (i
= 0; i
< nbases
; i
++)
1369 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1371 rslt
= gen_struct_ref_recursive (ax
, value
, field
,
1372 offset
+ TYPE_BASECLASS_BITPOS (type
, i
)
1379 /* Not found anywhere, flag so caller can complain. */
1383 /* Generate code to reference the member named FIELD of a structure or
1384 union. The top of the stack, as described by VALUE, should have
1385 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1386 the operator being compiled, and OPERAND_NAME is the kind of thing
1387 it operates on; we use them in error messages. */
1389 gen_struct_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1390 const char *field
, const char *operator_name
,
1391 const char *operand_name
)
1396 /* Follow pointers until we reach a non-pointer. These aren't the C
1397 semantics, but they're what the normal GDB evaluator does, so we
1398 should at least be consistent. */
1399 while (value
->type
->is_pointer_or_reference ())
1401 require_rvalue (ax
, value
);
1404 type
= check_typedef (value
->type
);
1406 /* This must yield a structure or a union. */
1407 if (type
->code () != TYPE_CODE_STRUCT
1408 && type
->code () != TYPE_CODE_UNION
)
1409 error (_("The left operand of `%s' is not a %s."),
1410 operator_name
, operand_name
);
1412 /* And it must be in memory; we don't deal with structure rvalues,
1413 or structures living in registers. */
1414 if (value
->kind
!= axs_lvalue_memory
)
1415 error (_("Structure does not live in memory."));
1417 /* Search through fields and base classes recursively. */
1418 found
= gen_struct_ref_recursive (ax
, value
, field
, 0, type
);
1421 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1422 field
, type
->name ());
1426 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1427 const struct type
*curtype
, const char *name
);
1429 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1430 const struct type
*curtype
, const char *name
);
1433 gen_static_field (struct agent_expr
*ax
, struct axs_value
*value
,
1434 struct type
*type
, int fieldno
)
1436 if (type
->field (fieldno
).loc_kind () == FIELD_LOC_KIND_PHYSADDR
)
1438 ax_const_l (ax
, type
->field (fieldno
).loc_physaddr ());
1439 value
->kind
= axs_lvalue_memory
;
1440 value
->type
= type
->field (fieldno
).type ();
1441 value
->optimized_out
= 0;
1445 const char *phys_name
= type
->field (fieldno
).loc_physname ();
1446 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0).symbol
;
1450 gen_var_ref (ax
, value
, sym
);
1452 /* Don't error if the value was optimized out, we may be
1453 scanning all static fields and just want to pass over this
1454 and continue with the rest. */
1458 /* Silently assume this was optimized out; class printing
1459 will let the user know why the data is missing. */
1460 value
->optimized_out
= 1;
1466 gen_struct_elt_for_reference (struct agent_expr
*ax
, struct axs_value
*value
,
1467 struct type
*type
, const char *fieldname
)
1469 struct type
*t
= type
;
1472 if (t
->code () != TYPE_CODE_STRUCT
1473 && t
->code () != TYPE_CODE_UNION
)
1474 internal_error (_("non-aggregate type to gen_struct_elt_for_reference"));
1476 for (i
= t
->num_fields () - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1478 const char *t_field_name
= t
->field (i
).name ();
1480 if (t_field_name
&& strcmp (t_field_name
, fieldname
) == 0)
1482 if (field_is_static (&t
->field (i
)))
1484 gen_static_field (ax
, value
, t
, i
);
1485 if (value
->optimized_out
)
1486 error (_("static field `%s' has been "
1487 "optimized out, cannot use"),
1491 if (TYPE_FIELD_PACKED (t
, i
))
1492 error (_("pointers to bitfield members not allowed"));
1494 /* FIXME we need a way to do "want_address" equivalent */
1496 error (_("Cannot reference non-static field \"%s\""), fieldname
);
1500 /* FIXME add other scoped-reference cases here */
1502 /* Do a last-ditch lookup. */
1503 return gen_maybe_namespace_elt (ax
, value
, type
, fieldname
);
1506 /* C++: Return the member NAME of the namespace given by the type
1510 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1511 const struct type
*curtype
, const char *name
)
1513 int found
= gen_maybe_namespace_elt (ax
, value
, curtype
, name
);
1516 error (_("No symbol \"%s\" in namespace \"%s\"."),
1517 name
, curtype
->name ());
1522 /* A helper function used by value_namespace_elt and
1523 value_struct_elt_for_reference. It looks up NAME inside the
1524 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1525 is a class and NAME refers to a type in CURTYPE itself (as opposed
1526 to, say, some base class of CURTYPE). */
1529 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1530 const struct type
*curtype
, const char *name
)
1532 const char *namespace_name
= curtype
->name ();
1533 struct block_symbol sym
;
1535 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
1536 block_for_pc (ax
->scope
),
1539 if (sym
.symbol
== NULL
)
1542 gen_var_ref (ax
, value
, sym
.symbol
);
1544 if (value
->optimized_out
)
1545 error (_("`%s' has been optimized out, cannot use"),
1546 sym
.symbol
->print_name ());
1553 gen_aggregate_elt_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1554 struct type
*type
, const char *field
)
1556 switch (type
->code ())
1558 case TYPE_CODE_STRUCT
:
1559 case TYPE_CODE_UNION
:
1560 return gen_struct_elt_for_reference (ax
, value
, type
, field
);
1562 case TYPE_CODE_NAMESPACE
:
1563 return gen_namespace_elt (ax
, value
, type
, field
);
1566 internal_error (_("non-aggregate type in gen_aggregate_elt_ref"));
1578 operation::generate_ax (struct expression
*exp
,
1579 struct agent_expr
*ax
,
1580 struct axs_value
*value
,
1581 struct type
*cast_type
)
1585 struct value
*v
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
1586 ax_const_l (ax
, value_as_long (v
));
1587 value
->kind
= axs_rvalue
;
1588 value
->type
= check_typedef (value_type (v
));
1592 do_generate_ax (exp
, ax
, value
, cast_type
);
1593 if (cast_type
!= nullptr)
1594 gen_cast (ax
, value
, cast_type
);
1599 scope_operation::do_generate_ax (struct expression
*exp
,
1600 struct agent_expr
*ax
,
1601 struct axs_value
*value
,
1602 struct type
*cast_type
)
1604 struct type
*type
= std::get
<0> (m_storage
);
1605 const std::string
&name
= std::get
<1> (m_storage
);
1606 int found
= gen_aggregate_elt_ref (ax
, value
, type
, name
.c_str ());
1608 error (_("There is no field named %s"), name
.c_str ());
1612 long_const_operation::do_generate_ax (struct expression
*exp
,
1613 struct agent_expr
*ax
,
1614 struct axs_value
*value
,
1615 struct type
*cast_type
)
1617 gen_int_literal (ax
, value
, std::get
<1> (m_storage
),
1618 std::get
<0> (m_storage
));
1622 var_msym_value_operation::do_generate_ax (struct expression
*exp
,
1623 struct agent_expr
*ax
,
1624 struct axs_value
*value
,
1625 struct type
*cast_type
)
1627 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
1628 gen_msym_var_ref (ax
, value
, b
.minsym
, b
.objfile
);
1630 if (value
->type
->code () == TYPE_CODE_ERROR
)
1632 if (cast_type
== nullptr)
1633 error_unknown_type (b
.minsym
->linkage_name ());
1634 value
->type
= cast_type
;
1639 register_operation::do_generate_ax (struct expression
*exp
,
1640 struct agent_expr
*ax
,
1641 struct axs_value
*value
,
1642 struct type
*cast_type
)
1644 const char *name
= std::get
<0> (m_storage
).c_str ();
1645 int len
= std::get
<0> (m_storage
).size ();
1648 reg
= user_reg_map_name_to_regnum (ax
->gdbarch
, name
, len
);
1650 internal_error (_("Register $%s not available"), name
);
1651 /* No support for tracing user registers yet. */
1652 if (reg
>= gdbarch_num_cooked_regs (ax
->gdbarch
))
1653 error (_("'%s' is a user-register; "
1654 "GDB cannot yet trace user-register contents."),
1656 value
->kind
= axs_lvalue_register
;
1658 value
->type
= register_type (ax
->gdbarch
, reg
);
1662 internalvar_operation::do_generate_ax (struct expression
*exp
,
1663 struct agent_expr
*ax
,
1664 struct axs_value
*value
,
1665 struct type
*cast_type
)
1667 struct internalvar
*var
= std::get
<0> (m_storage
);
1668 const char *name
= internalvar_name (var
);
1669 struct trace_state_variable
*tsv
;
1671 tsv
= find_trace_state_variable (name
);
1674 ax_tsv (ax
, aop_getv
, tsv
->number
);
1676 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1677 /* Trace state variables are always 64-bit integers. */
1678 value
->kind
= axs_rvalue
;
1679 value
->type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
1681 else if (! compile_internalvar_to_ax (var
, ax
, value
))
1682 error (_("$%s is not a trace state variable; GDB agent "
1683 "expressions cannot use convenience variables."), name
);
1687 ternop_cond_operation::do_generate_ax (struct expression
*exp
,
1688 struct agent_expr
*ax
,
1689 struct axs_value
*value
,
1690 struct type
*cast_type
)
1692 struct axs_value value1
, value2
, value3
;
1695 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
1696 gen_usual_unary (ax
, &value1
);
1697 /* For (A ? B : C), it's easiest to generate subexpression
1698 bytecodes in order, but if_goto jumps on true, so we invert
1699 the sense of A. Then we can do B by dropping through, and
1701 gen_logical_not (ax
, &value1
, builtin_type (ax
->gdbarch
)->builtin_int
);
1702 if1
= ax_goto (ax
, aop_if_goto
);
1703 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, &value2
);
1704 gen_usual_unary (ax
, &value2
);
1705 end
= ax_goto (ax
, aop_goto
);
1706 ax_label (ax
, if1
, ax
->len
);
1707 std::get
<2> (m_storage
)->generate_ax (exp
, ax
, &value3
);
1708 gen_usual_unary (ax
, &value3
);
1709 ax_label (ax
, end
, ax
->len
);
1710 /* This is arbitrary - what if B and C are incompatible types? */
1711 value
->type
= value2
.type
;
1712 value
->kind
= value2
.kind
;
1715 /* Generate code for GDB's magical `repeat' operator.
1716 LVALUE @ INT creates an array INT elements long, and whose elements
1717 have the same type as LVALUE, located in memory so that LVALUE is
1718 its first element. For example, argv[0]@argc gives you the array
1719 of command-line arguments.
1721 Unfortunately, because we have to know the types before we actually
1722 have a value for the expression, we can't implement this perfectly
1723 without changing the type system, having values that occupy two
1724 stack slots, doing weird things with sizeof, etc. So we require
1725 the right operand to be a constant expression. */
1727 repeat_operation::do_generate_ax (struct expression
*exp
,
1728 struct agent_expr
*ax
,
1729 struct axs_value
*value
,
1730 struct type
*cast_type
)
1732 struct axs_value value1
;
1734 /* We don't want to turn this into an rvalue, so no conversions
1736 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
1737 if (value1
.kind
!= axs_lvalue_memory
)
1738 error (_("Left operand of `@' must be an object in memory."));
1740 /* Evaluate the length; it had better be a constant. */
1741 if (!std::get
<1> (m_storage
)->constant_p ())
1742 error (_("Right operand of `@' must be a "
1743 "constant, in agent expressions."));
1746 = std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
1747 EVAL_AVOID_SIDE_EFFECTS
);
1748 if (value_type (v
)->code () != TYPE_CODE_INT
)
1749 error (_("Right operand of `@' must be an integer."));
1750 int length
= value_as_long (v
);
1752 error (_("Right operand of `@' must be positive."));
1754 /* The top of the stack is already the address of the object, so
1755 all we need to do is frob the type of the lvalue. */
1756 /* FIXME-type-allocation: need a way to free this type when we are
1759 = lookup_array_range_type (value1
.type
, 0, length
- 1);
1761 value
->kind
= axs_lvalue_memory
;
1762 value
->type
= array
;
1766 comma_operation::do_generate_ax (struct expression
*exp
,
1767 struct agent_expr
*ax
,
1768 struct axs_value
*value
,
1769 struct type
*cast_type
)
1771 /* Note that we need to be a little subtle about generating code
1772 for comma. In C, we can do some optimizations here because
1773 we know the left operand is only being evaluated for effect.
1774 However, if the tracing kludge is in effect, then we always
1775 need to evaluate the left hand side fully, so that all the
1776 variables it mentions get traced. */
1777 struct axs_value value1
;
1778 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
1779 /* Don't just dispose of the left operand. We might be tracing,
1780 in which case we want to emit code to trace it if it's an
1782 gen_traced_pop (ax
, &value1
);
1783 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
1784 /* It's the consumer's responsibility to trace the right operand. */
1788 unop_sizeof_operation::do_generate_ax (struct expression
*exp
,
1789 struct agent_expr
*ax
,
1790 struct axs_value
*value
,
1791 struct type
*cast_type
)
1793 /* We don't care about the value of the operand expression; we only
1794 care about its type. However, in the current arrangement, the
1795 only way to find an expression's type is to generate code for it.
1796 So we generate code for the operand, and then throw it away,
1797 replacing it with code that simply pushes its size. */
1798 int start
= ax
->len
;
1800 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
1802 /* Throw away the code we just generated. */
1805 ax_const_l (ax
, value
->type
->length ());
1806 value
->kind
= axs_rvalue
;
1807 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
1811 unop_cast_operation::do_generate_ax (struct expression
*exp
,
1812 struct agent_expr
*ax
,
1813 struct axs_value
*value
,
1814 struct type
*cast_type
)
1816 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
,
1817 std::get
<1> (m_storage
));
1821 unop_extract_operation::do_generate_ax (struct expression
*exp
,
1822 struct agent_expr
*ax
,
1823 struct axs_value
*value
,
1824 struct type
*cast_type
)
1826 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
1828 struct type
*to_type
= get_type ();
1830 if (!is_scalar_type (to_type
))
1831 error (_("can't generate agent expression to extract non-scalar type"));
1833 if (to_type
->is_unsigned ())
1834 gen_extend (ax
, to_type
);
1836 gen_sign_extend (ax
, to_type
);
1840 unop_memval_operation::do_generate_ax (struct expression
*exp
,
1841 struct agent_expr
*ax
,
1842 struct axs_value
*value
,
1843 struct type
*cast_type
)
1845 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
1846 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
1847 already have the right value on the stack. For
1848 axs_lvalue_register, we must convert. */
1849 if (value
->kind
== axs_lvalue_register
)
1850 require_rvalue (ax
, value
);
1852 value
->type
= std::get
<1> (m_storage
);
1853 value
->kind
= axs_lvalue_memory
;
1857 unop_memval_type_operation::do_generate_ax (struct expression
*exp
,
1858 struct agent_expr
*ax
,
1859 struct axs_value
*value
,
1860 struct type
*cast_type
)
1863 = std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1864 EVAL_AVOID_SIDE_EFFECTS
);
1865 struct type
*type
= value_type (val
);
1867 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
1869 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
1870 already have the right value on the stack. For
1871 axs_lvalue_register, we must convert. */
1872 if (value
->kind
== axs_lvalue_register
)
1873 require_rvalue (ax
, value
);
1876 value
->kind
= axs_lvalue_memory
;
1880 op_this_operation::do_generate_ax (struct expression
*exp
,
1881 struct agent_expr
*ax
,
1882 struct axs_value
*value
,
1883 struct type
*cast_type
)
1885 struct symbol
*sym
, *func
;
1886 const struct block
*b
;
1887 const struct language_defn
*lang
;
1889 b
= block_for_pc (ax
->scope
);
1890 func
= block_linkage_function (b
);
1891 lang
= language_def (func
->language ());
1893 sym
= lookup_language_this (lang
, b
).symbol
;
1895 error (_("no `%s' found"), lang
->name_of_this ());
1897 gen_var_ref (ax
, value
, sym
);
1899 if (value
->optimized_out
)
1900 error (_("`%s' has been optimized out, cannot use"),
1901 sym
->print_name ());
1905 assign_operation::do_generate_ax (struct expression
*exp
,
1906 struct agent_expr
*ax
,
1907 struct axs_value
*value
,
1908 struct type
*cast_type
)
1910 operation
*subop
= std::get
<0> (m_storage
).get ();
1911 if (subop
->opcode () != OP_INTERNALVAR
)
1912 error (_("May only assign to trace state variables"));
1914 internalvar_operation
*ivarop
1915 = gdb::checked_static_cast
<internalvar_operation
*> (subop
);
1917 const char *name
= internalvar_name (ivarop
->get_internalvar ());
1918 struct trace_state_variable
*tsv
;
1920 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
1921 tsv
= find_trace_state_variable (name
);
1924 ax_tsv (ax
, aop_setv
, tsv
->number
);
1926 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1929 error (_("$%s is not a trace state variable, "
1930 "may not assign to it"), name
);
1934 assign_modify_operation::do_generate_ax (struct expression
*exp
,
1935 struct agent_expr
*ax
,
1936 struct axs_value
*value
,
1937 struct type
*cast_type
)
1939 operation
*subop
= std::get
<1> (m_storage
).get ();
1940 if (subop
->opcode () != OP_INTERNALVAR
)
1941 error (_("May only assign to trace state variables"));
1943 internalvar_operation
*ivarop
1944 = gdb::checked_static_cast
<internalvar_operation
*> (subop
);
1946 const char *name
= internalvar_name (ivarop
->get_internalvar ());
1947 struct trace_state_variable
*tsv
;
1949 tsv
= find_trace_state_variable (name
);
1952 /* The tsv will be the left half of the binary operation. */
1953 ax_tsv (ax
, aop_getv
, tsv
->number
);
1955 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1956 /* Trace state variables are always 64-bit integers. */
1957 struct axs_value value1
, value2
;
1958 value1
.kind
= axs_rvalue
;
1959 value1
.type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
1960 /* Now do right half of expression. */
1961 std::get
<2> (m_storage
)->generate_ax (exp
, ax
, &value2
);
1962 gen_expr_binop_rest (exp
, std::get
<0> (m_storage
), ax
,
1963 value
, &value1
, &value2
);
1964 /* We have a result of the binary op, set the tsv. */
1965 ax_tsv (ax
, aop_setv
, tsv
->number
);
1967 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1970 error (_("$%s is not a trace state variable, "
1971 "may not assign to it"), name
);
1975 unop_cast_type_operation::do_generate_ax (struct expression
*exp
,
1976 struct agent_expr
*ax
,
1977 struct axs_value
*value
,
1978 struct type
*cast_type
)
1981 = std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1982 EVAL_AVOID_SIDE_EFFECTS
);
1983 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
, value_type (val
));
1987 var_value_operation::do_generate_ax (struct expression
*exp
,
1988 struct agent_expr
*ax
,
1989 struct axs_value
*value
,
1990 struct type
*cast_type
)
1992 gen_var_ref (ax
, value
, std::get
<0> (m_storage
).symbol
);
1994 if (value
->optimized_out
)
1995 error (_("`%s' has been optimized out, cannot use"),
1996 std::get
<0> (m_storage
).symbol
->print_name ());
1998 if (value
->type
->code () == TYPE_CODE_ERROR
)
2000 if (cast_type
== nullptr)
2001 error_unknown_type (std::get
<0> (m_storage
).symbol
->print_name ());
2002 value
->type
= cast_type
;
2007 logical_and_operation::do_generate_ax (struct expression
*exp
,
2008 struct agent_expr
*ax
,
2009 struct axs_value
*value
,
2010 struct type
*cast_type
)
2012 struct axs_value value1
, value2
;
2013 int if1
, go1
, if2
, go2
, end
;
2015 /* Generate the obvious sequence of tests and jumps. */
2016 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
2017 gen_usual_unary (ax
, &value1
);
2018 if1
= ax_goto (ax
, aop_if_goto
);
2019 go1
= ax_goto (ax
, aop_goto
);
2020 ax_label (ax
, if1
, ax
->len
);
2021 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, &value2
);
2022 gen_usual_unary (ax
, &value2
);
2023 if2
= ax_goto (ax
, aop_if_goto
);
2024 go2
= ax_goto (ax
, aop_goto
);
2025 ax_label (ax
, if2
, ax
->len
);
2027 end
= ax_goto (ax
, aop_goto
);
2028 ax_label (ax
, go1
, ax
->len
);
2029 ax_label (ax
, go2
, ax
->len
);
2031 ax_label (ax
, end
, ax
->len
);
2032 value
->kind
= axs_rvalue
;
2033 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2037 logical_or_operation::do_generate_ax (struct expression
*exp
,
2038 struct agent_expr
*ax
,
2039 struct axs_value
*value
,
2040 struct type
*cast_type
)
2042 struct axs_value value1
, value2
;
2045 /* Generate the obvious sequence of tests and jumps. */
2046 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
2047 gen_usual_unary (ax
, &value1
);
2048 if1
= ax_goto (ax
, aop_if_goto
);
2049 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, &value2
);
2050 gen_usual_unary (ax
, &value2
);
2051 if2
= ax_goto (ax
, aop_if_goto
);
2053 end
= ax_goto (ax
, aop_goto
);
2054 ax_label (ax
, if1
, ax
->len
);
2055 ax_label (ax
, if2
, ax
->len
);
2057 ax_label (ax
, end
, ax
->len
);
2058 value
->kind
= axs_rvalue
;
2059 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2064 /* This handles the middle-to-right-side of code generation for binary
2065 expressions, which is shared between regular binary operations and
2066 assign-modify (+= and friends) expressions. */
2069 gen_expr_binop_rest (struct expression
*exp
,
2071 struct agent_expr
*ax
, struct axs_value
*value
,
2072 struct axs_value
*value1
, struct axs_value
*value2
)
2074 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2076 gen_usual_unary (ax
, value2
);
2077 gen_usual_arithmetic (ax
, value1
, value2
);
2081 if (value1
->type
->code () == TYPE_CODE_INT
2082 && value2
->type
->is_pointer_or_reference ())
2084 /* Swap the values and proceed normally. */
2085 ax_simple (ax
, aop_swap
);
2086 gen_ptradd (ax
, value
, value2
, value1
);
2088 else if (value1
->type
->is_pointer_or_reference ()
2089 && value2
->type
->code () == TYPE_CODE_INT
)
2090 gen_ptradd (ax
, value
, value1
, value2
);
2092 gen_binop (ax
, value
, value1
, value2
,
2093 aop_add
, aop_add
, 1, "addition");
2096 if (value1
->type
->is_pointer_or_reference ()
2097 && value2
->type
->code () == TYPE_CODE_INT
)
2098 gen_ptrsub (ax
,value
, value1
, value2
);
2099 else if (value1
->type
->is_pointer_or_reference ()
2100 && value2
->type
->is_pointer_or_reference ())
2101 /* FIXME --- result type should be ptrdiff_t */
2102 gen_ptrdiff (ax
, value
, value1
, value2
,
2103 builtin_type (ax
->gdbarch
)->builtin_long
);
2105 gen_binop (ax
, value
, value1
, value2
,
2106 aop_sub
, aop_sub
, 1, "subtraction");
2109 gen_binop (ax
, value
, value1
, value2
,
2110 aop_mul
, aop_mul
, 1, "multiplication");
2113 gen_binop (ax
, value
, value1
, value2
,
2114 aop_div_signed
, aop_div_unsigned
, 1, "division");
2117 gen_binop (ax
, value
, value1
, value2
,
2118 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
2121 gen_binop (ax
, value
, value1
, value2
,
2122 aop_lsh
, aop_lsh
, 1, "left shift");
2125 gen_binop (ax
, value
, value1
, value2
,
2126 aop_rsh_signed
, aop_rsh_unsigned
, 1, "right shift");
2128 case BINOP_SUBSCRIPT
:
2132 if (binop_types_user_defined_p (op
, value1
->type
, value2
->type
))
2134 error (_("cannot subscript requested type: "
2135 "cannot call user defined functions"));
2139 /* If the user attempts to subscript something that is not
2140 an array or pointer type (like a plain int variable for
2141 example), then report this as an error. */
2142 type
= check_typedef (value1
->type
);
2143 if (type
->code () != TYPE_CODE_ARRAY
2144 && type
->code () != TYPE_CODE_PTR
)
2147 error (_("cannot subscript something of type `%s'"),
2150 error (_("cannot subscript requested type"));
2154 if (!is_integral_type (value2
->type
))
2155 error (_("Argument to arithmetic operation "
2156 "not a number or boolean."));
2158 gen_ptradd (ax
, value
, value1
, value2
);
2162 case BINOP_BITWISE_AND
:
2163 gen_binop (ax
, value
, value1
, value2
,
2164 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
2167 case BINOP_BITWISE_IOR
:
2168 gen_binop (ax
, value
, value1
, value2
,
2169 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
2172 case BINOP_BITWISE_XOR
:
2173 gen_binop (ax
, value
, value1
, value2
,
2174 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
2178 gen_equal (ax
, value
, value1
, value2
, int_type
);
2181 case BINOP_NOTEQUAL
:
2182 gen_equal (ax
, value
, value1
, value2
, int_type
);
2183 gen_logical_not (ax
, value
, int_type
);
2187 gen_less (ax
, value
, value1
, value2
, int_type
);
2191 ax_simple (ax
, aop_swap
);
2192 gen_less (ax
, value
, value1
, value2
, int_type
);
2196 ax_simple (ax
, aop_swap
);
2197 gen_less (ax
, value
, value1
, value2
, int_type
);
2198 gen_logical_not (ax
, value
, int_type
);
2202 gen_less (ax
, value
, value1
, value2
, int_type
);
2203 gen_logical_not (ax
, value
, int_type
);
2207 /* We should only list operators in the outer case statement
2208 that we actually handle in the inner case statement. */
2209 internal_error (_("gen_expr: op case sets don't match"));
2213 /* A helper function that emits a binop based on two operations. */
2216 gen_expr_binop (struct expression
*exp
,
2218 expr::operation
*lhs
, expr::operation
*rhs
,
2219 struct agent_expr
*ax
, struct axs_value
*value
)
2221 struct axs_value value1
, value2
;
2223 lhs
->generate_ax (exp
, ax
, &value1
);
2224 gen_usual_unary (ax
, &value1
);
2225 rhs
->generate_ax (exp
, ax
, &value2
);
2226 gen_expr_binop_rest (exp
, op
, ax
, value
, &value1
, &value2
);
2229 /* A helper function that emits a structop based on an operation and a
2233 gen_expr_structop (struct expression
*exp
,
2235 expr::operation
*lhs
,
2237 struct agent_expr
*ax
, struct axs_value
*value
)
2239 lhs
->generate_ax (exp
, ax
, value
);
2240 if (op
== STRUCTOP_STRUCT
)
2241 gen_struct_ref (ax
, value
, name
, ".", "structure or union");
2242 else if (op
== STRUCTOP_PTR
)
2243 gen_struct_ref (ax
, value
, name
, "->",
2244 "pointer to a structure or union");
2246 /* If this `if' chain doesn't handle it, then the case list
2247 shouldn't mention it, and we shouldn't be here. */
2248 internal_error (_("gen_expr: unhandled struct case"));
2251 /* A helper function that emits a unary operation. */
2254 gen_expr_unop (struct expression
*exp
,
2256 expr::operation
*lhs
,
2257 struct agent_expr
*ax
, struct axs_value
*value
)
2259 struct axs_value value1
, value2
;
2264 gen_int_literal (ax
, &value1
, 0,
2265 builtin_type (ax
->gdbarch
)->builtin_int
);
2266 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
2267 lhs
->generate_ax (exp
, ax
, &value2
);
2268 gen_usual_unary (ax
, &value2
);
2269 gen_usual_arithmetic (ax
, &value1
, &value2
);
2270 gen_binop (ax
, value
, &value1
, &value2
, aop_sub
, aop_sub
, 1, "negation");
2274 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2275 lhs
->generate_ax (exp
, ax
, value
);
2276 gen_usual_unary (ax
, value
);
2279 case UNOP_LOGICAL_NOT
:
2280 lhs
->generate_ax (exp
, ax
, value
);
2281 gen_usual_unary (ax
, value
);
2282 gen_logical_not (ax
, value
, builtin_type (ax
->gdbarch
)->builtin_int
);
2285 case UNOP_COMPLEMENT
:
2286 lhs
->generate_ax (exp
, ax
, value
);
2287 gen_usual_unary (ax
, value
);
2288 gen_integral_promotions (ax
, value
);
2289 gen_complement (ax
, value
);
2293 lhs
->generate_ax (exp
, ax
, value
);
2294 gen_usual_unary (ax
, value
);
2295 if (!value
->type
->is_pointer_or_reference ())
2296 error (_("Argument of unary `*' is not a pointer."));
2301 lhs
->generate_ax (exp
, ax
, value
);
2302 gen_address_of (value
);
2306 gdb_assert_not_reached ("invalid case in gen_expr_unop");
2312 /* Given a single variable and a scope, generate bytecodes to trace
2313 its value. This is for use in situations where we have only a
2314 variable's name, and no parsed expression; for instance, when the
2315 name comes from a list of local variables of a function. */
2318 gen_trace_for_var (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2319 struct symbol
*var
, int trace_string
)
2321 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2322 struct axs_value value
;
2325 ax
->trace_string
= trace_string
;
2326 gen_var_ref (ax
.get (), &value
, var
);
2328 /* If there is no actual variable to trace, flag it by returning
2329 an empty agent expression. */
2330 if (value
.optimized_out
)
2331 return agent_expr_up ();
2333 /* Make sure we record the final object, and get rid of it. */
2334 gen_traced_pop (ax
.get (), &value
);
2336 /* Oh, and terminate. */
2337 ax_simple (ax
.get (), aop_end
);
2342 /* Generating bytecode from GDB expressions: driver */
2344 /* Given a GDB expression EXPR, return bytecode to trace its value.
2345 The result will use the `trace' and `trace_quick' bytecodes to
2346 record the value of all memory touched by the expression. The
2347 caller can then use the ax_reqs function to discover which
2348 registers it relies upon. */
2351 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
,
2354 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2355 struct axs_value value
;
2358 ax
->trace_string
= trace_string
;
2359 value
.optimized_out
= 0;
2360 expr
->op
->generate_ax (expr
, ax
.get (), &value
);
2362 /* Make sure we record the final object, and get rid of it. */
2363 gen_traced_pop (ax
.get (), &value
);
2365 /* Oh, and terminate. */
2366 ax_simple (ax
.get (), aop_end
);
2371 /* Given a GDB expression EXPR, return a bytecode sequence that will
2372 evaluate and return a result. The bytecodes will do a direct
2373 evaluation, using the current data on the target, rather than
2374 recording blocks of memory and registers for later use, as
2375 gen_trace_for_expr does. The generated bytecode sequence leaves
2376 the result of expression evaluation on the top of the stack. */
2379 gen_eval_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2381 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2382 struct axs_value value
;
2385 value
.optimized_out
= 0;
2386 expr
->op
->generate_ax (expr
, ax
.get (), &value
);
2388 require_rvalue (ax
.get (), &value
);
2390 /* Oh, and terminate. */
2391 ax_simple (ax
.get (), aop_end
);
2397 gen_trace_for_return_address (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2400 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2401 struct axs_value value
;
2404 ax
->trace_string
= trace_string
;
2406 gdbarch_gen_return_address (gdbarch
, ax
.get (), &value
, scope
);
2408 /* Make sure we record the final object, and get rid of it. */
2409 gen_traced_pop (ax
.get (), &value
);
2411 /* Oh, and terminate. */
2412 ax_simple (ax
.get (), aop_end
);
2417 /* Given a collection of printf-style arguments, generate code to
2418 evaluate the arguments and pass everything to a special
2422 gen_printf (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2423 CORE_ADDR function
, LONGEST channel
,
2424 const char *format
, int fmtlen
,
2425 int nargs
, struct expression
**exprs
)
2427 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2428 struct axs_value value
;
2431 /* We're computing values, not doing side effects. */
2434 /* Evaluate and push the args on the stack in reverse order,
2435 for simplicity of collecting them on the target side. */
2436 for (tem
= nargs
- 1; tem
>= 0; --tem
)
2438 value
.optimized_out
= 0;
2439 exprs
[tem
]->op
->generate_ax (exprs
[tem
], ax
.get (), &value
);
2440 require_rvalue (ax
.get (), &value
);
2443 /* Push function and channel. */
2444 ax_const_l (ax
.get (), channel
);
2445 ax_const_l (ax
.get (), function
);
2447 /* Issue the printf bytecode proper. */
2448 ax_simple (ax
.get (), aop_printf
);
2449 ax_raw_byte (ax
.get (), nargs
);
2450 ax_string (ax
.get (), format
, fmtlen
);
2452 /* And terminate. */
2453 ax_simple (ax
.get (), aop_end
);
2459 agent_eval_command_one (const char *exp
, int eval
, CORE_ADDR pc
)
2462 int trace_string
= 0;
2467 exp
= decode_agent_options (exp
, &trace_string
);
2470 agent_expr_up agent
;
2473 if (!eval
&& strcmp (arg
, "$_ret") == 0)
2475 agent
= gen_trace_for_return_address (pc
, get_current_arch (),
2480 expression_up expr
= parse_exp_1 (&arg
, pc
, block_for_pc (pc
), 0);
2484 gdb_assert (trace_string
== 0);
2485 agent
= gen_eval_for_expr (pc
, expr
.get ());
2488 agent
= gen_trace_for_expr (pc
, expr
.get (), trace_string
);
2491 ax_reqs (agent
.get ());
2492 ax_print (gdb_stdout
, agent
.get ());
2494 /* It would be nice to call ax_reqs here to gather some general info
2495 about the expression, and then print out the result. */
2501 maint_agent_command_1 (const char *exp
, int eval
)
2503 /* We don't deal with overlay debugging at the moment. We need to
2504 think more carefully about this. If you copy this code into
2505 another command, change the error message; the user shouldn't
2506 have to know anything about agent expressions. */
2507 if (overlay_debugging
)
2508 error (_("GDB can't do agent expression translation with overlays."));
2511 error_no_arg (_("expression to translate"));
2513 if (check_for_argument (&exp
, "-at", sizeof ("-at") - 1))
2515 struct linespec_result canonical
;
2517 location_spec_up locspec
2518 = new_linespec_location_spec (&exp
, symbol_name_match_type::WILD
);
2519 decode_line_full (locspec
.get (), DECODE_LINE_FUNFIRSTLINE
, NULL
,
2520 NULL
, 0, &canonical
,
2522 exp
= skip_spaces (exp
);
2526 exp
= skip_spaces (exp
);
2528 for (const auto &lsal
: canonical
.lsals
)
2529 for (const auto &sal
: lsal
.sals
)
2530 agent_eval_command_one (exp
, eval
, sal
.pc
);
2533 agent_eval_command_one (exp
, eval
, get_frame_pc (get_current_frame ()));
2539 maint_agent_command (const char *exp
, int from_tty
)
2541 maint_agent_command_1 (exp
, 0);
2544 /* Parse the given expression, compile it into an agent expression
2545 that does direct evaluation, and display the resulting
2549 maint_agent_eval_command (const char *exp
, int from_tty
)
2551 maint_agent_command_1 (exp
, 1);
2554 /* Parse the given expression, compile it into an agent expression
2555 that does a printf, and display the resulting expression. */
2558 maint_agent_printf_command (const char *cmdrest
, int from_tty
)
2560 frame_info_ptr fi
= get_current_frame (); /* need current scope */
2561 const char *format_start
, *format_end
;
2563 /* We don't deal with overlay debugging at the moment. We need to
2564 think more carefully about this. If you copy this code into
2565 another command, change the error message; the user shouldn't
2566 have to know anything about agent expressions. */
2567 if (overlay_debugging
)
2568 error (_("GDB can't do agent expression translation with overlays."));
2571 error_no_arg (_("expression to translate"));
2573 cmdrest
= skip_spaces (cmdrest
);
2575 if (*cmdrest
++ != '"')
2576 error (_("Must start with a format string."));
2578 format_start
= cmdrest
;
2580 format_pieces
fpieces (&cmdrest
);
2582 format_end
= cmdrest
;
2584 if (*cmdrest
++ != '"')
2585 error (_("Bad format string, non-terminated '\"'."));
2587 cmdrest
= skip_spaces (cmdrest
);
2589 if (*cmdrest
!= ',' && *cmdrest
!= 0)
2590 error (_("Invalid argument syntax"));
2592 if (*cmdrest
== ',')
2594 cmdrest
= skip_spaces (cmdrest
);
2596 std::vector
<struct expression
*> argvec
;
2597 while (*cmdrest
!= '\0')
2602 expression_up expr
= parse_exp_1 (&cmd1
, 0, (struct block
*) 0, 1);
2603 argvec
.push_back (expr
.release ());
2605 if (*cmdrest
== ',')
2607 /* else complain? */
2611 agent_expr_up agent
= gen_printf (get_frame_pc (fi
), get_current_arch (),
2613 format_start
, format_end
- format_start
,
2614 argvec
.size (), argvec
.data ());
2615 ax_reqs (agent
.get ());
2616 ax_print (gdb_stdout
, agent
.get ());
2618 /* It would be nice to call ax_reqs here to gather some general info
2619 about the expression, and then print out the result. */
2624 /* Initialization code. */
2626 void _initialize_ax_gdb ();
2628 _initialize_ax_gdb ()
2630 add_cmd ("agent", class_maintenance
, maint_agent_command
,
2632 Translate an expression into remote agent bytecode for tracing.\n\
2633 Usage: maint agent [-at LOCATION,] EXPRESSION\n\
2634 If -at is given, generate remote agent bytecode for this location.\n\
2635 If not, generate remote agent bytecode for current frame pc address."),
2638 add_cmd ("agent-eval", class_maintenance
, maint_agent_eval_command
,
2640 Translate an expression into remote agent bytecode for evaluation.\n\
2641 Usage: maint agent-eval [-at LOCATION,] EXPRESSION\n\
2642 If -at is given, generate remote agent bytecode for this location.\n\
2643 If not, generate remote agent bytecode for current frame pc address."),
2646 add_cmd ("agent-printf", class_maintenance
, maint_agent_printf_command
,
2647 _("Translate an expression into remote "
2648 "agent bytecode for evaluation and display the bytecodes."),