1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998-2021 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 union exp_element ** --- pointer into expression
74 struct agent_expr * --- agent expression buffer to generate code into
75 struct axs_value * --- describes value left on top of stack */
77 static struct value
*const_var_ref (struct symbol
*var
);
78 static struct value
*const_expr (union exp_element
**pc
);
79 static struct value
*maybe_const_expr (union exp_element
**pc
);
81 static void gen_traced_pop (struct agent_expr
*, struct axs_value
*);
83 static void gen_sign_extend (struct agent_expr
*, struct type
*);
84 static void gen_extend (struct agent_expr
*, struct type
*);
85 static void gen_fetch (struct agent_expr
*, struct type
*);
86 static void gen_left_shift (struct agent_expr
*, int);
89 static void gen_frame_args_address (struct agent_expr
*);
90 static void gen_frame_locals_address (struct agent_expr
*);
91 static void gen_offset (struct agent_expr
*ax
, int offset
);
92 static void gen_sym_offset (struct agent_expr
*, struct symbol
*);
93 static void gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
,
97 static void gen_int_literal (struct agent_expr
*ax
,
98 struct axs_value
*value
,
99 LONGEST k
, struct type
*type
);
101 static void gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
);
102 static int type_wider_than (struct type
*type1
, struct type
*type2
);
103 static struct type
*max_type (struct type
*type1
, struct type
*type2
);
104 static void gen_conversion (struct agent_expr
*ax
,
105 struct type
*from
, struct type
*to
);
106 static int is_nontrivial_conversion (struct type
*from
, struct type
*to
);
107 static void gen_usual_arithmetic (struct agent_expr
*ax
,
108 struct axs_value
*value1
,
109 struct axs_value
*value2
);
110 static void gen_integral_promotions (struct agent_expr
*ax
,
111 struct axs_value
*value
);
112 static void gen_cast (struct agent_expr
*ax
,
113 struct axs_value
*value
, struct type
*type
);
114 static void gen_scale (struct agent_expr
*ax
,
115 enum agent_op op
, struct type
*type
);
116 static void gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
117 struct axs_value
*value1
, struct axs_value
*value2
);
118 static void gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
119 struct axs_value
*value1
, struct axs_value
*value2
);
120 static void gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
121 struct axs_value
*value1
, struct axs_value
*value2
,
122 struct type
*result_type
);
123 static void gen_binop (struct agent_expr
*ax
,
124 struct axs_value
*value
,
125 struct axs_value
*value1
,
126 struct axs_value
*value2
,
128 enum agent_op op_unsigned
, int may_carry
,
130 static void gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
131 struct type
*result_type
);
132 static void gen_complement (struct agent_expr
*ax
, struct axs_value
*value
);
133 static void gen_deref (struct axs_value
*);
134 static void gen_address_of (struct axs_value
*);
135 static void gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
136 struct type
*type
, int start
, int end
);
137 static void gen_primitive_field (struct agent_expr
*ax
,
138 struct axs_value
*value
,
139 int offset
, int fieldno
, struct type
*type
);
140 static int gen_struct_ref_recursive (struct agent_expr
*ax
,
141 struct axs_value
*value
,
142 const char *field
, int offset
,
144 static void gen_struct_ref (struct agent_expr
*ax
,
145 struct axs_value
*value
,
147 const char *operator_name
,
148 const char *operand_name
);
149 static void gen_static_field (struct agent_expr
*ax
, struct axs_value
*value
,
150 struct type
*type
, int fieldno
);
151 static void gen_repeat (struct expression
*exp
, union exp_element
**pc
,
152 struct agent_expr
*ax
, struct axs_value
*value
);
153 static void gen_sizeof (struct expression
*exp
, union exp_element
**pc
,
154 struct agent_expr
*ax
, struct axs_value
*value
,
155 struct type
*size_type
);
156 static void gen_expr_binop_rest (struct expression
*exp
,
157 enum exp_opcode op
, union exp_element
**pc
,
158 struct agent_expr
*ax
,
159 struct axs_value
*value
,
160 struct axs_value
*value1
,
161 struct axs_value
*value2
);
162 static void gen_expr_binop_rest (struct expression
*exp
,
164 struct agent_expr
*ax
,
165 struct axs_value
*value
,
166 struct axs_value
*value1
,
167 struct axs_value
*value2
);
170 /* Detecting constant expressions. */
172 /* If the variable reference at *PC is a constant, return its value.
173 Otherwise, return zero.
175 Hey, Wally! How can a variable reference be a constant?
177 Well, Beav, this function really handles the OP_VAR_VALUE operator,
178 not specifically variable references. GDB uses OP_VAR_VALUE to
179 refer to any kind of symbolic reference: function names, enum
180 elements, and goto labels are all handled through the OP_VAR_VALUE
181 operator, even though they're constants. It makes sense given the
184 Gee, Wally, don'cha wonder sometimes if data representations that
185 subvert commonly accepted definitions of terms in favor of heavily
186 context-specific interpretations are really just a tool of the
187 programming hegemony to preserve their power and exclude the
190 static struct value
*
191 const_var_ref (struct symbol
*var
)
193 struct type
*type
= SYMBOL_TYPE (var
);
195 switch (SYMBOL_CLASS (var
))
198 return value_from_longest (type
, (LONGEST
) SYMBOL_VALUE (var
));
201 return value_from_pointer (type
, (CORE_ADDR
) SYMBOL_VALUE_ADDRESS (var
));
209 /* If the expression starting at *PC has a constant value, return it.
210 Otherwise, return zero. If we return a value, then *PC will be
211 advanced to the end of it. If we return zero, *PC could be
213 static struct value
*
214 const_expr (union exp_element
**pc
)
216 enum exp_opcode op
= (*pc
)->opcode
;
223 struct type
*type
= (*pc
)[1].type
;
224 LONGEST k
= (*pc
)[2].longconst
;
227 return value_from_longest (type
, k
);
232 struct value
*v
= const_var_ref ((*pc
)[2].symbol
);
238 /* We could add more operators in here. */
242 v1
= const_expr (pc
);
244 return value_neg (v1
);
254 /* Like const_expr, but guarantee also that *PC is undisturbed if the
255 expression is not constant. */
256 static struct value
*
257 maybe_const_expr (union exp_element
**pc
)
259 union exp_element
*tentative_pc
= *pc
;
260 struct value
*v
= const_expr (&tentative_pc
);
262 /* If we got a value, then update the real PC. */
270 /* Generating bytecode from GDB expressions: general assumptions */
272 /* Here are a few general assumptions made throughout the code; if you
273 want to make a change that contradicts one of these, then you'd
274 better scan things pretty thoroughly.
276 - We assume that all values occupy one stack element. For example,
277 sometimes we'll swap to get at the left argument to a binary
278 operator. If we decide that void values should occupy no stack
279 elements, or that synthetic arrays (whose size is determined at
280 run time, created by the `@' operator) should occupy two stack
281 elements (address and length), then this will cause trouble.
283 - We assume the stack elements are infinitely wide, and that we
284 don't have to worry what happens if the user requests an
285 operation that is wider than the actual interpreter's stack.
286 That is, it's up to the interpreter to handle directly all the
287 integer widths the user has access to. (Woe betide the language
290 - We don't support side effects. Thus, we don't have to worry about
291 GCC's generalized lvalues, function calls, etc.
293 - We don't support floating point. Many places where we switch on
294 some type don't bother to include cases for floating point; there
295 may be even more subtle ways this assumption exists. For
296 example, the arguments to % must be integers.
298 - We assume all subexpressions have a static, unchanging type. If
299 we tried to support convenience variables, this would be a
302 - All values on the stack should always be fully zero- or
305 (I wasn't sure whether to choose this or its opposite --- that
306 only addresses are assumed extended --- but it turns out that
307 neither convention completely eliminates spurious extend
308 operations (if everything is always extended, then you have to
309 extend after add, because it could overflow; if nothing is
310 extended, then you end up producing extends whenever you change
311 sizes), and this is simpler.) */
314 /* Scan for all static fields in the given class, including any base
315 classes, and generate tracing bytecodes for each. */
318 gen_trace_static_fields (struct agent_expr
*ax
,
321 int i
, nbases
= TYPE_N_BASECLASSES (type
);
322 struct axs_value value
;
324 type
= check_typedef (type
);
326 for (i
= type
->num_fields () - 1; i
>= nbases
; i
--)
328 if (field_is_static (&type
->field (i
)))
330 gen_static_field (ax
, &value
, type
, i
);
331 if (value
.optimized_out
)
335 case axs_lvalue_memory
:
337 /* Initialize the TYPE_LENGTH if it is a typedef. */
338 check_typedef (value
.type
);
339 ax_const_l (ax
, TYPE_LENGTH (value
.type
));
340 ax_simple (ax
, aop_trace
);
344 case axs_lvalue_register
:
345 /* We don't actually need the register's value to be pushed,
346 just note that we need it to be collected. */
347 ax_reg_mask (ax
, value
.u
.reg
);
355 /* Now scan through base classes recursively. */
356 for (i
= 0; i
< nbases
; i
++)
358 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
360 gen_trace_static_fields (ax
, basetype
);
364 /* Trace the lvalue on the stack, if it needs it. In either case, pop
365 the value. Useful on the left side of a comma, and at the end of
366 an expression being used for tracing. */
368 gen_traced_pop (struct agent_expr
*ax
, struct axs_value
*value
)
370 int string_trace
= 0;
372 && value
->type
->code () == TYPE_CODE_PTR
373 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value
->type
)),
383 ax_const_l (ax
, ax
->trace_string
);
384 ax_simple (ax
, aop_tracenz
);
387 /* We don't trace rvalues, just the lvalues necessary to
388 produce them. So just dispose of this value. */
389 ax_simple (ax
, aop_pop
);
392 case axs_lvalue_memory
:
394 /* Initialize the TYPE_LENGTH if it is a typedef. */
395 check_typedef (value
->type
);
399 gen_fetch (ax
, value
->type
);
400 ax_const_l (ax
, ax
->trace_string
);
401 ax_simple (ax
, aop_tracenz
);
405 /* There's no point in trying to use a trace_quick bytecode
406 here, since "trace_quick SIZE pop" is three bytes, whereas
407 "const8 SIZE trace" is also three bytes, does the same
408 thing, and the simplest code which generates that will also
409 work correctly for objects with large sizes. */
410 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
411 ax_simple (ax
, aop_trace
);
416 case axs_lvalue_register
:
417 /* We don't actually need the register's value to be on the
418 stack, and the target will get heartburn if the register is
419 larger than will fit in a stack, so just mark it for
420 collection and be done with it. */
421 ax_reg_mask (ax
, value
->u
.reg
);
423 /* But if the register points to a string, assume the value
424 will fit on the stack and push it anyway. */
427 ax_reg (ax
, value
->u
.reg
);
428 ax_const_l (ax
, ax
->trace_string
);
429 ax_simple (ax
, aop_tracenz
);
434 /* If we're not tracing, just pop the value. */
435 ax_simple (ax
, aop_pop
);
437 /* To trace C++ classes with static fields stored elsewhere. */
439 && (value
->type
->code () == TYPE_CODE_STRUCT
440 || value
->type
->code () == TYPE_CODE_UNION
))
441 gen_trace_static_fields (ax
, value
->type
);
446 /* Generating bytecode from GDB expressions: helper functions */
448 /* Assume that the lower bits of the top of the stack is a value of
449 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
451 gen_sign_extend (struct agent_expr
*ax
, struct type
*type
)
453 /* Do we need to sign-extend this? */
454 if (!type
->is_unsigned ())
455 ax_ext (ax
, TYPE_LENGTH (type
) * TARGET_CHAR_BIT
);
459 /* Assume the lower bits of the top of the stack hold a value of type
460 TYPE, and the upper bits are garbage. Sign-extend or truncate as
463 gen_extend (struct agent_expr
*ax
, struct type
*type
)
465 int bits
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
468 ((type
->is_unsigned () ? ax_zero_ext
: ax_ext
) (ax
, bits
));
472 /* Assume that the top of the stack contains a value of type "pointer
473 to TYPE"; generate code to fetch its value. Note that TYPE is the
474 target type, not the pointer type. */
476 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
480 /* Record the area of memory we're about to fetch. */
481 ax_trace_quick (ax
, TYPE_LENGTH (type
));
484 if (type
->code () == TYPE_CODE_RANGE
)
485 type
= TYPE_TARGET_TYPE (type
);
487 switch (type
->code ())
491 case TYPE_CODE_RVALUE_REF
:
496 /* It's a scalar value, so we know how to dereference it. How
497 many bytes long is it? */
498 switch (TYPE_LENGTH (type
))
500 case 8 / TARGET_CHAR_BIT
:
501 ax_simple (ax
, aop_ref8
);
503 case 16 / TARGET_CHAR_BIT
:
504 ax_simple (ax
, aop_ref16
);
506 case 32 / TARGET_CHAR_BIT
:
507 ax_simple (ax
, aop_ref32
);
509 case 64 / TARGET_CHAR_BIT
:
510 ax_simple (ax
, aop_ref64
);
513 /* Either our caller shouldn't have asked us to dereference
514 that pointer (other code's fault), or we're not
515 implementing something we should be (this code's fault).
516 In any case, it's a bug the user shouldn't see. */
518 internal_error (__FILE__
, __LINE__
,
519 _("gen_fetch: strange size"));
522 gen_sign_extend (ax
, type
);
526 /* Our caller requested us to dereference a pointer from an unsupported
527 type. Error out and give callers a chance to handle the failure
529 error (_("gen_fetch: Unsupported type code `%s'."),
535 /* Generate code to left shift the top of the stack by DISTANCE bits, or
536 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
537 unsigned (logical) right shifts. */
539 gen_left_shift (struct agent_expr
*ax
, int distance
)
543 ax_const_l (ax
, distance
);
544 ax_simple (ax
, aop_lsh
);
546 else if (distance
< 0)
548 ax_const_l (ax
, -distance
);
549 ax_simple (ax
, aop_rsh_unsigned
);
555 /* Generating bytecode from GDB expressions: symbol references */
557 /* Generate code to push the base address of the argument portion of
558 the top stack frame. */
560 gen_frame_args_address (struct agent_expr
*ax
)
563 LONGEST frame_offset
;
565 gdbarch_virtual_frame_pointer (ax
->gdbarch
,
566 ax
->scope
, &frame_reg
, &frame_offset
);
567 ax_reg (ax
, frame_reg
);
568 gen_offset (ax
, frame_offset
);
572 /* Generate code to push the base address of the locals portion of the
575 gen_frame_locals_address (struct agent_expr
*ax
)
578 LONGEST frame_offset
;
580 gdbarch_virtual_frame_pointer (ax
->gdbarch
,
581 ax
->scope
, &frame_reg
, &frame_offset
);
582 ax_reg (ax
, frame_reg
);
583 gen_offset (ax
, frame_offset
);
587 /* Generate code to add OFFSET to the top of the stack. Try to
588 generate short and readable code. We use this for getting to
589 variables on the stack, and structure members. If we were
590 programming in ML, it would be clearer why these are the same
593 gen_offset (struct agent_expr
*ax
, int offset
)
595 /* It would suffice to simply push the offset and add it, but this
596 makes it easier to read positive and negative offsets in the
600 ax_const_l (ax
, offset
);
601 ax_simple (ax
, aop_add
);
605 ax_const_l (ax
, -offset
);
606 ax_simple (ax
, aop_sub
);
611 /* In many cases, a symbol's value is the offset from some other
612 address (stack frame, base register, etc.) Generate code to add
613 VAR's value to the top of the stack. */
615 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
617 gen_offset (ax
, SYMBOL_VALUE (var
));
621 /* Generate code for a variable reference to AX. The variable is the
622 symbol VAR. Set VALUE to describe the result. */
625 gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
, struct symbol
*var
)
627 /* Dereference any typedefs. */
628 value
->type
= check_typedef (SYMBOL_TYPE (var
));
629 value
->optimized_out
= 0;
631 if (SYMBOL_COMPUTED_OPS (var
) != NULL
)
633 SYMBOL_COMPUTED_OPS (var
)->tracepoint_var_ref (var
, ax
, value
);
637 /* I'm imitating the code in read_var_value. */
638 switch (SYMBOL_CLASS (var
))
640 case LOC_CONST
: /* A constant, like an enum value. */
641 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE (var
));
642 value
->kind
= axs_rvalue
;
645 case LOC_LABEL
: /* A goto label, being used as a value. */
646 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
647 value
->kind
= axs_rvalue
;
650 case LOC_CONST_BYTES
:
651 internal_error (__FILE__
, __LINE__
,
652 _("gen_var_ref: LOC_CONST_BYTES "
653 "symbols are not supported"));
655 /* Variable at a fixed location in memory. Easy. */
657 /* Push the address of the variable. */
658 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (var
));
659 value
->kind
= axs_lvalue_memory
;
662 case LOC_ARG
: /* var lives in argument area of frame */
663 gen_frame_args_address (ax
);
664 gen_sym_offset (ax
, var
);
665 value
->kind
= axs_lvalue_memory
;
668 case LOC_REF_ARG
: /* As above, but the frame slot really
669 holds the address of the variable. */
670 gen_frame_args_address (ax
);
671 gen_sym_offset (ax
, var
);
672 /* Don't assume any particular pointer size. */
673 gen_fetch (ax
, builtin_type (ax
->gdbarch
)->builtin_data_ptr
);
674 value
->kind
= axs_lvalue_memory
;
677 case LOC_LOCAL
: /* var lives in locals area of frame */
678 gen_frame_locals_address (ax
);
679 gen_sym_offset (ax
, var
);
680 value
->kind
= axs_lvalue_memory
;
684 error (_("Cannot compute value of typedef `%s'."),
689 ax_const_l (ax
, BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (var
)));
690 value
->kind
= axs_rvalue
;
694 /* Don't generate any code at all; in the process of treating
695 this as an lvalue or rvalue, the caller will generate the
697 value
->kind
= axs_lvalue_register
;
699 = SYMBOL_REGISTER_OPS (var
)->register_number (var
, ax
->gdbarch
);
702 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
703 register, not on the stack. Simpler than LOC_REGISTER
704 because it's just like any other case where the thing
705 has a real address. */
706 case LOC_REGPARM_ADDR
:
708 SYMBOL_REGISTER_OPS (var
)->register_number (var
, ax
->gdbarch
));
709 value
->kind
= axs_lvalue_memory
;
714 struct bound_minimal_symbol msym
715 = lookup_minimal_symbol (var
->linkage_name (), NULL
, NULL
);
718 error (_("Couldn't resolve symbol `%s'."), var
->print_name ());
720 /* Push the address of the variable. */
721 ax_const_l (ax
, BMSYMBOL_VALUE_ADDRESS (msym
));
722 value
->kind
= axs_lvalue_memory
;
727 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
729 case LOC_OPTIMIZED_OUT
:
730 /* Flag this, but don't say anything; leave it up to callers to
732 value
->optimized_out
= 1;
736 error (_("Cannot find value of botched symbol `%s'."),
742 /* Generate code for a minimal symbol variable reference to AX. The
743 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
747 gen_msym_var_ref (agent_expr
*ax
, axs_value
*value
,
748 minimal_symbol
*msymbol
, objfile
*objf
)
751 type
*t
= find_minsym_type_and_address (msymbol
, objf
, &address
);
753 value
->optimized_out
= false;
754 ax_const_l (ax
, address
);
755 value
->kind
= axs_lvalue_memory
;
761 /* Generating bytecode from GDB expressions: literals */
764 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
768 value
->kind
= axs_rvalue
;
769 value
->type
= check_typedef (type
);
774 /* Generating bytecode from GDB expressions: unary conversions, casts */
776 /* Take what's on the top of the stack (as described by VALUE), and
777 try to make an rvalue out of it. Signal an error if we can't do
780 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
782 /* Only deal with scalars, structs and such may be too large
783 to fit in a stack entry. */
784 value
->type
= check_typedef (value
->type
);
785 if (value
->type
->code () == TYPE_CODE_ARRAY
786 || value
->type
->code () == TYPE_CODE_STRUCT
787 || value
->type
->code () == TYPE_CODE_UNION
788 || value
->type
->code () == TYPE_CODE_FUNC
)
789 error (_("Value not scalar: cannot be an rvalue."));
794 /* It's already an rvalue. */
797 case axs_lvalue_memory
:
798 /* The top of stack is the address of the object. Dereference. */
799 gen_fetch (ax
, value
->type
);
802 case axs_lvalue_register
:
803 /* There's nothing on the stack, but value->u.reg is the
804 register number containing the value.
806 When we add floating-point support, this is going to have to
807 change. What about SPARC register pairs, for example? */
808 ax_reg (ax
, value
->u
.reg
);
809 gen_extend (ax
, value
->type
);
813 value
->kind
= axs_rvalue
;
817 /* Assume the top of the stack is described by VALUE, and perform the
818 usual unary conversions. This is motivated by ANSI 6.2.2, but of
819 course GDB expressions are not ANSI; they're the mishmash union of
820 a bunch of languages. Rah.
822 NOTE! This function promises to produce an rvalue only when the
823 incoming value is of an appropriate type. In other words, the
824 consumer of the value this function produces may assume the value
825 is an rvalue only after checking its type.
827 The immediate issue is that if the user tries to use a structure or
828 union as an operand of, say, the `+' operator, we don't want to try
829 to convert that structure to an rvalue; require_rvalue will bomb on
830 structs and unions. Rather, we want to simply pass the struct
831 lvalue through unchanged, and let `+' raise an error. */
834 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
836 /* We don't have to generate any code for the usual integral
837 conversions, since values are always represented as full-width on
838 the stack. Should we tweak the type? */
840 /* Some types require special handling. */
841 switch (value
->type
->code ())
843 /* Functions get converted to a pointer to the function. */
845 value
->type
= lookup_pointer_type (value
->type
);
846 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
849 /* Arrays get converted to a pointer to their first element, and
850 are no longer an lvalue. */
851 case TYPE_CODE_ARRAY
:
853 struct type
*elements
= TYPE_TARGET_TYPE (value
->type
);
855 value
->type
= lookup_pointer_type (elements
);
856 value
->kind
= axs_rvalue
;
857 /* We don't need to generate any code; the address of the array
858 is also the address of its first element. */
862 /* Don't try to convert structures and unions to rvalues. Let the
863 consumer signal an error. */
864 case TYPE_CODE_STRUCT
:
865 case TYPE_CODE_UNION
:
869 /* If the value is an lvalue, dereference it. */
870 require_rvalue (ax
, value
);
874 /* Return non-zero iff the type TYPE1 is considered "wider" than the
875 type TYPE2, according to the rules described in gen_usual_arithmetic. */
877 type_wider_than (struct type
*type1
, struct type
*type2
)
879 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)
880 || (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
881 && type1
->is_unsigned ()
882 && !type2
->is_unsigned ()));
886 /* Return the "wider" of the two types TYPE1 and TYPE2. */
888 max_type (struct type
*type1
, struct type
*type2
)
890 return type_wider_than (type1
, type2
) ? type1
: type2
;
894 /* Generate code to convert a scalar value of type FROM to type TO. */
896 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
898 /* Perhaps there is a more graceful way to state these rules. */
900 /* If we're converting to a narrower type, then we need to clear out
902 if (TYPE_LENGTH (to
) < TYPE_LENGTH (from
))
905 /* If the two values have equal width, but different signednesses,
906 then we need to extend. */
907 else if (TYPE_LENGTH (to
) == TYPE_LENGTH (from
))
909 if (from
->is_unsigned () != to
->is_unsigned ())
913 /* If we're converting to a wider type, and becoming unsigned, then
914 we need to zero out any possible sign bits. */
915 else if (TYPE_LENGTH (to
) > TYPE_LENGTH (from
))
917 if (to
->is_unsigned ())
923 /* Return non-zero iff the type FROM will require any bytecodes to be
924 emitted to be converted to the type TO. */
926 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
928 agent_expr_up
ax (new agent_expr (NULL
, 0));
931 /* Actually generate the code, and see if anything came out. At the
932 moment, it would be trivial to replicate the code in
933 gen_conversion here, but in the future, when we're supporting
934 floating point and the like, it may not be. Doing things this
935 way allows this function to be independent of the logic in
937 gen_conversion (ax
.get (), from
, to
);
938 nontrivial
= ax
->len
> 0;
943 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
944 6.2.1.5) for the two operands of an arithmetic operator. This
945 effectively finds a "least upper bound" type for the two arguments,
946 and promotes each argument to that type. *VALUE1 and *VALUE2
947 describe the values as they are passed in, and as they are left. */
949 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
950 struct axs_value
*value2
)
952 /* Do the usual binary conversions. */
953 if (value1
->type
->code () == TYPE_CODE_INT
954 && value2
->type
->code () == TYPE_CODE_INT
)
956 /* The ANSI integral promotions seem to work this way: Order the
957 integer types by size, and then by signedness: an n-bit
958 unsigned type is considered "wider" than an n-bit signed
959 type. Promote to the "wider" of the two types, and always
960 promote at least to int. */
961 struct type
*target
= max_type (builtin_type (ax
->gdbarch
)->builtin_int
,
962 max_type (value1
->type
, value2
->type
));
964 /* Deal with value2, on the top of the stack. */
965 gen_conversion (ax
, value2
->type
, target
);
967 /* Deal with value1, not on the top of the stack. Don't
968 generate the `swap' instructions if we're not actually going
970 if (is_nontrivial_conversion (value1
->type
, target
))
972 ax_simple (ax
, aop_swap
);
973 gen_conversion (ax
, value1
->type
, target
);
974 ax_simple (ax
, aop_swap
);
977 value1
->type
= value2
->type
= check_typedef (target
);
982 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
983 the value on the top of the stack, as described by VALUE. Assume
984 the value has integral type. */
986 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
988 const struct builtin_type
*builtin
= builtin_type (ax
->gdbarch
);
990 if (!type_wider_than (value
->type
, builtin
->builtin_int
))
992 gen_conversion (ax
, value
->type
, builtin
->builtin_int
);
993 value
->type
= builtin
->builtin_int
;
995 else if (!type_wider_than (value
->type
, builtin
->builtin_unsigned_int
))
997 gen_conversion (ax
, value
->type
, builtin
->builtin_unsigned_int
);
998 value
->type
= builtin
->builtin_unsigned_int
;
1003 /* Generate code for a cast to TYPE. */
1005 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
1007 /* GCC does allow casts to yield lvalues, so this should be fixed
1008 before merging these changes into the trunk. */
1009 require_rvalue (ax
, value
);
1010 /* Dereference typedefs. */
1011 type
= check_typedef (type
);
1013 switch (type
->code ())
1017 case TYPE_CODE_RVALUE_REF
:
1018 /* It's implementation-defined, and I'll bet this is what GCC
1022 case TYPE_CODE_ARRAY
:
1023 case TYPE_CODE_STRUCT
:
1024 case TYPE_CODE_UNION
:
1025 case TYPE_CODE_FUNC
:
1026 error (_("Invalid type cast: intended type must be scalar."));
1028 case TYPE_CODE_ENUM
:
1029 case TYPE_CODE_BOOL
:
1030 /* We don't have to worry about the size of the value, because
1031 all our integral values are fully sign-extended, and when
1032 casting pointers we can do anything we like. Is there any
1033 way for us to know what GCC actually does with a cast like
1038 gen_conversion (ax
, value
->type
, type
);
1041 case TYPE_CODE_VOID
:
1042 /* We could pop the value, and rely on everyone else to check
1043 the type and notice that this value doesn't occupy a stack
1044 slot. But for now, leave the value on the stack, and
1045 preserve the "value == stack element" assumption. */
1049 error (_("Casts to requested type are not yet implemented."));
1057 /* Generating bytecode from GDB expressions: arithmetic */
1059 /* Scale the integer on the top of the stack by the size of the target
1060 of the pointer type TYPE. */
1062 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
1064 struct type
*element
= TYPE_TARGET_TYPE (type
);
1066 if (TYPE_LENGTH (element
) != 1)
1068 ax_const_l (ax
, TYPE_LENGTH (element
));
1074 /* Generate code for pointer arithmetic PTR + INT. */
1076 gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
1077 struct axs_value
*value1
, struct axs_value
*value2
)
1079 gdb_assert (pointer_type (value1
->type
));
1080 gdb_assert (value2
->type
->code () == TYPE_CODE_INT
);
1082 gen_scale (ax
, aop_mul
, value1
->type
);
1083 ax_simple (ax
, aop_add
);
1084 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1085 value
->type
= value1
->type
;
1086 value
->kind
= axs_rvalue
;
1090 /* Generate code for pointer arithmetic PTR - INT. */
1092 gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
1093 struct axs_value
*value1
, struct axs_value
*value2
)
1095 gdb_assert (pointer_type (value1
->type
));
1096 gdb_assert (value2
->type
->code () == TYPE_CODE_INT
);
1098 gen_scale (ax
, aop_mul
, value1
->type
);
1099 ax_simple (ax
, aop_sub
);
1100 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1101 value
->type
= value1
->type
;
1102 value
->kind
= axs_rvalue
;
1106 /* Generate code for pointer arithmetic PTR - PTR. */
1108 gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
1109 struct axs_value
*value1
, struct axs_value
*value2
,
1110 struct type
*result_type
)
1112 gdb_assert (pointer_type (value1
->type
));
1113 gdb_assert (pointer_type (value2
->type
));
1115 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1
->type
))
1116 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2
->type
)))
1118 First argument of `-' is a pointer, but second argument is neither\n\
1119 an integer nor a pointer of the same type."));
1121 ax_simple (ax
, aop_sub
);
1122 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1123 value
->type
= result_type
;
1124 value
->kind
= axs_rvalue
;
1128 gen_equal (struct agent_expr
*ax
, struct axs_value
*value
,
1129 struct axs_value
*value1
, struct axs_value
*value2
,
1130 struct type
*result_type
)
1132 if (pointer_type (value1
->type
) || pointer_type (value2
->type
))
1133 ax_simple (ax
, aop_equal
);
1135 gen_binop (ax
, value
, value1
, value2
,
1136 aop_equal
, aop_equal
, 0, "equal");
1137 value
->type
= result_type
;
1138 value
->kind
= axs_rvalue
;
1142 gen_less (struct agent_expr
*ax
, struct axs_value
*value
,
1143 struct axs_value
*value1
, struct axs_value
*value2
,
1144 struct type
*result_type
)
1146 if (pointer_type (value1
->type
) || pointer_type (value2
->type
))
1147 ax_simple (ax
, aop_less_unsigned
);
1149 gen_binop (ax
, value
, value1
, value2
,
1150 aop_less_signed
, aop_less_unsigned
, 0, "less than");
1151 value
->type
= result_type
;
1152 value
->kind
= axs_rvalue
;
1155 /* Generate code for a binary operator that doesn't do pointer magic.
1156 We set VALUE to describe the result value; we assume VALUE1 and
1157 VALUE2 describe the two operands, and that they've undergone the
1158 usual binary conversions. MAY_CARRY should be non-zero iff the
1159 result needs to be extended. NAME is the English name of the
1160 operator, used in error messages */
1162 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1163 struct axs_value
*value1
, struct axs_value
*value2
,
1164 enum agent_op op
, enum agent_op op_unsigned
,
1165 int may_carry
, const char *name
)
1167 /* We only handle INT op INT. */
1168 if ((value1
->type
->code () != TYPE_CODE_INT
)
1169 || (value2
->type
->code () != TYPE_CODE_INT
))
1170 error (_("Invalid combination of types in %s."), name
);
1172 ax_simple (ax
, value1
->type
->is_unsigned () ? op_unsigned
: op
);
1174 gen_extend (ax
, value1
->type
); /* catch overflow */
1175 value
->type
= value1
->type
;
1176 value
->kind
= axs_rvalue
;
1181 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
1182 struct type
*result_type
)
1184 if (value
->type
->code () != TYPE_CODE_INT
1185 && value
->type
->code () != TYPE_CODE_PTR
)
1186 error (_("Invalid type of operand to `!'."));
1188 ax_simple (ax
, aop_log_not
);
1189 value
->type
= result_type
;
1194 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1196 if (value
->type
->code () != TYPE_CODE_INT
)
1197 error (_("Invalid type of operand to `~'."));
1199 ax_simple (ax
, aop_bit_not
);
1200 gen_extend (ax
, value
->type
);
1205 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1207 /* Dereference the value on the top of the stack. */
1209 gen_deref (struct axs_value
*value
)
1211 /* The caller should check the type, because several operators use
1212 this, and we don't know what error message to generate. */
1213 if (!pointer_type (value
->type
))
1214 internal_error (__FILE__
, __LINE__
,
1215 _("gen_deref: expected a pointer"));
1217 /* We've got an rvalue now, which is a pointer. We want to yield an
1218 lvalue, whose address is exactly that pointer. So we don't
1219 actually emit any code; we just change the type from "Pointer to
1220 T" to "T", and mark the value as an lvalue in memory. Leave it
1221 to the consumer to actually dereference it. */
1222 value
->type
= check_typedef (TYPE_TARGET_TYPE (value
->type
));
1223 if (value
->type
->code () == TYPE_CODE_VOID
)
1224 error (_("Attempt to dereference a generic pointer."));
1225 value
->kind
= ((value
->type
->code () == TYPE_CODE_FUNC
)
1226 ? axs_rvalue
: axs_lvalue_memory
);
1230 /* Produce the address of the lvalue on the top of the stack. */
1232 gen_address_of (struct axs_value
*value
)
1234 /* Special case for taking the address of a function. The ANSI
1235 standard describes this as a special case, too, so this
1236 arrangement is not without motivation. */
1237 if (value
->type
->code () == TYPE_CODE_FUNC
)
1238 /* The value's already an rvalue on the stack, so we just need to
1240 value
->type
= lookup_pointer_type (value
->type
);
1242 switch (value
->kind
)
1245 error (_("Operand of `&' is an rvalue, which has no address."));
1247 case axs_lvalue_register
:
1248 error (_("Operand of `&' is in a register, and has no address."));
1250 case axs_lvalue_memory
:
1251 value
->kind
= axs_rvalue
;
1252 value
->type
= lookup_pointer_type (value
->type
);
1257 /* Generate code to push the value of a bitfield of a structure whose
1258 address is on the top of the stack. START and END give the
1259 starting and one-past-ending *bit* numbers of the field within the
1262 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1263 struct type
*type
, int start
, int end
)
1265 /* Note that ops[i] fetches 8 << i bits. */
1266 static enum agent_op ops
[]
1267 = {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1268 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1270 /* We don't want to touch any byte that the bitfield doesn't
1271 actually occupy; we shouldn't make any accesses we're not
1272 explicitly permitted to. We rely here on the fact that the
1273 bytecode `ref' operators work on unaligned addresses.
1275 It takes some fancy footwork to get the stack to work the way
1276 we'd like. Say we're retrieving a bitfield that requires three
1277 fetches. Initially, the stack just contains the address:
1279 For the first fetch, we duplicate the address
1281 then add the byte offset, do the fetch, and shift and mask as
1282 needed, yielding a fragment of the value, properly aligned for
1283 the final bitwise or:
1285 then we swap, and repeat the process:
1286 frag1 addr --- address on top
1287 frag1 addr addr --- duplicate it
1288 frag1 addr frag2 --- get second fragment
1289 frag1 frag2 addr --- swap again
1290 frag1 frag2 frag3 --- get third fragment
1291 Notice that, since the third fragment is the last one, we don't
1292 bother duplicating the address this time. Now we have all the
1293 fragments on the stack, and we can simply `or' them together,
1294 yielding the final value of the bitfield. */
1296 /* The first and one-after-last bits in the field, but rounded down
1297 and up to byte boundaries. */
1298 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1299 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1303 /* current bit offset within the structure */
1306 /* The index in ops of the opcode we're considering. */
1309 /* The number of fragments we generated in the process. Probably
1310 equal to the number of `one' bits in bytesize, but who cares? */
1313 /* Dereference any typedefs. */
1314 type
= check_typedef (type
);
1316 /* Can we fetch the number of bits requested at all? */
1317 if ((end
- start
) > ((1 << num_ops
) * 8))
1318 internal_error (__FILE__
, __LINE__
,
1319 _("gen_bitfield_ref: bitfield too wide"));
1321 /* Note that we know here that we only need to try each opcode once.
1322 That may not be true on machines with weird byte sizes. */
1323 offset
= bound_start
;
1325 for (op
= num_ops
- 1; op
>= 0; op
--)
1327 /* number of bits that ops[op] would fetch */
1328 int op_size
= 8 << op
;
1330 /* The stack at this point, from bottom to top, contains zero or
1331 more fragments, then the address. */
1333 /* Does this fetch fit within the bitfield? */
1334 if (offset
+ op_size
<= bound_end
)
1336 /* Is this the last fragment? */
1337 int last_frag
= (offset
+ op_size
== bound_end
);
1340 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1342 /* Add the offset. */
1343 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1347 /* Record the area of memory we're about to fetch. */
1348 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1351 /* Perform the fetch. */
1352 ax_simple (ax
, ops
[op
]);
1354 /* Shift the bits we have to their proper position.
1355 gen_left_shift will generate right shifts when the operand
1358 A big-endian field diagram to ponder:
1359 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1360 +------++------++------++------++------++------++------++------+
1361 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1363 bit number 16 32 48 53
1364 These are bit numbers as supplied by GDB. Note that the
1365 bit numbers run from right to left once you've fetched the
1368 A little-endian field diagram to ponder:
1369 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1370 +------++------++------++------++------++------++------++------+
1371 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1373 bit number 48 32 16 4 0
1375 In both cases, the most significant end is on the left
1376 (i.e. normal numeric writing order), which means that you
1377 don't go crazy thinking about `left' and `right' shifts.
1379 We don't have to worry about masking yet:
1380 - If they contain garbage off the least significant end, then we
1381 must be looking at the low end of the field, and the right
1382 shift will wipe them out.
1383 - If they contain garbage off the most significant end, then we
1384 must be looking at the most significant end of the word, and
1385 the sign/zero extension will wipe them out.
1386 - If we're in the interior of the word, then there is no garbage
1387 on either end, because the ref operators zero-extend. */
1388 if (gdbarch_byte_order (ax
->gdbarch
) == BFD_ENDIAN_BIG
)
1389 gen_left_shift (ax
, end
- (offset
+ op_size
));
1391 gen_left_shift (ax
, offset
- start
);
1394 /* Bring the copy of the address up to the top. */
1395 ax_simple (ax
, aop_swap
);
1402 /* Generate enough bitwise `or' operations to combine all the
1403 fragments we left on the stack. */
1404 while (fragment_count
-- > 1)
1405 ax_simple (ax
, aop_bit_or
);
1407 /* Sign- or zero-extend the value as appropriate. */
1408 ((type
->is_unsigned () ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1410 /* This is *not* an lvalue. Ugh. */
1411 value
->kind
= axs_rvalue
;
1415 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1416 is an accumulated offset (in bytes), will be nonzero for objects
1417 embedded in other objects, like C++ base classes. Behavior should
1418 generally follow value_primitive_field. */
1421 gen_primitive_field (struct agent_expr
*ax
, struct axs_value
*value
,
1422 int offset
, int fieldno
, struct type
*type
)
1424 /* Is this a bitfield? */
1425 if (TYPE_FIELD_PACKED (type
, fieldno
))
1426 gen_bitfield_ref (ax
, value
, type
->field (fieldno
).type (),
1427 (offset
* TARGET_CHAR_BIT
1428 + TYPE_FIELD_BITPOS (type
, fieldno
)),
1429 (offset
* TARGET_CHAR_BIT
1430 + TYPE_FIELD_BITPOS (type
, fieldno
)
1431 + TYPE_FIELD_BITSIZE (type
, fieldno
)));
1434 gen_offset (ax
, offset
1435 + TYPE_FIELD_BITPOS (type
, fieldno
) / TARGET_CHAR_BIT
);
1436 value
->kind
= axs_lvalue_memory
;
1437 value
->type
= type
->field (fieldno
).type ();
1441 /* Search for the given field in either the given type or one of its
1442 base classes. Return 1 if found, 0 if not. */
1445 gen_struct_ref_recursive (struct agent_expr
*ax
, struct axs_value
*value
,
1446 const char *field
, int offset
, struct type
*type
)
1449 int nbases
= TYPE_N_BASECLASSES (type
);
1451 type
= check_typedef (type
);
1453 for (i
= type
->num_fields () - 1; i
>= nbases
; i
--)
1455 const char *this_name
= TYPE_FIELD_NAME (type
, i
);
1459 if (strcmp (field
, this_name
) == 0)
1461 /* Note that bytecodes for the struct's base (aka
1462 "this") will have been generated already, which will
1463 be unnecessary but not harmful if the static field is
1464 being handled as a global. */
1465 if (field_is_static (&type
->field (i
)))
1467 gen_static_field (ax
, value
, type
, i
);
1468 if (value
->optimized_out
)
1469 error (_("static field `%s' has been "
1470 "optimized out, cannot use"),
1475 gen_primitive_field (ax
, value
, offset
, i
, type
);
1478 #if 0 /* is this right? */
1479 if (this_name
[0] == '\0')
1480 internal_error (__FILE__
, __LINE__
,
1481 _("find_field: anonymous unions not supported"));
1486 /* Now scan through base classes recursively. */
1487 for (i
= 0; i
< nbases
; i
++)
1489 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1491 rslt
= gen_struct_ref_recursive (ax
, value
, field
,
1492 offset
+ TYPE_BASECLASS_BITPOS (type
, i
)
1499 /* Not found anywhere, flag so caller can complain. */
1503 /* Generate code to reference the member named FIELD of a structure or
1504 union. The top of the stack, as described by VALUE, should have
1505 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1506 the operator being compiled, and OPERAND_NAME is the kind of thing
1507 it operates on; we use them in error messages. */
1509 gen_struct_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1510 const char *field
, const char *operator_name
,
1511 const char *operand_name
)
1516 /* Follow pointers until we reach a non-pointer. These aren't the C
1517 semantics, but they're what the normal GDB evaluator does, so we
1518 should at least be consistent. */
1519 while (pointer_type (value
->type
))
1521 require_rvalue (ax
, value
);
1524 type
= check_typedef (value
->type
);
1526 /* This must yield a structure or a union. */
1527 if (type
->code () != TYPE_CODE_STRUCT
1528 && type
->code () != TYPE_CODE_UNION
)
1529 error (_("The left operand of `%s' is not a %s."),
1530 operator_name
, operand_name
);
1532 /* And it must be in memory; we don't deal with structure rvalues,
1533 or structures living in registers. */
1534 if (value
->kind
!= axs_lvalue_memory
)
1535 error (_("Structure does not live in memory."));
1537 /* Search through fields and base classes recursively. */
1538 found
= gen_struct_ref_recursive (ax
, value
, field
, 0, type
);
1541 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1542 field
, type
->name ());
1546 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1547 const struct type
*curtype
, const char *name
);
1549 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1550 const struct type
*curtype
, const char *name
);
1553 gen_static_field (struct agent_expr
*ax
, struct axs_value
*value
,
1554 struct type
*type
, int fieldno
)
1556 if (TYPE_FIELD_LOC_KIND (type
, fieldno
) == FIELD_LOC_KIND_PHYSADDR
)
1558 ax_const_l (ax
, TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
1559 value
->kind
= axs_lvalue_memory
;
1560 value
->type
= type
->field (fieldno
).type ();
1561 value
->optimized_out
= 0;
1565 const char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1566 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0).symbol
;
1570 gen_var_ref (ax
, value
, sym
);
1572 /* Don't error if the value was optimized out, we may be
1573 scanning all static fields and just want to pass over this
1574 and continue with the rest. */
1578 /* Silently assume this was optimized out; class printing
1579 will let the user know why the data is missing. */
1580 value
->optimized_out
= 1;
1586 gen_struct_elt_for_reference (struct agent_expr
*ax
, struct axs_value
*value
,
1587 struct type
*type
, const char *fieldname
)
1589 struct type
*t
= type
;
1592 if (t
->code () != TYPE_CODE_STRUCT
1593 && t
->code () != TYPE_CODE_UNION
)
1594 internal_error (__FILE__
, __LINE__
,
1595 _("non-aggregate type to gen_struct_elt_for_reference"));
1597 for (i
= t
->num_fields () - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1599 const char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1601 if (t_field_name
&& strcmp (t_field_name
, fieldname
) == 0)
1603 if (field_is_static (&t
->field (i
)))
1605 gen_static_field (ax
, value
, t
, i
);
1606 if (value
->optimized_out
)
1607 error (_("static field `%s' has been "
1608 "optimized out, cannot use"),
1612 if (TYPE_FIELD_PACKED (t
, i
))
1613 error (_("pointers to bitfield members not allowed"));
1615 /* FIXME we need a way to do "want_address" equivalent */
1617 error (_("Cannot reference non-static field \"%s\""), fieldname
);
1621 /* FIXME add other scoped-reference cases here */
1623 /* Do a last-ditch lookup. */
1624 return gen_maybe_namespace_elt (ax
, value
, type
, fieldname
);
1627 /* C++: Return the member NAME of the namespace given by the type
1631 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1632 const struct type
*curtype
, const char *name
)
1634 int found
= gen_maybe_namespace_elt (ax
, value
, curtype
, name
);
1637 error (_("No symbol \"%s\" in namespace \"%s\"."),
1638 name
, curtype
->name ());
1643 /* A helper function used by value_namespace_elt and
1644 value_struct_elt_for_reference. It looks up NAME inside the
1645 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1646 is a class and NAME refers to a type in CURTYPE itself (as opposed
1647 to, say, some base class of CURTYPE). */
1650 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1651 const struct type
*curtype
, const char *name
)
1653 const char *namespace_name
= curtype
->name ();
1654 struct block_symbol sym
;
1656 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
1657 block_for_pc (ax
->scope
),
1660 if (sym
.symbol
== NULL
)
1663 gen_var_ref (ax
, value
, sym
.symbol
);
1665 if (value
->optimized_out
)
1666 error (_("`%s' has been optimized out, cannot use"),
1667 sym
.symbol
->print_name ());
1674 gen_aggregate_elt_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1675 struct type
*type
, const char *field
)
1677 switch (type
->code ())
1679 case TYPE_CODE_STRUCT
:
1680 case TYPE_CODE_UNION
:
1681 return gen_struct_elt_for_reference (ax
, value
, type
, field
);
1683 case TYPE_CODE_NAMESPACE
:
1684 return gen_namespace_elt (ax
, value
, type
, field
);
1687 internal_error (__FILE__
, __LINE__
,
1688 _("non-aggregate type in gen_aggregate_elt_ref"));
1694 /* Generate code for GDB's magical `repeat' operator.
1695 LVALUE @ INT creates an array INT elements long, and whose elements
1696 have the same type as LVALUE, located in memory so that LVALUE is
1697 its first element. For example, argv[0]@argc gives you the array
1698 of command-line arguments.
1700 Unfortunately, because we have to know the types before we actually
1701 have a value for the expression, we can't implement this perfectly
1702 without changing the type system, having values that occupy two
1703 stack slots, doing weird things with sizeof, etc. So we require
1704 the right operand to be a constant expression. */
1706 gen_repeat (struct expression
*exp
, union exp_element
**pc
,
1707 struct agent_expr
*ax
, struct axs_value
*value
)
1709 struct axs_value value1
;
1711 /* We don't want to turn this into an rvalue, so no conversions
1713 gen_expr (exp
, pc
, ax
, &value1
);
1714 if (value1
.kind
!= axs_lvalue_memory
)
1715 error (_("Left operand of `@' must be an object in memory."));
1717 /* Evaluate the length; it had better be a constant. */
1719 struct value
*v
= const_expr (pc
);
1723 error (_("Right operand of `@' must be a "
1724 "constant, in agent expressions."));
1725 if (value_type (v
)->code () != TYPE_CODE_INT
)
1726 error (_("Right operand of `@' must be an integer."));
1727 length
= value_as_long (v
);
1729 error (_("Right operand of `@' must be positive."));
1731 /* The top of the stack is already the address of the object, so
1732 all we need to do is frob the type of the lvalue. */
1734 /* FIXME-type-allocation: need a way to free this type when we are
1737 = lookup_array_range_type (value1
.type
, 0, length
- 1);
1739 value
->kind
= axs_lvalue_memory
;
1740 value
->type
= array
;
1746 /* Emit code for the `sizeof' operator.
1747 *PC should point at the start of the operand expression; we advance it
1748 to the first instruction after the operand. */
1750 gen_sizeof (struct expression
*exp
, union exp_element
**pc
,
1751 struct agent_expr
*ax
, struct axs_value
*value
,
1752 struct type
*size_type
)
1754 /* We don't care about the value of the operand expression; we only
1755 care about its type. However, in the current arrangement, the
1756 only way to find an expression's type is to generate code for it.
1757 So we generate code for the operand, and then throw it away,
1758 replacing it with code that simply pushes its size. */
1759 int start
= ax
->len
;
1761 gen_expr (exp
, pc
, ax
, value
);
1763 /* Throw away the code we just generated. */
1766 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
1767 value
->kind
= axs_rvalue
;
1768 value
->type
= size_type
;
1772 /* Generate bytecode for a cast to TO_TYPE. Advance *PC over the
1776 gen_expr_for_cast (struct expression
*exp
, union exp_element
**pc
,
1777 struct agent_expr
*ax
, struct axs_value
*value
,
1778 struct type
*to_type
)
1780 enum exp_opcode op
= (*pc
)[0].opcode
;
1782 /* Don't let symbols be handled with gen_expr because that throws an
1783 "unknown type" error for no-debug data symbols. Instead, we want
1784 the cast to reinterpret such symbols. */
1785 if (op
== OP_VAR_MSYM_VALUE
|| op
== OP_VAR_VALUE
)
1787 if (op
== OP_VAR_VALUE
)
1789 gen_var_ref (ax
, value
, (*pc
)[2].symbol
);
1791 if (value
->optimized_out
)
1792 error (_("`%s' has been optimized out, cannot use"),
1793 (*pc
)[2].symbol
->print_name ());
1796 gen_msym_var_ref (ax
, value
, (*pc
)[2].msymbol
, (*pc
)[1].objfile
);
1797 if (value
->type
->code () == TYPE_CODE_ERROR
)
1798 value
->type
= to_type
;
1802 gen_expr (exp
, pc
, ax
, value
);
1803 gen_cast (ax
, value
, to_type
);
1806 /* Generating bytecode from GDB expressions: general recursive thingy */
1809 /* A gen_expr function written by a Gen-X'er guy.
1810 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1812 gen_expr (struct expression
*exp
, union exp_element
**pc
,
1813 struct agent_expr
*ax
, struct axs_value
*value
)
1815 /* Used to hold the descriptions of operand expressions. */
1816 struct axs_value value1
, value2
, value3
;
1817 enum exp_opcode op
= (*pc
)[0].opcode
, op2
;
1818 int if1
, go1
, if2
, go2
, end
;
1819 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
1821 /* If we're looking at a constant expression, just push its value. */
1823 struct value
*v
= maybe_const_expr (pc
);
1827 ax_const_l (ax
, value_as_long (v
));
1828 value
->kind
= axs_rvalue
;
1829 value
->type
= check_typedef (value_type (v
));
1834 /* Otherwise, go ahead and generate code for it. */
1837 /* Binary arithmetic operators. */
1845 case BINOP_SUBSCRIPT
:
1846 case BINOP_BITWISE_AND
:
1847 case BINOP_BITWISE_IOR
:
1848 case BINOP_BITWISE_XOR
:
1850 case BINOP_NOTEQUAL
:
1856 gen_expr (exp
, pc
, ax
, &value1
);
1857 gen_usual_unary (ax
, &value1
);
1858 gen_expr_binop_rest (exp
, op
, pc
, ax
, value
, &value1
, &value2
);
1861 case BINOP_LOGICAL_AND
:
1863 /* Generate the obvious sequence of tests and jumps. */
1864 gen_expr (exp
, pc
, ax
, &value1
);
1865 gen_usual_unary (ax
, &value1
);
1866 if1
= ax_goto (ax
, aop_if_goto
);
1867 go1
= ax_goto (ax
, aop_goto
);
1868 ax_label (ax
, if1
, ax
->len
);
1869 gen_expr (exp
, pc
, ax
, &value2
);
1870 gen_usual_unary (ax
, &value2
);
1871 if2
= ax_goto (ax
, aop_if_goto
);
1872 go2
= ax_goto (ax
, aop_goto
);
1873 ax_label (ax
, if2
, ax
->len
);
1875 end
= ax_goto (ax
, aop_goto
);
1876 ax_label (ax
, go1
, ax
->len
);
1877 ax_label (ax
, go2
, ax
->len
);
1879 ax_label (ax
, end
, ax
->len
);
1880 value
->kind
= axs_rvalue
;
1881 value
->type
= int_type
;
1884 case BINOP_LOGICAL_OR
:
1886 /* Generate the obvious sequence of tests and jumps. */
1887 gen_expr (exp
, pc
, ax
, &value1
);
1888 gen_usual_unary (ax
, &value1
);
1889 if1
= ax_goto (ax
, aop_if_goto
);
1890 gen_expr (exp
, pc
, ax
, &value2
);
1891 gen_usual_unary (ax
, &value2
);
1892 if2
= ax_goto (ax
, aop_if_goto
);
1894 end
= ax_goto (ax
, aop_goto
);
1895 ax_label (ax
, if1
, ax
->len
);
1896 ax_label (ax
, if2
, ax
->len
);
1898 ax_label (ax
, end
, ax
->len
);
1899 value
->kind
= axs_rvalue
;
1900 value
->type
= int_type
;
1905 gen_expr (exp
, pc
, ax
, &value1
);
1906 gen_usual_unary (ax
, &value1
);
1907 /* For (A ? B : C), it's easiest to generate subexpression
1908 bytecodes in order, but if_goto jumps on true, so we invert
1909 the sense of A. Then we can do B by dropping through, and
1911 gen_logical_not (ax
, &value1
, int_type
);
1912 if1
= ax_goto (ax
, aop_if_goto
);
1913 gen_expr (exp
, pc
, ax
, &value2
);
1914 gen_usual_unary (ax
, &value2
);
1915 end
= ax_goto (ax
, aop_goto
);
1916 ax_label (ax
, if1
, ax
->len
);
1917 gen_expr (exp
, pc
, ax
, &value3
);
1918 gen_usual_unary (ax
, &value3
);
1919 ax_label (ax
, end
, ax
->len
);
1920 /* This is arbitrary - what if B and C are incompatible types? */
1921 value
->type
= value2
.type
;
1922 value
->kind
= value2
.kind
;
1927 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1929 const char *name
= internalvar_name ((*pc
)[1].internalvar
);
1930 struct trace_state_variable
*tsv
;
1933 gen_expr (exp
, pc
, ax
, value
);
1934 tsv
= find_trace_state_variable (name
);
1937 ax_tsv (ax
, aop_setv
, tsv
->number
);
1939 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1942 error (_("$%s is not a trace state variable, "
1943 "may not assign to it"), name
);
1946 error (_("May only assign to trace state variables"));
1949 case BINOP_ASSIGN_MODIFY
:
1951 op2
= (*pc
)[0].opcode
;
1954 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1956 const char *name
= internalvar_name ((*pc
)[1].internalvar
);
1957 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 value1
.kind
= axs_rvalue
;
1969 value1
.type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
1970 /* Now do right half of expression. */
1971 gen_expr_binop_rest (exp
, op2
, pc
, ax
, value
, &value1
, &value2
);
1972 /* We have a result of the binary op, set the tsv. */
1973 ax_tsv (ax
, aop_setv
, tsv
->number
);
1975 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1978 error (_("$%s is not a trace state variable, "
1979 "may not assign to it"), name
);
1982 error (_("May only assign to trace state variables"));
1985 /* Note that we need to be a little subtle about generating code
1986 for comma. In C, we can do some optimizations here because
1987 we know the left operand is only being evaluated for effect.
1988 However, if the tracing kludge is in effect, then we always
1989 need to evaluate the left hand side fully, so that all the
1990 variables it mentions get traced. */
1993 gen_expr (exp
, pc
, ax
, &value1
);
1994 /* Don't just dispose of the left operand. We might be tracing,
1995 in which case we want to emit code to trace it if it's an
1997 gen_traced_pop (ax
, &value1
);
1998 gen_expr (exp
, pc
, ax
, value
);
1999 /* It's the consumer's responsibility to trace the right operand. */
2002 case OP_LONG
: /* some integer constant */
2004 struct type
*type
= (*pc
)[1].type
;
2005 LONGEST k
= (*pc
)[2].longconst
;
2008 gen_int_literal (ax
, value
, k
, type
);
2013 gen_var_ref (ax
, value
, (*pc
)[2].symbol
);
2015 if (value
->optimized_out
)
2016 error (_("`%s' has been optimized out, cannot use"),
2017 (*pc
)[2].symbol
->print_name ());
2019 if (value
->type
->code () == TYPE_CODE_ERROR
)
2020 error_unknown_type ((*pc
)[2].symbol
->print_name ());
2025 case OP_VAR_MSYM_VALUE
:
2026 gen_msym_var_ref (ax
, value
, (*pc
)[2].msymbol
, (*pc
)[1].objfile
);
2028 if (value
->type
->code () == TYPE_CODE_ERROR
)
2029 error_unknown_type ((*pc
)[2].msymbol
->linkage_name ());
2036 const char *name
= &(*pc
)[2].string
;
2039 (*pc
) += 4 + BYTES_TO_EXP_ELEM ((*pc
)[1].longconst
+ 1);
2040 reg
= user_reg_map_name_to_regnum (ax
->gdbarch
, name
, strlen (name
));
2042 internal_error (__FILE__
, __LINE__
,
2043 _("Register $%s not available"), name
);
2044 /* No support for tracing user registers yet. */
2045 if (reg
>= gdbarch_num_cooked_regs (ax
->gdbarch
))
2046 error (_("'%s' is a user-register; "
2047 "GDB cannot yet trace user-register contents."),
2049 value
->kind
= axs_lvalue_register
;
2051 value
->type
= register_type (ax
->gdbarch
, reg
);
2055 case OP_INTERNALVAR
:
2057 struct internalvar
*var
= (*pc
)[1].internalvar
;
2058 const char *name
= internalvar_name (var
);
2059 struct trace_state_variable
*tsv
;
2062 tsv
= find_trace_state_variable (name
);
2065 ax_tsv (ax
, aop_getv
, tsv
->number
);
2067 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2068 /* Trace state variables are always 64-bit integers. */
2069 value
->kind
= axs_rvalue
;
2070 value
->type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
2072 else if (! compile_internalvar_to_ax (var
, ax
, value
))
2073 error (_("$%s is not a trace state variable; GDB agent "
2074 "expressions cannot use convenience variables."), name
);
2078 /* Weirdo operator: see comments for gen_repeat for details. */
2080 /* Note that gen_repeat handles its own argument evaluation. */
2082 gen_repeat (exp
, pc
, ax
, value
);
2087 struct type
*type
= (*pc
)[1].type
;
2090 gen_expr_for_cast (exp
, pc
, ax
, value
, type
);
2094 case UNOP_CAST_TYPE
:
2101 offset
= *pc
- exp
->elts
;
2102 val
= evaluate_subexp (NULL
, exp
, &offset
, EVAL_AVOID_SIDE_EFFECTS
);
2103 type
= value_type (val
);
2104 *pc
= &exp
->elts
[offset
];
2105 gen_expr_for_cast (exp
, pc
, ax
, value
, type
);
2111 struct type
*type
= check_typedef ((*pc
)[1].type
);
2114 gen_expr (exp
, pc
, ax
, value
);
2116 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2117 already have the right value on the stack. For
2118 axs_lvalue_register, we must convert. */
2119 if (value
->kind
== axs_lvalue_register
)
2120 require_rvalue (ax
, value
);
2123 value
->kind
= axs_lvalue_memory
;
2127 case UNOP_MEMVAL_TYPE
:
2134 offset
= *pc
- exp
->elts
;
2135 val
= evaluate_subexp (NULL
, exp
, &offset
, EVAL_AVOID_SIDE_EFFECTS
);
2136 type
= value_type (val
);
2137 *pc
= &exp
->elts
[offset
];
2139 gen_expr (exp
, pc
, ax
, value
);
2141 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2142 already have the right value on the stack. For
2143 axs_lvalue_register, we must convert. */
2144 if (value
->kind
== axs_lvalue_register
)
2145 require_rvalue (ax
, value
);
2148 value
->kind
= axs_lvalue_memory
;
2154 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2155 gen_expr (exp
, pc
, ax
, value
);
2156 gen_usual_unary (ax
, value
);
2161 /* -FOO is equivalent to 0 - FOO. */
2162 gen_int_literal (ax
, &value1
, 0,
2163 builtin_type (ax
->gdbarch
)->builtin_int
);
2164 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
2165 gen_expr (exp
, pc
, ax
, &value2
);
2166 gen_usual_unary (ax
, &value2
);
2167 gen_usual_arithmetic (ax
, &value1
, &value2
);
2168 gen_binop (ax
, value
, &value1
, &value2
, aop_sub
, aop_sub
, 1, "negation");
2171 case UNOP_LOGICAL_NOT
:
2173 gen_expr (exp
, pc
, ax
, value
);
2174 gen_usual_unary (ax
, value
);
2175 gen_logical_not (ax
, value
, int_type
);
2178 case UNOP_COMPLEMENT
:
2180 gen_expr (exp
, pc
, ax
, value
);
2181 gen_usual_unary (ax
, value
);
2182 gen_integral_promotions (ax
, value
);
2183 gen_complement (ax
, value
);
2188 gen_expr (exp
, pc
, ax
, value
);
2189 gen_usual_unary (ax
, value
);
2190 if (!pointer_type (value
->type
))
2191 error (_("Argument of unary `*' is not a pointer."));
2197 gen_expr (exp
, pc
, ax
, value
);
2198 gen_address_of (value
);
2203 /* Notice that gen_sizeof handles its own operand, unlike most
2204 of the other unary operator functions. This is because we
2205 have to throw away the code we generate. */
2206 gen_sizeof (exp
, pc
, ax
, value
,
2207 builtin_type (ax
->gdbarch
)->builtin_int
);
2210 case STRUCTOP_STRUCT
:
2213 int length
= (*pc
)[1].longconst
;
2214 const char *name
= &(*pc
)[2].string
;
2216 (*pc
) += 4 + BYTES_TO_EXP_ELEM (length
+ 1);
2217 gen_expr (exp
, pc
, ax
, value
);
2218 if (op
== STRUCTOP_STRUCT
)
2219 gen_struct_ref (ax
, value
, name
, ".", "structure or union");
2220 else if (op
== STRUCTOP_PTR
)
2221 gen_struct_ref (ax
, value
, name
, "->",
2222 "pointer to a structure or union");
2224 /* If this `if' chain doesn't handle it, then the case list
2225 shouldn't mention it, and we shouldn't be here. */
2226 internal_error (__FILE__
, __LINE__
,
2227 _("gen_expr: unhandled struct case"));
2233 struct symbol
*sym
, *func
;
2234 const struct block
*b
;
2235 const struct language_defn
*lang
;
2237 b
= block_for_pc (ax
->scope
);
2238 func
= block_linkage_function (b
);
2239 lang
= language_def (func
->language ());
2241 sym
= lookup_language_this (lang
, b
).symbol
;
2243 error (_("no `%s' found"), lang
->name_of_this ());
2245 gen_var_ref (ax
, value
, sym
);
2247 if (value
->optimized_out
)
2248 error (_("`%s' has been optimized out, cannot use"),
2249 sym
->print_name ());
2257 struct type
*type
= (*pc
)[1].type
;
2258 int length
= longest_to_int ((*pc
)[2].longconst
);
2259 const char *name
= &(*pc
)[3].string
;
2262 found
= gen_aggregate_elt_ref (ax
, value
, type
, name
);
2264 error (_("There is no field named %s"), name
);
2265 (*pc
) += 5 + BYTES_TO_EXP_ELEM (length
+ 1);
2272 error (_("Attempt to use a type name as an expression."));
2275 error (_("Unsupported operator %s (%d) in expression."),
2284 operation::generate_ax (struct expression
*exp
,
2285 struct agent_expr
*ax
,
2286 struct axs_value
*value
,
2287 struct type
*cast_type
)
2291 struct value
*v
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2292 ax_const_l (ax
, value_as_long (v
));
2293 value
->kind
= axs_rvalue
;
2294 value
->type
= check_typedef (value_type (v
));
2298 do_generate_ax (exp
, ax
, value
, cast_type
);
2299 if (cast_type
!= nullptr)
2300 gen_cast (ax
, value
, cast_type
);
2305 scope_operation::do_generate_ax (struct expression
*exp
,
2306 struct agent_expr
*ax
,
2307 struct axs_value
*value
,
2308 struct type
*cast_type
)
2310 struct type
*type
= std::get
<0> (m_storage
);
2311 const std::string
&name
= std::get
<1> (m_storage
);
2312 int found
= gen_aggregate_elt_ref (ax
, value
, type
, name
.c_str ());
2314 error (_("There is no field named %s"), name
.c_str ());
2318 long_const_operation::do_generate_ax (struct expression
*exp
,
2319 struct agent_expr
*ax
,
2320 struct axs_value
*value
,
2321 struct type
*cast_type
)
2323 gen_int_literal (ax
, value
, std::get
<1> (m_storage
),
2324 std::get
<0> (m_storage
));
2328 var_msym_value_operation::do_generate_ax (struct expression
*exp
,
2329 struct agent_expr
*ax
,
2330 struct axs_value
*value
,
2331 struct type
*cast_type
)
2333 gen_msym_var_ref (ax
, value
, std::get
<0> (m_storage
),
2334 std::get
<1> (m_storage
));
2336 if (value
->type
->code () == TYPE_CODE_ERROR
)
2338 if (cast_type
== nullptr)
2339 error_unknown_type (std::get
<0> (m_storage
)->linkage_name ());
2340 value
->type
= cast_type
;
2345 register_operation::do_generate_ax (struct expression
*exp
,
2346 struct agent_expr
*ax
,
2347 struct axs_value
*value
,
2348 struct type
*cast_type
)
2350 const char *name
= std::get
<0> (m_storage
).c_str ();
2351 int len
= std::get
<0> (m_storage
).size ();
2354 reg
= user_reg_map_name_to_regnum (ax
->gdbarch
, name
, len
);
2356 internal_error (__FILE__
, __LINE__
,
2357 _("Register $%s not available"), name
);
2358 /* No support for tracing user registers yet. */
2359 if (reg
>= gdbarch_num_cooked_regs (ax
->gdbarch
))
2360 error (_("'%s' is a user-register; "
2361 "GDB cannot yet trace user-register contents."),
2363 value
->kind
= axs_lvalue_register
;
2365 value
->type
= register_type (ax
->gdbarch
, reg
);
2369 internalvar_operation::do_generate_ax (struct expression
*exp
,
2370 struct agent_expr
*ax
,
2371 struct axs_value
*value
,
2372 struct type
*cast_type
)
2374 struct internalvar
*var
= std::get
<0> (m_storage
);
2375 const char *name
= internalvar_name (var
);
2376 struct trace_state_variable
*tsv
;
2378 tsv
= find_trace_state_variable (name
);
2381 ax_tsv (ax
, aop_getv
, tsv
->number
);
2383 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2384 /* Trace state variables are always 64-bit integers. */
2385 value
->kind
= axs_rvalue
;
2386 value
->type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
2388 else if (! compile_internalvar_to_ax (var
, ax
, value
))
2389 error (_("$%s is not a trace state variable; GDB agent "
2390 "expressions cannot use convenience variables."), name
);
2394 ternop_cond_operation::do_generate_ax (struct expression
*exp
,
2395 struct agent_expr
*ax
,
2396 struct axs_value
*value
,
2397 struct type
*cast_type
)
2399 struct axs_value value1
, value2
, value3
;
2402 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
2403 gen_usual_unary (ax
, &value1
);
2404 /* For (A ? B : C), it's easiest to generate subexpression
2405 bytecodes in order, but if_goto jumps on true, so we invert
2406 the sense of A. Then we can do B by dropping through, and
2408 gen_logical_not (ax
, &value1
, builtin_type (ax
->gdbarch
)->builtin_int
);
2409 if1
= ax_goto (ax
, aop_if_goto
);
2410 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, &value2
);
2411 gen_usual_unary (ax
, &value2
);
2412 end
= ax_goto (ax
, aop_goto
);
2413 ax_label (ax
, if1
, ax
->len
);
2414 std::get
<2> (m_storage
)->generate_ax (exp
, ax
, &value3
);
2415 gen_usual_unary (ax
, &value3
);
2416 ax_label (ax
, end
, ax
->len
);
2417 /* This is arbitrary - what if B and C are incompatible types? */
2418 value
->type
= value2
.type
;
2419 value
->kind
= value2
.kind
;
2422 /* Generate code for GDB's magical `repeat' operator.
2423 LVALUE @ INT creates an array INT elements long, and whose elements
2424 have the same type as LVALUE, located in memory so that LVALUE is
2425 its first element. For example, argv[0]@argc gives you the array
2426 of command-line arguments.
2428 Unfortunately, because we have to know the types before we actually
2429 have a value for the expression, we can't implement this perfectly
2430 without changing the type system, having values that occupy two
2431 stack slots, doing weird things with sizeof, etc. So we require
2432 the right operand to be a constant expression. */
2434 repeat_operation::do_generate_ax (struct expression
*exp
,
2435 struct agent_expr
*ax
,
2436 struct axs_value
*value
,
2437 struct type
*cast_type
)
2439 struct axs_value value1
;
2441 /* We don't want to turn this into an rvalue, so no conversions
2443 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
2444 if (value1
.kind
!= axs_lvalue_memory
)
2445 error (_("Left operand of `@' must be an object in memory."));
2447 /* Evaluate the length; it had better be a constant. */
2448 if (!std::get
<1> (m_storage
)->constant_p ())
2449 error (_("Right operand of `@' must be a "
2450 "constant, in agent expressions."));
2453 = std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2454 EVAL_AVOID_SIDE_EFFECTS
);
2455 if (value_type (v
)->code () != TYPE_CODE_INT
)
2456 error (_("Right operand of `@' must be an integer."));
2457 int length
= value_as_long (v
);
2459 error (_("Right operand of `@' must be positive."));
2461 /* The top of the stack is already the address of the object, so
2462 all we need to do is frob the type of the lvalue. */
2463 /* FIXME-type-allocation: need a way to free this type when we are
2466 = lookup_array_range_type (value1
.type
, 0, length
- 1);
2468 value
->kind
= axs_lvalue_memory
;
2469 value
->type
= array
;
2473 comma_operation::do_generate_ax (struct expression
*exp
,
2474 struct agent_expr
*ax
,
2475 struct axs_value
*value
,
2476 struct type
*cast_type
)
2478 /* Note that we need to be a little subtle about generating code
2479 for comma. In C, we can do some optimizations here because
2480 we know the left operand is only being evaluated for effect.
2481 However, if the tracing kludge is in effect, then we always
2482 need to evaluate the left hand side fully, so that all the
2483 variables it mentions get traced. */
2484 struct axs_value value1
;
2485 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, &value1
);
2486 /* Don't just dispose of the left operand. We might be tracing,
2487 in which case we want to emit code to trace it if it's an
2489 gen_traced_pop (ax
, &value1
);
2490 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
2491 /* It's the consumer's responsibility to trace the right operand. */
2495 unop_sizeof_operation::do_generate_ax (struct expression
*exp
,
2496 struct agent_expr
*ax
,
2497 struct axs_value
*value
,
2498 struct type
*cast_type
)
2500 /* We don't care about the value of the operand expression; we only
2501 care about its type. However, in the current arrangement, the
2502 only way to find an expression's type is to generate code for it.
2503 So we generate code for the operand, and then throw it away,
2504 replacing it with code that simply pushes its size. */
2505 int start
= ax
->len
;
2507 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
2509 /* Throw away the code we just generated. */
2512 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
2513 value
->kind
= axs_rvalue
;
2514 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2518 unop_cast_operation::do_generate_ax (struct expression
*exp
,
2519 struct agent_expr
*ax
,
2520 struct axs_value
*value
,
2521 struct type
*cast_type
)
2523 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
,
2524 std::get
<1> (m_storage
));
2528 unop_memval_operation::do_generate_ax (struct expression
*exp
,
2529 struct agent_expr
*ax
,
2530 struct axs_value
*value
,
2531 struct type
*cast_type
)
2533 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
2534 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2535 already have the right value on the stack. For
2536 axs_lvalue_register, we must convert. */
2537 if (value
->kind
== axs_lvalue_register
)
2538 require_rvalue (ax
, value
);
2540 value
->type
= std::get
<1> (m_storage
);
2541 value
->kind
= axs_lvalue_memory
;
2545 unop_memval_type_operation::do_generate_ax (struct expression
*exp
,
2546 struct agent_expr
*ax
,
2547 struct axs_value
*value
,
2548 struct type
*cast_type
)
2551 = std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2552 EVAL_AVOID_SIDE_EFFECTS
);
2553 struct type
*type
= value_type (val
);
2555 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
2557 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2558 already have the right value on the stack. For
2559 axs_lvalue_register, we must convert. */
2560 if (value
->kind
== axs_lvalue_register
)
2561 require_rvalue (ax
, value
);
2564 value
->kind
= axs_lvalue_memory
;
2568 op_this_operation::do_generate_ax (struct expression
*exp
,
2569 struct agent_expr
*ax
,
2570 struct axs_value
*value
,
2571 struct type
*cast_type
)
2573 struct symbol
*sym
, *func
;
2574 const struct block
*b
;
2575 const struct language_defn
*lang
;
2577 b
= block_for_pc (ax
->scope
);
2578 func
= block_linkage_function (b
);
2579 lang
= language_def (func
->language ());
2581 sym
= lookup_language_this (lang
, b
).symbol
;
2583 error (_("no `%s' found"), lang
->name_of_this ());
2585 gen_var_ref (ax
, value
, sym
);
2587 if (value
->optimized_out
)
2588 error (_("`%s' has been optimized out, cannot use"),
2589 sym
->print_name ());
2593 assign_operation::do_generate_ax (struct expression
*exp
,
2594 struct agent_expr
*ax
,
2595 struct axs_value
*value
,
2596 struct type
*cast_type
)
2598 operation
*subop
= std::get
<0> (m_storage
).get ();
2599 if (subop
->opcode () != OP_INTERNALVAR
)
2600 error (_("May only assign to trace state variables"));
2602 internalvar_operation
*ivarop
2603 = dynamic_cast<internalvar_operation
*> (subop
);
2604 gdb_assert (ivarop
!= nullptr);
2606 const char *name
= internalvar_name (ivarop
->get_internalvar ());
2607 struct trace_state_variable
*tsv
;
2609 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
2610 tsv
= find_trace_state_variable (name
);
2613 ax_tsv (ax
, aop_setv
, tsv
->number
);
2615 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2618 error (_("$%s is not a trace state variable, "
2619 "may not assign to it"), name
);
2623 assign_modify_operation::do_generate_ax (struct expression
*exp
,
2624 struct agent_expr
*ax
,
2625 struct axs_value
*value
,
2626 struct type
*cast_type
)
2628 operation
*subop
= std::get
<1> (m_storage
).get ();
2629 if (subop
->opcode () != OP_INTERNALVAR
)
2630 error (_("May only assign to trace state variables"));
2632 internalvar_operation
*ivarop
2633 = dynamic_cast<internalvar_operation
*> (subop
);
2634 gdb_assert (ivarop
!= nullptr);
2636 const char *name
= internalvar_name (ivarop
->get_internalvar ());
2637 struct trace_state_variable
*tsv
;
2639 tsv
= find_trace_state_variable (name
);
2642 /* The tsv will be the left half of the binary operation. */
2643 ax_tsv (ax
, aop_getv
, tsv
->number
);
2645 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2646 /* Trace state variables are always 64-bit integers. */
2647 struct axs_value value1
, value2
;
2648 value1
.kind
= axs_rvalue
;
2649 value1
.type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
2650 /* Now do right half of expression. */
2651 std::get
<2> (m_storage
)->generate_ax (exp
, ax
, &value2
);
2652 gen_expr_binop_rest (exp
, std::get
<0> (m_storage
), ax
,
2653 value
, &value1
, &value2
);
2654 /* We have a result of the binary op, set the tsv. */
2655 ax_tsv (ax
, aop_setv
, tsv
->number
);
2657 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2660 error (_("$%s is not a trace state variable, "
2661 "may not assign to it"), name
);
2666 /* This handles the middle-to-right-side of code generation for binary
2667 expressions, which is shared between regular binary operations and
2668 assign-modify (+= and friends) expressions. */
2671 gen_expr_binop_rest (struct expression
*exp
,
2673 struct agent_expr
*ax
, struct axs_value
*value
,
2674 struct axs_value
*value1
, struct axs_value
*value2
)
2676 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2678 gen_usual_unary (ax
, value2
);
2679 gen_usual_arithmetic (ax
, value1
, value2
);
2683 if (value1
->type
->code () == TYPE_CODE_INT
2684 && pointer_type (value2
->type
))
2686 /* Swap the values and proceed normally. */
2687 ax_simple (ax
, aop_swap
);
2688 gen_ptradd (ax
, value
, value2
, value1
);
2690 else if (pointer_type (value1
->type
)
2691 && value2
->type
->code () == TYPE_CODE_INT
)
2692 gen_ptradd (ax
, value
, value1
, value2
);
2694 gen_binop (ax
, value
, value1
, value2
,
2695 aop_add
, aop_add
, 1, "addition");
2698 if (pointer_type (value1
->type
)
2699 && value2
->type
->code () == TYPE_CODE_INT
)
2700 gen_ptrsub (ax
,value
, value1
, value2
);
2701 else if (pointer_type (value1
->type
)
2702 && pointer_type (value2
->type
))
2703 /* FIXME --- result type should be ptrdiff_t */
2704 gen_ptrdiff (ax
, value
, value1
, value2
,
2705 builtin_type (ax
->gdbarch
)->builtin_long
);
2707 gen_binop (ax
, value
, value1
, value2
,
2708 aop_sub
, aop_sub
, 1, "subtraction");
2711 gen_binop (ax
, value
, value1
, value2
,
2712 aop_mul
, aop_mul
, 1, "multiplication");
2715 gen_binop (ax
, value
, value1
, value2
,
2716 aop_div_signed
, aop_div_unsigned
, 1, "division");
2719 gen_binop (ax
, value
, value1
, value2
,
2720 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
2723 gen_binop (ax
, value
, value1
, value2
,
2724 aop_lsh
, aop_lsh
, 1, "left shift");
2727 gen_binop (ax
, value
, value1
, value2
,
2728 aop_rsh_signed
, aop_rsh_unsigned
, 1, "right shift");
2730 case BINOP_SUBSCRIPT
:
2734 if (binop_types_user_defined_p (op
, value1
->type
, value2
->type
))
2736 error (_("cannot subscript requested type: "
2737 "cannot call user defined functions"));
2741 /* If the user attempts to subscript something that is not
2742 an array or pointer type (like a plain int variable for
2743 example), then report this as an error. */
2744 type
= check_typedef (value1
->type
);
2745 if (type
->code () != TYPE_CODE_ARRAY
2746 && type
->code () != TYPE_CODE_PTR
)
2749 error (_("cannot subscript something of type `%s'"),
2752 error (_("cannot subscript requested type"));
2756 if (!is_integral_type (value2
->type
))
2757 error (_("Argument to arithmetic operation "
2758 "not a number or boolean."));
2760 gen_ptradd (ax
, value
, value1
, value2
);
2764 case BINOP_BITWISE_AND
:
2765 gen_binop (ax
, value
, value1
, value2
,
2766 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
2769 case BINOP_BITWISE_IOR
:
2770 gen_binop (ax
, value
, value1
, value2
,
2771 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
2774 case BINOP_BITWISE_XOR
:
2775 gen_binop (ax
, value
, value1
, value2
,
2776 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
2780 gen_equal (ax
, value
, value1
, value2
, int_type
);
2783 case BINOP_NOTEQUAL
:
2784 gen_equal (ax
, value
, value1
, value2
, int_type
);
2785 gen_logical_not (ax
, value
, int_type
);
2789 gen_less (ax
, value
, value1
, value2
, int_type
);
2793 ax_simple (ax
, aop_swap
);
2794 gen_less (ax
, value
, value1
, value2
, int_type
);
2798 ax_simple (ax
, aop_swap
);
2799 gen_less (ax
, value
, value1
, value2
, int_type
);
2800 gen_logical_not (ax
, value
, int_type
);
2804 gen_less (ax
, value
, value1
, value2
, int_type
);
2805 gen_logical_not (ax
, value
, int_type
);
2809 /* We should only list operators in the outer case statement
2810 that we actually handle in the inner case statement. */
2811 internal_error (__FILE__
, __LINE__
,
2812 _("gen_expr: op case sets don't match"));
2816 /* Variant of gen_expr_binop_rest that first generates the
2820 gen_expr_binop_rest (struct expression
*exp
,
2821 enum exp_opcode op
, union exp_element
**pc
,
2822 struct agent_expr
*ax
, struct axs_value
*value
,
2823 struct axs_value
*value1
, struct axs_value
*value2
)
2825 gen_expr (exp
, pc
, ax
, value2
);
2826 gen_expr_binop_rest (exp
, op
, ax
, value
, value1
, value2
);
2829 /* A helper function that emits a binop based on two operations. */
2832 gen_expr_binop (struct expression
*exp
,
2834 expr::operation
*lhs
, expr::operation
*rhs
,
2835 struct agent_expr
*ax
, struct axs_value
*value
)
2837 struct axs_value value1
, value2
;
2839 lhs
->generate_ax (exp
, ax
, &value1
);
2840 gen_usual_unary (ax
, &value1
);
2841 rhs
->generate_ax (exp
, ax
, &value2
);
2842 gen_expr_binop_rest (exp
, op
, ax
, value
, &value1
, &value2
);
2845 /* A helper function that emits a structop based on an operation and a
2849 gen_expr_structop (struct expression
*exp
,
2851 expr::operation
*lhs
,
2853 struct agent_expr
*ax
, struct axs_value
*value
)
2855 lhs
->generate_ax (exp
, ax
, value
);
2856 if (op
== STRUCTOP_STRUCT
)
2857 gen_struct_ref (ax
, value
, name
, ".", "structure or union");
2858 else if (op
== STRUCTOP_PTR
)
2859 gen_struct_ref (ax
, value
, name
, "->",
2860 "pointer to a structure or union");
2862 /* If this `if' chain doesn't handle it, then the case list
2863 shouldn't mention it, and we shouldn't be here. */
2864 internal_error (__FILE__
, __LINE__
,
2865 _("gen_expr: unhandled struct case"));
2868 /* A helper function that emits a unary operation. */
2871 gen_expr_unop (struct expression
*exp
,
2873 expr::operation
*lhs
,
2874 struct agent_expr
*ax
, struct axs_value
*value
)
2876 struct axs_value value1
, value2
;
2881 gen_int_literal (ax
, &value1
, 0,
2882 builtin_type (ax
->gdbarch
)->builtin_int
);
2883 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
2884 lhs
->generate_ax (exp
, ax
, &value2
);
2885 gen_usual_unary (ax
, &value2
);
2886 gen_usual_arithmetic (ax
, &value1
, &value2
);
2887 gen_binop (ax
, value
, &value1
, &value2
, aop_sub
, aop_sub
, 1, "negation");
2891 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2892 lhs
->generate_ax (exp
, ax
, value
);
2893 gen_usual_unary (ax
, value
);
2896 case UNOP_LOGICAL_NOT
:
2897 lhs
->generate_ax (exp
, ax
, value
);
2898 gen_usual_unary (ax
, value
);
2899 gen_logical_not (ax
, value
, builtin_type (ax
->gdbarch
)->builtin_int
);
2902 case UNOP_COMPLEMENT
:
2903 lhs
->generate_ax (exp
, ax
, value
);
2904 gen_usual_unary (ax
, value
);
2905 gen_integral_promotions (ax
, value
);
2906 gen_complement (ax
, value
);
2910 lhs
->generate_ax (exp
, ax
, value
);
2911 gen_usual_unary (ax
, value
);
2912 if (!pointer_type (value
->type
))
2913 error (_("Argument of unary `*' is not a pointer."));
2918 lhs
->generate_ax (exp
, ax
, value
);
2919 gen_address_of (value
);
2923 gdb_assert_not_reached ("invalid case in gen_expr_unop");
2929 /* Given a single variable and a scope, generate bytecodes to trace
2930 its value. This is for use in situations where we have only a
2931 variable's name, and no parsed expression; for instance, when the
2932 name comes from a list of local variables of a function. */
2935 gen_trace_for_var (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2936 struct symbol
*var
, int trace_string
)
2938 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2939 struct axs_value value
;
2942 ax
->trace_string
= trace_string
;
2943 gen_var_ref (ax
.get (), &value
, var
);
2945 /* If there is no actual variable to trace, flag it by returning
2946 an empty agent expression. */
2947 if (value
.optimized_out
)
2948 return agent_expr_up ();
2950 /* Make sure we record the final object, and get rid of it. */
2951 gen_traced_pop (ax
.get (), &value
);
2953 /* Oh, and terminate. */
2954 ax_simple (ax
.get (), aop_end
);
2959 /* Generating bytecode from GDB expressions: driver */
2961 /* Given a GDB expression EXPR, return bytecode to trace its value.
2962 The result will use the `trace' and `trace_quick' bytecodes to
2963 record the value of all memory touched by the expression. The
2964 caller can then use the ax_reqs function to discover which
2965 registers it relies upon. */
2968 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
,
2971 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2972 union exp_element
*pc
;
2973 struct axs_value value
;
2977 ax
->trace_string
= trace_string
;
2978 value
.optimized_out
= 0;
2979 gen_expr (expr
, &pc
, ax
.get (), &value
);
2981 /* Make sure we record the final object, and get rid of it. */
2982 gen_traced_pop (ax
.get (), &value
);
2984 /* Oh, and terminate. */
2985 ax_simple (ax
.get (), aop_end
);
2990 /* Given a GDB expression EXPR, return a bytecode sequence that will
2991 evaluate and return a result. The bytecodes will do a direct
2992 evaluation, using the current data on the target, rather than
2993 recording blocks of memory and registers for later use, as
2994 gen_trace_for_expr does. The generated bytecode sequence leaves
2995 the result of expression evaluation on the top of the stack. */
2998 gen_eval_for_expr (CORE_ADDR scope
, struct expression
*expr
)
3000 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
3001 union exp_element
*pc
;
3002 struct axs_value value
;
3006 value
.optimized_out
= 0;
3007 gen_expr (expr
, &pc
, ax
.get (), &value
);
3009 require_rvalue (ax
.get (), &value
);
3011 /* Oh, and terminate. */
3012 ax_simple (ax
.get (), aop_end
);
3018 gen_trace_for_return_address (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
3021 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
3022 struct axs_value value
;
3025 ax
->trace_string
= trace_string
;
3027 gdbarch_gen_return_address (gdbarch
, ax
.get (), &value
, scope
);
3029 /* Make sure we record the final object, and get rid of it. */
3030 gen_traced_pop (ax
.get (), &value
);
3032 /* Oh, and terminate. */
3033 ax_simple (ax
.get (), aop_end
);
3038 /* Given a collection of printf-style arguments, generate code to
3039 evaluate the arguments and pass everything to a special
3043 gen_printf (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
3044 CORE_ADDR function
, LONGEST channel
,
3045 const char *format
, int fmtlen
,
3046 int nargs
, struct expression
**exprs
)
3048 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
3049 union exp_element
*pc
;
3050 struct axs_value value
;
3053 /* We're computing values, not doing side effects. */
3056 /* Evaluate and push the args on the stack in reverse order,
3057 for simplicity of collecting them on the target side. */
3058 for (tem
= nargs
- 1; tem
>= 0; --tem
)
3060 pc
= exprs
[tem
]->elts
;
3061 value
.optimized_out
= 0;
3062 gen_expr (exprs
[tem
], &pc
, ax
.get (), &value
);
3063 require_rvalue (ax
.get (), &value
);
3066 /* Push function and channel. */
3067 ax_const_l (ax
.get (), channel
);
3068 ax_const_l (ax
.get (), function
);
3070 /* Issue the printf bytecode proper. */
3071 ax_simple (ax
.get (), aop_printf
);
3072 ax_raw_byte (ax
.get (), nargs
);
3073 ax_string (ax
.get (), format
, fmtlen
);
3075 /* And terminate. */
3076 ax_simple (ax
.get (), aop_end
);
3082 agent_eval_command_one (const char *exp
, int eval
, CORE_ADDR pc
)
3085 int trace_string
= 0;
3090 exp
= decode_agent_options (exp
, &trace_string
);
3093 agent_expr_up agent
;
3096 if (!eval
&& strcmp (arg
, "$_ret") == 0)
3098 agent
= gen_trace_for_return_address (pc
, get_current_arch (),
3103 expression_up expr
= parse_exp_1 (&arg
, pc
, block_for_pc (pc
), 0);
3107 gdb_assert (trace_string
== 0);
3108 agent
= gen_eval_for_expr (pc
, expr
.get ());
3111 agent
= gen_trace_for_expr (pc
, expr
.get (), trace_string
);
3114 ax_reqs (agent
.get ());
3115 ax_print (gdb_stdout
, agent
.get ());
3117 /* It would be nice to call ax_reqs here to gather some general info
3118 about the expression, and then print out the result. */
3124 agent_command_1 (const char *exp
, int eval
)
3126 /* We don't deal with overlay debugging at the moment. We need to
3127 think more carefully about this. If you copy this code into
3128 another command, change the error message; the user shouldn't
3129 have to know anything about agent expressions. */
3130 if (overlay_debugging
)
3131 error (_("GDB can't do agent expression translation with overlays."));
3134 error_no_arg (_("expression to translate"));
3136 if (check_for_argument (&exp
, "-at", sizeof ("-at") - 1))
3138 struct linespec_result canonical
;
3140 event_location_up location
3141 = new_linespec_location (&exp
, symbol_name_match_type::WILD
);
3142 decode_line_full (location
.get (), DECODE_LINE_FUNFIRSTLINE
, NULL
,
3143 NULL
, 0, &canonical
,
3145 exp
= skip_spaces (exp
);
3149 exp
= skip_spaces (exp
);
3151 for (const auto &lsal
: canonical
.lsals
)
3152 for (const auto &sal
: lsal
.sals
)
3153 agent_eval_command_one (exp
, eval
, sal
.pc
);
3156 agent_eval_command_one (exp
, eval
, get_frame_pc (get_current_frame ()));
3162 agent_command (const char *exp
, int from_tty
)
3164 agent_command_1 (exp
, 0);
3167 /* Parse the given expression, compile it into an agent expression
3168 that does direct evaluation, and display the resulting
3172 agent_eval_command (const char *exp
, int from_tty
)
3174 agent_command_1 (exp
, 1);
3177 /* Parse the given expression, compile it into an agent expression
3178 that does a printf, and display the resulting expression. */
3181 maint_agent_printf_command (const char *cmdrest
, int from_tty
)
3183 struct frame_info
*fi
= get_current_frame (); /* need current scope */
3184 const char *format_start
, *format_end
;
3186 /* We don't deal with overlay debugging at the moment. We need to
3187 think more carefully about this. If you copy this code into
3188 another command, change the error message; the user shouldn't
3189 have to know anything about agent expressions. */
3190 if (overlay_debugging
)
3191 error (_("GDB can't do agent expression translation with overlays."));
3194 error_no_arg (_("expression to translate"));
3196 cmdrest
= skip_spaces (cmdrest
);
3198 if (*cmdrest
++ != '"')
3199 error (_("Must start with a format string."));
3201 format_start
= cmdrest
;
3203 format_pieces
fpieces (&cmdrest
);
3205 format_end
= cmdrest
;
3207 if (*cmdrest
++ != '"')
3208 error (_("Bad format string, non-terminated '\"'."));
3210 cmdrest
= skip_spaces (cmdrest
);
3212 if (*cmdrest
!= ',' && *cmdrest
!= 0)
3213 error (_("Invalid argument syntax"));
3215 if (*cmdrest
== ',')
3217 cmdrest
= skip_spaces (cmdrest
);
3219 std::vector
<struct expression
*> argvec
;
3220 while (*cmdrest
!= '\0')
3225 expression_up expr
= parse_exp_1 (&cmd1
, 0, (struct block
*) 0, 1);
3226 argvec
.push_back (expr
.release ());
3228 if (*cmdrest
== ',')
3230 /* else complain? */
3234 agent_expr_up agent
= gen_printf (get_frame_pc (fi
), get_current_arch (),
3236 format_start
, format_end
- format_start
,
3237 argvec
.size (), argvec
.data ());
3238 ax_reqs (agent
.get ());
3239 ax_print (gdb_stdout
, agent
.get ());
3241 /* It would be nice to call ax_reqs here to gather some general info
3242 about the expression, and then print out the result. */
3247 /* Initialization code. */
3249 void _initialize_ax_gdb ();
3251 _initialize_ax_gdb ()
3253 add_cmd ("agent", class_maintenance
, agent_command
,
3255 Translate an expression into remote agent bytecode for tracing.\n\
3256 Usage: maint agent [-at LOCATION,] EXPRESSION\n\
3257 If -at is given, generate remote agent bytecode for this location.\n\
3258 If not, generate remote agent bytecode for current frame pc address."),
3261 add_cmd ("agent-eval", class_maintenance
, agent_eval_command
,
3263 Translate an expression into remote agent bytecode for evaluation.\n\
3264 Usage: maint agent-eval [-at LOCATION,] EXPRESSION\n\
3265 If -at is given, generate remote agent bytecode for this location.\n\
3266 If not, generate remote agent bytecode for current frame pc address."),
3269 add_cmd ("agent-printf", class_maintenance
, maint_agent_printf_command
,
3270 _("Translate an expression into remote "
3271 "agent bytecode for evaluation and display the bytecodes."),