1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998-2017 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"
51 /* To make sense of this file, you should read doc/agentexpr.texi.
52 Then look at the types and enums in ax-gdb.h. For the code itself,
53 look at gen_expr, towards the bottom; that's the main function that
54 looks at the GDB expressions and calls everything else to generate
57 I'm beginning to wonder whether it wouldn't be nicer to internally
58 generate trees, with types, and then spit out the bytecode in
59 linear form afterwards; we could generate fewer `swap', `ext', and
60 `zero_ext' bytecodes that way; it would make good constant folding
61 easier, too. But at the moment, I think we should be willing to
62 pay for the simplicity of this code with less-than-optimal bytecode
65 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
69 /* Prototypes for local functions. */
71 /* There's a standard order to the arguments of these functions:
72 union exp_element ** --- pointer into expression
73 struct agent_expr * --- agent expression buffer to generate code into
74 struct axs_value * --- describes value left on top of stack */
76 static struct value
*const_var_ref (struct symbol
*var
);
77 static struct value
*const_expr (union exp_element
**pc
);
78 static struct value
*maybe_const_expr (union exp_element
**pc
);
80 static void gen_traced_pop (struct agent_expr
*, struct axs_value
*);
82 static void gen_sign_extend (struct agent_expr
*, struct type
*);
83 static void gen_extend (struct agent_expr
*, struct type
*);
84 static void gen_fetch (struct agent_expr
*, struct type
*);
85 static void gen_left_shift (struct agent_expr
*, int);
88 static void gen_frame_args_address (struct agent_expr
*);
89 static void gen_frame_locals_address (struct agent_expr
*);
90 static void gen_offset (struct agent_expr
*ax
, int offset
);
91 static void gen_sym_offset (struct agent_expr
*, struct symbol
*);
92 static void gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
,
96 static void gen_int_literal (struct agent_expr
*ax
,
97 struct axs_value
*value
,
98 LONGEST k
, struct type
*type
);
100 static void gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
);
101 static int type_wider_than (struct type
*type1
, struct type
*type2
);
102 static struct type
*max_type (struct type
*type1
, struct type
*type2
);
103 static void gen_conversion (struct agent_expr
*ax
,
104 struct type
*from
, struct type
*to
);
105 static int is_nontrivial_conversion (struct type
*from
, struct type
*to
);
106 static void gen_usual_arithmetic (struct agent_expr
*ax
,
107 struct axs_value
*value1
,
108 struct axs_value
*value2
);
109 static void gen_integral_promotions (struct agent_expr
*ax
,
110 struct axs_value
*value
);
111 static void gen_cast (struct agent_expr
*ax
,
112 struct axs_value
*value
, struct type
*type
);
113 static void gen_scale (struct agent_expr
*ax
,
114 enum agent_op op
, struct type
*type
);
115 static void gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
116 struct axs_value
*value1
, struct axs_value
*value2
);
117 static void gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
118 struct axs_value
*value1
, struct axs_value
*value2
);
119 static void gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
120 struct axs_value
*value1
, struct axs_value
*value2
,
121 struct type
*result_type
);
122 static void gen_binop (struct agent_expr
*ax
,
123 struct axs_value
*value
,
124 struct axs_value
*value1
,
125 struct axs_value
*value2
,
127 enum agent_op op_unsigned
, int may_carry
,
129 static void gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
130 struct type
*result_type
);
131 static void gen_complement (struct agent_expr
*ax
, struct axs_value
*value
);
132 static void gen_deref (struct axs_value
*);
133 static void gen_address_of (struct axs_value
*);
134 static void gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
135 struct type
*type
, int start
, int end
);
136 static void gen_primitive_field (struct agent_expr
*ax
,
137 struct axs_value
*value
,
138 int offset
, int fieldno
, struct type
*type
);
139 static int gen_struct_ref_recursive (struct agent_expr
*ax
,
140 struct axs_value
*value
,
141 const char *field
, int offset
,
143 static void gen_struct_ref (struct agent_expr
*ax
,
144 struct axs_value
*value
,
146 const char *operator_name
,
147 const char *operand_name
);
148 static void gen_static_field (struct agent_expr
*ax
, struct axs_value
*value
,
149 struct type
*type
, int fieldno
);
150 static void gen_repeat (struct expression
*exp
, union exp_element
**pc
,
151 struct agent_expr
*ax
, struct axs_value
*value
);
152 static void gen_sizeof (struct expression
*exp
, union exp_element
**pc
,
153 struct agent_expr
*ax
, struct axs_value
*value
,
154 struct type
*size_type
);
155 static void gen_expr_binop_rest (struct expression
*exp
,
156 enum exp_opcode op
, union exp_element
**pc
,
157 struct agent_expr
*ax
,
158 struct axs_value
*value
,
159 struct axs_value
*value1
,
160 struct axs_value
*value2
);
162 static void agent_command (char *exp
, int from_tty
);
165 /* Detecting constant expressions. */
167 /* If the variable reference at *PC is a constant, return its value.
168 Otherwise, return zero.
170 Hey, Wally! How can a variable reference be a constant?
172 Well, Beav, this function really handles the OP_VAR_VALUE operator,
173 not specifically variable references. GDB uses OP_VAR_VALUE to
174 refer to any kind of symbolic reference: function names, enum
175 elements, and goto labels are all handled through the OP_VAR_VALUE
176 operator, even though they're constants. It makes sense given the
179 Gee, Wally, don'cha wonder sometimes if data representations that
180 subvert commonly accepted definitions of terms in favor of heavily
181 context-specific interpretations are really just a tool of the
182 programming hegemony to preserve their power and exclude the
185 static struct value
*
186 const_var_ref (struct symbol
*var
)
188 struct type
*type
= SYMBOL_TYPE (var
);
190 switch (SYMBOL_CLASS (var
))
193 return value_from_longest (type
, (LONGEST
) SYMBOL_VALUE (var
));
196 return value_from_pointer (type
, (CORE_ADDR
) SYMBOL_VALUE_ADDRESS (var
));
204 /* If the expression starting at *PC has a constant value, return it.
205 Otherwise, return zero. If we return a value, then *PC will be
206 advanced to the end of it. If we return zero, *PC could be
208 static struct value
*
209 const_expr (union exp_element
**pc
)
211 enum exp_opcode op
= (*pc
)->opcode
;
218 struct type
*type
= (*pc
)[1].type
;
219 LONGEST k
= (*pc
)[2].longconst
;
222 return value_from_longest (type
, k
);
227 struct value
*v
= const_var_ref ((*pc
)[2].symbol
);
233 /* We could add more operators in here. */
237 v1
= const_expr (pc
);
239 return value_neg (v1
);
249 /* Like const_expr, but guarantee also that *PC is undisturbed if the
250 expression is not constant. */
251 static struct value
*
252 maybe_const_expr (union exp_element
**pc
)
254 union exp_element
*tentative_pc
= *pc
;
255 struct value
*v
= const_expr (&tentative_pc
);
257 /* If we got a value, then update the real PC. */
265 /* Generating bytecode from GDB expressions: general assumptions */
267 /* Here are a few general assumptions made throughout the code; if you
268 want to make a change that contradicts one of these, then you'd
269 better scan things pretty thoroughly.
271 - We assume that all values occupy one stack element. For example,
272 sometimes we'll swap to get at the left argument to a binary
273 operator. If we decide that void values should occupy no stack
274 elements, or that synthetic arrays (whose size is determined at
275 run time, created by the `@' operator) should occupy two stack
276 elements (address and length), then this will cause trouble.
278 - We assume the stack elements are infinitely wide, and that we
279 don't have to worry what happens if the user requests an
280 operation that is wider than the actual interpreter's stack.
281 That is, it's up to the interpreter to handle directly all the
282 integer widths the user has access to. (Woe betide the language
285 - We don't support side effects. Thus, we don't have to worry about
286 GCC's generalized lvalues, function calls, etc.
288 - We don't support floating point. Many places where we switch on
289 some type don't bother to include cases for floating point; there
290 may be even more subtle ways this assumption exists. For
291 example, the arguments to % must be integers.
293 - We assume all subexpressions have a static, unchanging type. If
294 we tried to support convenience variables, this would be a
297 - All values on the stack should always be fully zero- or
300 (I wasn't sure whether to choose this or its opposite --- that
301 only addresses are assumed extended --- but it turns out that
302 neither convention completely eliminates spurious extend
303 operations (if everything is always extended, then you have to
304 extend after add, because it could overflow; if nothing is
305 extended, then you end up producing extends whenever you change
306 sizes), and this is simpler.) */
309 /* Scan for all static fields in the given class, including any base
310 classes, and generate tracing bytecodes for each. */
313 gen_trace_static_fields (struct agent_expr
*ax
,
316 int i
, nbases
= TYPE_N_BASECLASSES (type
);
317 struct axs_value value
;
319 type
= check_typedef (type
);
321 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
323 if (field_is_static (&TYPE_FIELD (type
, i
)))
325 gen_static_field (ax
, &value
, type
, i
);
326 if (value
.optimized_out
)
330 case axs_lvalue_memory
:
332 /* Initialize the TYPE_LENGTH if it is a typedef. */
333 check_typedef (value
.type
);
334 ax_const_l (ax
, TYPE_LENGTH (value
.type
));
335 ax_simple (ax
, aop_trace
);
339 case axs_lvalue_register
:
340 /* We don't actually need the register's value to be pushed,
341 just note that we need it to be collected. */
342 ax_reg_mask (ax
, value
.u
.reg
);
350 /* Now scan through base classes recursively. */
351 for (i
= 0; i
< nbases
; i
++)
353 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
355 gen_trace_static_fields (ax
, basetype
);
359 /* Trace the lvalue on the stack, if it needs it. In either case, pop
360 the value. Useful on the left side of a comma, and at the end of
361 an expression being used for tracing. */
363 gen_traced_pop (struct agent_expr
*ax
, struct axs_value
*value
)
365 int string_trace
= 0;
367 && TYPE_CODE (value
->type
) == TYPE_CODE_PTR
368 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value
->type
)),
378 ax_const_l (ax
, ax
->trace_string
);
379 ax_simple (ax
, aop_tracenz
);
382 /* We don't trace rvalues, just the lvalues necessary to
383 produce them. So just dispose of this value. */
384 ax_simple (ax
, aop_pop
);
387 case axs_lvalue_memory
:
389 /* Initialize the TYPE_LENGTH if it is a typedef. */
390 check_typedef (value
->type
);
394 gen_fetch (ax
, value
->type
);
395 ax_const_l (ax
, ax
->trace_string
);
396 ax_simple (ax
, aop_tracenz
);
400 /* There's no point in trying to use a trace_quick bytecode
401 here, since "trace_quick SIZE pop" is three bytes, whereas
402 "const8 SIZE trace" is also three bytes, does the same
403 thing, and the simplest code which generates that will also
404 work correctly for objects with large sizes. */
405 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
406 ax_simple (ax
, aop_trace
);
411 case axs_lvalue_register
:
412 /* We don't actually need the register's value to be on the
413 stack, and the target will get heartburn if the register is
414 larger than will fit in a stack, so just mark it for
415 collection and be done with it. */
416 ax_reg_mask (ax
, value
->u
.reg
);
418 /* But if the register points to a string, assume the value
419 will fit on the stack and push it anyway. */
422 ax_reg (ax
, value
->u
.reg
);
423 ax_const_l (ax
, ax
->trace_string
);
424 ax_simple (ax
, aop_tracenz
);
429 /* If we're not tracing, just pop the value. */
430 ax_simple (ax
, aop_pop
);
432 /* To trace C++ classes with static fields stored elsewhere. */
434 && (TYPE_CODE (value
->type
) == TYPE_CODE_STRUCT
435 || TYPE_CODE (value
->type
) == TYPE_CODE_UNION
))
436 gen_trace_static_fields (ax
, value
->type
);
441 /* Generating bytecode from GDB expressions: helper functions */
443 /* Assume that the lower bits of the top of the stack is a value of
444 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
446 gen_sign_extend (struct agent_expr
*ax
, struct type
*type
)
448 /* Do we need to sign-extend this? */
449 if (!TYPE_UNSIGNED (type
))
450 ax_ext (ax
, TYPE_LENGTH (type
) * TARGET_CHAR_BIT
);
454 /* Assume the lower bits of the top of the stack hold a value of type
455 TYPE, and the upper bits are garbage. Sign-extend or truncate as
458 gen_extend (struct agent_expr
*ax
, struct type
*type
)
460 int bits
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
463 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, bits
));
467 /* Assume that the top of the stack contains a value of type "pointer
468 to TYPE"; generate code to fetch its value. Note that TYPE is the
469 target type, not the pointer type. */
471 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
475 /* Record the area of memory we're about to fetch. */
476 ax_trace_quick (ax
, TYPE_LENGTH (type
));
479 if (TYPE_CODE (type
) == TYPE_CODE_RANGE
)
480 type
= TYPE_TARGET_TYPE (type
);
482 switch (TYPE_CODE (type
))
486 case TYPE_CODE_RVALUE_REF
:
491 /* It's a scalar value, so we know how to dereference it. How
492 many bytes long is it? */
493 switch (TYPE_LENGTH (type
))
495 case 8 / TARGET_CHAR_BIT
:
496 ax_simple (ax
, aop_ref8
);
498 case 16 / TARGET_CHAR_BIT
:
499 ax_simple (ax
, aop_ref16
);
501 case 32 / TARGET_CHAR_BIT
:
502 ax_simple (ax
, aop_ref32
);
504 case 64 / TARGET_CHAR_BIT
:
505 ax_simple (ax
, aop_ref64
);
508 /* Either our caller shouldn't have asked us to dereference
509 that pointer (other code's fault), or we're not
510 implementing something we should be (this code's fault).
511 In any case, it's a bug the user shouldn't see. */
513 internal_error (__FILE__
, __LINE__
,
514 _("gen_fetch: strange size"));
517 gen_sign_extend (ax
, type
);
521 /* Our caller requested us to dereference a pointer from an unsupported
522 type. Error out and give callers a chance to handle the failure
524 error (_("gen_fetch: Unsupported type code `%s'."),
530 /* Generate code to left shift the top of the stack by DISTANCE bits, or
531 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
532 unsigned (logical) right shifts. */
534 gen_left_shift (struct agent_expr
*ax
, int distance
)
538 ax_const_l (ax
, distance
);
539 ax_simple (ax
, aop_lsh
);
541 else if (distance
< 0)
543 ax_const_l (ax
, -distance
);
544 ax_simple (ax
, aop_rsh_unsigned
);
550 /* Generating bytecode from GDB expressions: symbol references */
552 /* Generate code to push the base address of the argument portion of
553 the top stack frame. */
555 gen_frame_args_address (struct agent_expr
*ax
)
558 LONGEST frame_offset
;
560 gdbarch_virtual_frame_pointer (ax
->gdbarch
,
561 ax
->scope
, &frame_reg
, &frame_offset
);
562 ax_reg (ax
, frame_reg
);
563 gen_offset (ax
, frame_offset
);
567 /* Generate code to push the base address of the locals portion of the
570 gen_frame_locals_address (struct agent_expr
*ax
)
573 LONGEST frame_offset
;
575 gdbarch_virtual_frame_pointer (ax
->gdbarch
,
576 ax
->scope
, &frame_reg
, &frame_offset
);
577 ax_reg (ax
, frame_reg
);
578 gen_offset (ax
, frame_offset
);
582 /* Generate code to add OFFSET to the top of the stack. Try to
583 generate short and readable code. We use this for getting to
584 variables on the stack, and structure members. If we were
585 programming in ML, it would be clearer why these are the same
588 gen_offset (struct agent_expr
*ax
, int offset
)
590 /* It would suffice to simply push the offset and add it, but this
591 makes it easier to read positive and negative offsets in the
595 ax_const_l (ax
, offset
);
596 ax_simple (ax
, aop_add
);
600 ax_const_l (ax
, -offset
);
601 ax_simple (ax
, aop_sub
);
606 /* In many cases, a symbol's value is the offset from some other
607 address (stack frame, base register, etc.) Generate code to add
608 VAR's value to the top of the stack. */
610 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
612 gen_offset (ax
, SYMBOL_VALUE (var
));
616 /* Generate code for a variable reference to AX. The variable is the
617 symbol VAR. Set VALUE to describe the result. */
620 gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
, struct symbol
*var
)
622 /* Dereference any typedefs. */
623 value
->type
= check_typedef (SYMBOL_TYPE (var
));
624 value
->optimized_out
= 0;
626 if (SYMBOL_COMPUTED_OPS (var
) != NULL
)
628 SYMBOL_COMPUTED_OPS (var
)->tracepoint_var_ref (var
, ax
, value
);
632 /* I'm imitating the code in read_var_value. */
633 switch (SYMBOL_CLASS (var
))
635 case LOC_CONST
: /* A constant, like an enum value. */
636 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE (var
));
637 value
->kind
= axs_rvalue
;
640 case LOC_LABEL
: /* A goto label, being used as a value. */
641 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
642 value
->kind
= axs_rvalue
;
645 case LOC_CONST_BYTES
:
646 internal_error (__FILE__
, __LINE__
,
647 _("gen_var_ref: LOC_CONST_BYTES "
648 "symbols are not supported"));
650 /* Variable at a fixed location in memory. Easy. */
652 /* Push the address of the variable. */
653 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (var
));
654 value
->kind
= axs_lvalue_memory
;
657 case LOC_ARG
: /* var lives in argument area of frame */
658 gen_frame_args_address (ax
);
659 gen_sym_offset (ax
, var
);
660 value
->kind
= axs_lvalue_memory
;
663 case LOC_REF_ARG
: /* As above, but the frame slot really
664 holds the address of the variable. */
665 gen_frame_args_address (ax
);
666 gen_sym_offset (ax
, var
);
667 /* Don't assume any particular pointer size. */
668 gen_fetch (ax
, builtin_type (ax
->gdbarch
)->builtin_data_ptr
);
669 value
->kind
= axs_lvalue_memory
;
672 case LOC_LOCAL
: /* var lives in locals area of frame */
673 gen_frame_locals_address (ax
);
674 gen_sym_offset (ax
, var
);
675 value
->kind
= axs_lvalue_memory
;
679 error (_("Cannot compute value of typedef `%s'."),
680 SYMBOL_PRINT_NAME (var
));
684 ax_const_l (ax
, BLOCK_START (SYMBOL_BLOCK_VALUE (var
)));
685 value
->kind
= axs_rvalue
;
689 /* Don't generate any code at all; in the process of treating
690 this as an lvalue or rvalue, the caller will generate the
692 value
->kind
= axs_lvalue_register
;
694 = SYMBOL_REGISTER_OPS (var
)->register_number (var
, ax
->gdbarch
);
697 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
698 register, not on the stack. Simpler than LOC_REGISTER
699 because it's just like any other case where the thing
700 has a real address. */
701 case LOC_REGPARM_ADDR
:
703 SYMBOL_REGISTER_OPS (var
)->register_number (var
, ax
->gdbarch
));
704 value
->kind
= axs_lvalue_memory
;
709 struct bound_minimal_symbol msym
710 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var
), NULL
, NULL
);
713 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var
));
715 /* Push the address of the variable. */
716 ax_const_l (ax
, BMSYMBOL_VALUE_ADDRESS (msym
));
717 value
->kind
= axs_lvalue_memory
;
722 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
724 case LOC_OPTIMIZED_OUT
:
725 /* Flag this, but don't say anything; leave it up to callers to
727 value
->optimized_out
= 1;
731 error (_("Cannot find value of botched symbol `%s'."),
732 SYMBOL_PRINT_NAME (var
));
737 /* Generate code for a minimal symbol variable reference to AX. The
738 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
742 gen_msym_var_ref (agent_expr
*ax
, axs_value
*value
,
743 minimal_symbol
*msymbol
, objfile
*objf
)
746 type
*t
= find_minsym_type_and_address (msymbol
, objf
, &address
);
748 value
->optimized_out
= false;
749 ax_const_l (ax
, address
);
750 value
->kind
= axs_lvalue_memory
;
756 /* Generating bytecode from GDB expressions: literals */
759 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
763 value
->kind
= axs_rvalue
;
764 value
->type
= check_typedef (type
);
769 /* Generating bytecode from GDB expressions: unary conversions, casts */
771 /* Take what's on the top of the stack (as described by VALUE), and
772 try to make an rvalue out of it. Signal an error if we can't do
775 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
777 /* Only deal with scalars, structs and such may be too large
778 to fit in a stack entry. */
779 value
->type
= check_typedef (value
->type
);
780 if (TYPE_CODE (value
->type
) == TYPE_CODE_ARRAY
781 || TYPE_CODE (value
->type
) == TYPE_CODE_STRUCT
782 || TYPE_CODE (value
->type
) == TYPE_CODE_UNION
783 || TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
784 error (_("Value not scalar: cannot be an rvalue."));
789 /* It's already an rvalue. */
792 case axs_lvalue_memory
:
793 /* The top of stack is the address of the object. Dereference. */
794 gen_fetch (ax
, value
->type
);
797 case axs_lvalue_register
:
798 /* There's nothing on the stack, but value->u.reg is the
799 register number containing the value.
801 When we add floating-point support, this is going to have to
802 change. What about SPARC register pairs, for example? */
803 ax_reg (ax
, value
->u
.reg
);
804 gen_extend (ax
, value
->type
);
808 value
->kind
= axs_rvalue
;
812 /* Assume the top of the stack is described by VALUE, and perform the
813 usual unary conversions. This is motivated by ANSI 6.2.2, but of
814 course GDB expressions are not ANSI; they're the mishmash union of
815 a bunch of languages. Rah.
817 NOTE! This function promises to produce an rvalue only when the
818 incoming value is of an appropriate type. In other words, the
819 consumer of the value this function produces may assume the value
820 is an rvalue only after checking its type.
822 The immediate issue is that if the user tries to use a structure or
823 union as an operand of, say, the `+' operator, we don't want to try
824 to convert that structure to an rvalue; require_rvalue will bomb on
825 structs and unions. Rather, we want to simply pass the struct
826 lvalue through unchanged, and let `+' raise an error. */
829 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
831 /* We don't have to generate any code for the usual integral
832 conversions, since values are always represented as full-width on
833 the stack. Should we tweak the type? */
835 /* Some types require special handling. */
836 switch (TYPE_CODE (value
->type
))
838 /* Functions get converted to a pointer to the function. */
840 value
->type
= lookup_pointer_type (value
->type
);
841 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
844 /* Arrays get converted to a pointer to their first element, and
845 are no longer an lvalue. */
846 case TYPE_CODE_ARRAY
:
848 struct type
*elements
= TYPE_TARGET_TYPE (value
->type
);
850 value
->type
= lookup_pointer_type (elements
);
851 value
->kind
= axs_rvalue
;
852 /* We don't need to generate any code; the address of the array
853 is also the address of its first element. */
857 /* Don't try to convert structures and unions to rvalues. Let the
858 consumer signal an error. */
859 case TYPE_CODE_STRUCT
:
860 case TYPE_CODE_UNION
:
864 /* If the value is an lvalue, dereference it. */
865 require_rvalue (ax
, value
);
869 /* Return non-zero iff the type TYPE1 is considered "wider" than the
870 type TYPE2, according to the rules described in gen_usual_arithmetic. */
872 type_wider_than (struct type
*type1
, struct type
*type2
)
874 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)
875 || (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
876 && TYPE_UNSIGNED (type1
)
877 && !TYPE_UNSIGNED (type2
)));
881 /* Return the "wider" of the two types TYPE1 and TYPE2. */
883 max_type (struct type
*type1
, struct type
*type2
)
885 return type_wider_than (type1
, type2
) ? type1
: type2
;
889 /* Generate code to convert a scalar value of type FROM to type TO. */
891 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
893 /* Perhaps there is a more graceful way to state these rules. */
895 /* If we're converting to a narrower type, then we need to clear out
897 if (TYPE_LENGTH (to
) < TYPE_LENGTH (from
))
900 /* If the two values have equal width, but different signednesses,
901 then we need to extend. */
902 else if (TYPE_LENGTH (to
) == TYPE_LENGTH (from
))
904 if (TYPE_UNSIGNED (from
) != TYPE_UNSIGNED (to
))
908 /* If we're converting to a wider type, and becoming unsigned, then
909 we need to zero out any possible sign bits. */
910 else if (TYPE_LENGTH (to
) > TYPE_LENGTH (from
))
912 if (TYPE_UNSIGNED (to
))
918 /* Return non-zero iff the type FROM will require any bytecodes to be
919 emitted to be converted to the type TO. */
921 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
923 agent_expr_up
ax (new agent_expr (NULL
, 0));
926 /* Actually generate the code, and see if anything came out. At the
927 moment, it would be trivial to replicate the code in
928 gen_conversion here, but in the future, when we're supporting
929 floating point and the like, it may not be. Doing things this
930 way allows this function to be independent of the logic in
932 gen_conversion (ax
.get (), from
, to
);
933 nontrivial
= ax
->len
> 0;
938 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
939 6.2.1.5) for the two operands of an arithmetic operator. This
940 effectively finds a "least upper bound" type for the two arguments,
941 and promotes each argument to that type. *VALUE1 and *VALUE2
942 describe the values as they are passed in, and as they are left. */
944 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
945 struct axs_value
*value2
)
947 /* Do the usual binary conversions. */
948 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
949 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
951 /* The ANSI integral promotions seem to work this way: Order the
952 integer types by size, and then by signedness: an n-bit
953 unsigned type is considered "wider" than an n-bit signed
954 type. Promote to the "wider" of the two types, and always
955 promote at least to int. */
956 struct type
*target
= max_type (builtin_type (ax
->gdbarch
)->builtin_int
,
957 max_type (value1
->type
, value2
->type
));
959 /* Deal with value2, on the top of the stack. */
960 gen_conversion (ax
, value2
->type
, target
);
962 /* Deal with value1, not on the top of the stack. Don't
963 generate the `swap' instructions if we're not actually going
965 if (is_nontrivial_conversion (value1
->type
, target
))
967 ax_simple (ax
, aop_swap
);
968 gen_conversion (ax
, value1
->type
, target
);
969 ax_simple (ax
, aop_swap
);
972 value1
->type
= value2
->type
= check_typedef (target
);
977 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
978 the value on the top of the stack, as described by VALUE. Assume
979 the value has integral type. */
981 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
983 const struct builtin_type
*builtin
= builtin_type (ax
->gdbarch
);
985 if (!type_wider_than (value
->type
, builtin
->builtin_int
))
987 gen_conversion (ax
, value
->type
, builtin
->builtin_int
);
988 value
->type
= builtin
->builtin_int
;
990 else if (!type_wider_than (value
->type
, builtin
->builtin_unsigned_int
))
992 gen_conversion (ax
, value
->type
, builtin
->builtin_unsigned_int
);
993 value
->type
= builtin
->builtin_unsigned_int
;
998 /* Generate code for a cast to TYPE. */
1000 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
1002 /* GCC does allow casts to yield lvalues, so this should be fixed
1003 before merging these changes into the trunk. */
1004 require_rvalue (ax
, value
);
1005 /* Dereference typedefs. */
1006 type
= check_typedef (type
);
1008 switch (TYPE_CODE (type
))
1012 case TYPE_CODE_RVALUE_REF
:
1013 /* It's implementation-defined, and I'll bet this is what GCC
1017 case TYPE_CODE_ARRAY
:
1018 case TYPE_CODE_STRUCT
:
1019 case TYPE_CODE_UNION
:
1020 case TYPE_CODE_FUNC
:
1021 error (_("Invalid type cast: intended type must be scalar."));
1023 case TYPE_CODE_ENUM
:
1024 case TYPE_CODE_BOOL
:
1025 /* We don't have to worry about the size of the value, because
1026 all our integral values are fully sign-extended, and when
1027 casting pointers we can do anything we like. Is there any
1028 way for us to know what GCC actually does with a cast like
1033 gen_conversion (ax
, value
->type
, type
);
1036 case TYPE_CODE_VOID
:
1037 /* We could pop the value, and rely on everyone else to check
1038 the type and notice that this value doesn't occupy a stack
1039 slot. But for now, leave the value on the stack, and
1040 preserve the "value == stack element" assumption. */
1044 error (_("Casts to requested type are not yet implemented."));
1052 /* Generating bytecode from GDB expressions: arithmetic */
1054 /* Scale the integer on the top of the stack by the size of the target
1055 of the pointer type TYPE. */
1057 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
1059 struct type
*element
= TYPE_TARGET_TYPE (type
);
1061 if (TYPE_LENGTH (element
) != 1)
1063 ax_const_l (ax
, TYPE_LENGTH (element
));
1069 /* Generate code for pointer arithmetic PTR + INT. */
1071 gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
1072 struct axs_value
*value1
, struct axs_value
*value2
)
1074 gdb_assert (pointer_type (value1
->type
));
1075 gdb_assert (TYPE_CODE (value2
->type
) == TYPE_CODE_INT
);
1077 gen_scale (ax
, aop_mul
, value1
->type
);
1078 ax_simple (ax
, aop_add
);
1079 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1080 value
->type
= value1
->type
;
1081 value
->kind
= axs_rvalue
;
1085 /* Generate code for pointer arithmetic PTR - INT. */
1087 gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
1088 struct axs_value
*value1
, struct axs_value
*value2
)
1090 gdb_assert (pointer_type (value1
->type
));
1091 gdb_assert (TYPE_CODE (value2
->type
) == TYPE_CODE_INT
);
1093 gen_scale (ax
, aop_mul
, value1
->type
);
1094 ax_simple (ax
, aop_sub
);
1095 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1096 value
->type
= value1
->type
;
1097 value
->kind
= axs_rvalue
;
1101 /* Generate code for pointer arithmetic PTR - PTR. */
1103 gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
1104 struct axs_value
*value1
, struct axs_value
*value2
,
1105 struct type
*result_type
)
1107 gdb_assert (pointer_type (value1
->type
));
1108 gdb_assert (pointer_type (value2
->type
));
1110 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1
->type
))
1111 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2
->type
)))
1113 First argument of `-' is a pointer, but second argument is neither\n\
1114 an integer nor a pointer of the same type."));
1116 ax_simple (ax
, aop_sub
);
1117 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1118 value
->type
= result_type
;
1119 value
->kind
= axs_rvalue
;
1123 gen_equal (struct agent_expr
*ax
, struct axs_value
*value
,
1124 struct axs_value
*value1
, struct axs_value
*value2
,
1125 struct type
*result_type
)
1127 if (pointer_type (value1
->type
) || pointer_type (value2
->type
))
1128 ax_simple (ax
, aop_equal
);
1130 gen_binop (ax
, value
, value1
, value2
,
1131 aop_equal
, aop_equal
, 0, "equal");
1132 value
->type
= result_type
;
1133 value
->kind
= axs_rvalue
;
1137 gen_less (struct agent_expr
*ax
, struct axs_value
*value
,
1138 struct axs_value
*value1
, struct axs_value
*value2
,
1139 struct type
*result_type
)
1141 if (pointer_type (value1
->type
) || pointer_type (value2
->type
))
1142 ax_simple (ax
, aop_less_unsigned
);
1144 gen_binop (ax
, value
, value1
, value2
,
1145 aop_less_signed
, aop_less_unsigned
, 0, "less than");
1146 value
->type
= result_type
;
1147 value
->kind
= axs_rvalue
;
1150 /* Generate code for a binary operator that doesn't do pointer magic.
1151 We set VALUE to describe the result value; we assume VALUE1 and
1152 VALUE2 describe the two operands, and that they've undergone the
1153 usual binary conversions. MAY_CARRY should be non-zero iff the
1154 result needs to be extended. NAME is the English name of the
1155 operator, used in error messages */
1157 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1158 struct axs_value
*value1
, struct axs_value
*value2
,
1159 enum agent_op op
, enum agent_op op_unsigned
,
1160 int may_carry
, const char *name
)
1162 /* We only handle INT op INT. */
1163 if ((TYPE_CODE (value1
->type
) != TYPE_CODE_INT
)
1164 || (TYPE_CODE (value2
->type
) != TYPE_CODE_INT
))
1165 error (_("Invalid combination of types in %s."), name
);
1168 TYPE_UNSIGNED (value1
->type
) ? op_unsigned
: op
);
1170 gen_extend (ax
, value1
->type
); /* catch overflow */
1171 value
->type
= value1
->type
;
1172 value
->kind
= axs_rvalue
;
1177 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
1178 struct type
*result_type
)
1180 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
1181 && TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1182 error (_("Invalid type of operand to `!'."));
1184 ax_simple (ax
, aop_log_not
);
1185 value
->type
= result_type
;
1190 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1192 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
)
1193 error (_("Invalid type of operand to `~'."));
1195 ax_simple (ax
, aop_bit_not
);
1196 gen_extend (ax
, value
->type
);
1201 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1203 /* Dereference the value on the top of the stack. */
1205 gen_deref (struct axs_value
*value
)
1207 /* The caller should check the type, because several operators use
1208 this, and we don't know what error message to generate. */
1209 if (!pointer_type (value
->type
))
1210 internal_error (__FILE__
, __LINE__
,
1211 _("gen_deref: expected a pointer"));
1213 /* We've got an rvalue now, which is a pointer. We want to yield an
1214 lvalue, whose address is exactly that pointer. So we don't
1215 actually emit any code; we just change the type from "Pointer to
1216 T" to "T", and mark the value as an lvalue in memory. Leave it
1217 to the consumer to actually dereference it. */
1218 value
->type
= check_typedef (TYPE_TARGET_TYPE (value
->type
));
1219 if (TYPE_CODE (value
->type
) == TYPE_CODE_VOID
)
1220 error (_("Attempt to dereference a generic pointer."));
1221 value
->kind
= ((TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1222 ? axs_rvalue
: axs_lvalue_memory
);
1226 /* Produce the address of the lvalue on the top of the stack. */
1228 gen_address_of (struct axs_value
*value
)
1230 /* Special case for taking the address of a function. The ANSI
1231 standard describes this as a special case, too, so this
1232 arrangement is not without motivation. */
1233 if (TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1234 /* The value's already an rvalue on the stack, so we just need to
1236 value
->type
= lookup_pointer_type (value
->type
);
1238 switch (value
->kind
)
1241 error (_("Operand of `&' is an rvalue, which has no address."));
1243 case axs_lvalue_register
:
1244 error (_("Operand of `&' is in a register, and has no address."));
1246 case axs_lvalue_memory
:
1247 value
->kind
= axs_rvalue
;
1248 value
->type
= lookup_pointer_type (value
->type
);
1253 /* Generate code to push the value of a bitfield of a structure whose
1254 address is on the top of the stack. START and END give the
1255 starting and one-past-ending *bit* numbers of the field within the
1258 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1259 struct type
*type
, int start
, int end
)
1261 /* Note that ops[i] fetches 8 << i bits. */
1262 static enum agent_op ops
[]
1263 = {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1264 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1266 /* We don't want to touch any byte that the bitfield doesn't
1267 actually occupy; we shouldn't make any accesses we're not
1268 explicitly permitted to. We rely here on the fact that the
1269 bytecode `ref' operators work on unaligned addresses.
1271 It takes some fancy footwork to get the stack to work the way
1272 we'd like. Say we're retrieving a bitfield that requires three
1273 fetches. Initially, the stack just contains the address:
1275 For the first fetch, we duplicate the address
1277 then add the byte offset, do the fetch, and shift and mask as
1278 needed, yielding a fragment of the value, properly aligned for
1279 the final bitwise or:
1281 then we swap, and repeat the process:
1282 frag1 addr --- address on top
1283 frag1 addr addr --- duplicate it
1284 frag1 addr frag2 --- get second fragment
1285 frag1 frag2 addr --- swap again
1286 frag1 frag2 frag3 --- get third fragment
1287 Notice that, since the third fragment is the last one, we don't
1288 bother duplicating the address this time. Now we have all the
1289 fragments on the stack, and we can simply `or' them together,
1290 yielding the final value of the bitfield. */
1292 /* The first and one-after-last bits in the field, but rounded down
1293 and up to byte boundaries. */
1294 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1295 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1299 /* current bit offset within the structure */
1302 /* The index in ops of the opcode we're considering. */
1305 /* The number of fragments we generated in the process. Probably
1306 equal to the number of `one' bits in bytesize, but who cares? */
1309 /* Dereference any typedefs. */
1310 type
= check_typedef (type
);
1312 /* Can we fetch the number of bits requested at all? */
1313 if ((end
- start
) > ((1 << num_ops
) * 8))
1314 internal_error (__FILE__
, __LINE__
,
1315 _("gen_bitfield_ref: bitfield too wide"));
1317 /* Note that we know here that we only need to try each opcode once.
1318 That may not be true on machines with weird byte sizes. */
1319 offset
= bound_start
;
1321 for (op
= num_ops
- 1; op
>= 0; op
--)
1323 /* number of bits that ops[op] would fetch */
1324 int op_size
= 8 << op
;
1326 /* The stack at this point, from bottom to top, contains zero or
1327 more fragments, then the address. */
1329 /* Does this fetch fit within the bitfield? */
1330 if (offset
+ op_size
<= bound_end
)
1332 /* Is this the last fragment? */
1333 int last_frag
= (offset
+ op_size
== bound_end
);
1336 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1338 /* Add the offset. */
1339 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1343 /* Record the area of memory we're about to fetch. */
1344 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1347 /* Perform the fetch. */
1348 ax_simple (ax
, ops
[op
]);
1350 /* Shift the bits we have to their proper position.
1351 gen_left_shift will generate right shifts when the operand
1354 A big-endian field diagram to ponder:
1355 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1356 +------++------++------++------++------++------++------++------+
1357 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1359 bit number 16 32 48 53
1360 These are bit numbers as supplied by GDB. Note that the
1361 bit numbers run from right to left once you've fetched the
1364 A little-endian field diagram to ponder:
1365 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1366 +------++------++------++------++------++------++------++------+
1367 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1369 bit number 48 32 16 4 0
1371 In both cases, the most significant end is on the left
1372 (i.e. normal numeric writing order), which means that you
1373 don't go crazy thinking about `left' and `right' shifts.
1375 We don't have to worry about masking yet:
1376 - If they contain garbage off the least significant end, then we
1377 must be looking at the low end of the field, and the right
1378 shift will wipe them out.
1379 - If they contain garbage off the most significant end, then we
1380 must be looking at the most significant end of the word, and
1381 the sign/zero extension will wipe them out.
1382 - If we're in the interior of the word, then there is no garbage
1383 on either end, because the ref operators zero-extend. */
1384 if (gdbarch_byte_order (ax
->gdbarch
) == BFD_ENDIAN_BIG
)
1385 gen_left_shift (ax
, end
- (offset
+ op_size
));
1387 gen_left_shift (ax
, offset
- start
);
1390 /* Bring the copy of the address up to the top. */
1391 ax_simple (ax
, aop_swap
);
1398 /* Generate enough bitwise `or' operations to combine all the
1399 fragments we left on the stack. */
1400 while (fragment_count
-- > 1)
1401 ax_simple (ax
, aop_bit_or
);
1403 /* Sign- or zero-extend the value as appropriate. */
1404 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1406 /* This is *not* an lvalue. Ugh. */
1407 value
->kind
= axs_rvalue
;
1411 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1412 is an accumulated offset (in bytes), will be nonzero for objects
1413 embedded in other objects, like C++ base classes. Behavior should
1414 generally follow value_primitive_field. */
1417 gen_primitive_field (struct agent_expr
*ax
, struct axs_value
*value
,
1418 int offset
, int fieldno
, struct type
*type
)
1420 /* Is this a bitfield? */
1421 if (TYPE_FIELD_PACKED (type
, fieldno
))
1422 gen_bitfield_ref (ax
, value
, TYPE_FIELD_TYPE (type
, fieldno
),
1423 (offset
* TARGET_CHAR_BIT
1424 + TYPE_FIELD_BITPOS (type
, fieldno
)),
1425 (offset
* TARGET_CHAR_BIT
1426 + TYPE_FIELD_BITPOS (type
, fieldno
)
1427 + TYPE_FIELD_BITSIZE (type
, fieldno
)));
1430 gen_offset (ax
, offset
1431 + TYPE_FIELD_BITPOS (type
, fieldno
) / TARGET_CHAR_BIT
);
1432 value
->kind
= axs_lvalue_memory
;
1433 value
->type
= TYPE_FIELD_TYPE (type
, fieldno
);
1437 /* Search for the given field in either the given type or one of its
1438 base classes. Return 1 if found, 0 if not. */
1441 gen_struct_ref_recursive (struct agent_expr
*ax
, struct axs_value
*value
,
1442 const char *field
, int offset
, struct type
*type
)
1445 int nbases
= TYPE_N_BASECLASSES (type
);
1447 type
= check_typedef (type
);
1449 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
1451 const char *this_name
= TYPE_FIELD_NAME (type
, i
);
1455 if (strcmp (field
, this_name
) == 0)
1457 /* Note that bytecodes for the struct's base (aka
1458 "this") will have been generated already, which will
1459 be unnecessary but not harmful if the static field is
1460 being handled as a global. */
1461 if (field_is_static (&TYPE_FIELD (type
, i
)))
1463 gen_static_field (ax
, value
, type
, i
);
1464 if (value
->optimized_out
)
1465 error (_("static field `%s' has been "
1466 "optimized out, cannot use"),
1471 gen_primitive_field (ax
, value
, offset
, i
, type
);
1474 #if 0 /* is this right? */
1475 if (this_name
[0] == '\0')
1476 internal_error (__FILE__
, __LINE__
,
1477 _("find_field: anonymous unions not supported"));
1482 /* Now scan through base classes recursively. */
1483 for (i
= 0; i
< nbases
; i
++)
1485 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1487 rslt
= gen_struct_ref_recursive (ax
, value
, field
,
1488 offset
+ TYPE_BASECLASS_BITPOS (type
, i
)
1495 /* Not found anywhere, flag so caller can complain. */
1499 /* Generate code to reference the member named FIELD of a structure or
1500 union. The top of the stack, as described by VALUE, should have
1501 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1502 the operator being compiled, and OPERAND_NAME is the kind of thing
1503 it operates on; we use them in error messages. */
1505 gen_struct_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1506 const char *field
, const char *operator_name
,
1507 const char *operand_name
)
1512 /* Follow pointers until we reach a non-pointer. These aren't the C
1513 semantics, but they're what the normal GDB evaluator does, so we
1514 should at least be consistent. */
1515 while (pointer_type (value
->type
))
1517 require_rvalue (ax
, value
);
1520 type
= check_typedef (value
->type
);
1522 /* This must yield a structure or a union. */
1523 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
1524 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
1525 error (_("The left operand of `%s' is not a %s."),
1526 operator_name
, operand_name
);
1528 /* And it must be in memory; we don't deal with structure rvalues,
1529 or structures living in registers. */
1530 if (value
->kind
!= axs_lvalue_memory
)
1531 error (_("Structure does not live in memory."));
1533 /* Search through fields and base classes recursively. */
1534 found
= gen_struct_ref_recursive (ax
, value
, field
, 0, type
);
1537 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1538 field
, TYPE_TAG_NAME (type
));
1542 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1543 const struct type
*curtype
, char *name
);
1545 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1546 const struct type
*curtype
, char *name
);
1549 gen_static_field (struct agent_expr
*ax
, struct axs_value
*value
,
1550 struct type
*type
, int fieldno
)
1552 if (TYPE_FIELD_LOC_KIND (type
, fieldno
) == FIELD_LOC_KIND_PHYSADDR
)
1554 ax_const_l (ax
, TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
1555 value
->kind
= axs_lvalue_memory
;
1556 value
->type
= TYPE_FIELD_TYPE (type
, fieldno
);
1557 value
->optimized_out
= 0;
1561 const char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1562 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0).symbol
;
1566 gen_var_ref (ax
, value
, sym
);
1568 /* Don't error if the value was optimized out, we may be
1569 scanning all static fields and just want to pass over this
1570 and continue with the rest. */
1574 /* Silently assume this was optimized out; class printing
1575 will let the user know why the data is missing. */
1576 value
->optimized_out
= 1;
1582 gen_struct_elt_for_reference (struct agent_expr
*ax
, struct axs_value
*value
,
1583 struct type
*type
, char *fieldname
)
1585 struct type
*t
= type
;
1588 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1589 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1590 internal_error (__FILE__
, __LINE__
,
1591 _("non-aggregate type to gen_struct_elt_for_reference"));
1593 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1595 const char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1597 if (t_field_name
&& strcmp (t_field_name
, fieldname
) == 0)
1599 if (field_is_static (&TYPE_FIELD (t
, i
)))
1601 gen_static_field (ax
, value
, t
, i
);
1602 if (value
->optimized_out
)
1603 error (_("static field `%s' has been "
1604 "optimized out, cannot use"),
1608 if (TYPE_FIELD_PACKED (t
, i
))
1609 error (_("pointers to bitfield members not allowed"));
1611 /* FIXME we need a way to do "want_address" equivalent */
1613 error (_("Cannot reference non-static field \"%s\""), fieldname
);
1617 /* FIXME add other scoped-reference cases here */
1619 /* Do a last-ditch lookup. */
1620 return gen_maybe_namespace_elt (ax
, value
, type
, fieldname
);
1623 /* C++: Return the member NAME of the namespace given by the type
1627 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1628 const struct type
*curtype
, char *name
)
1630 int found
= gen_maybe_namespace_elt (ax
, value
, curtype
, name
);
1633 error (_("No symbol \"%s\" in namespace \"%s\"."),
1634 name
, TYPE_TAG_NAME (curtype
));
1639 /* A helper function used by value_namespace_elt and
1640 value_struct_elt_for_reference. It looks up NAME inside the
1641 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1642 is a class and NAME refers to a type in CURTYPE itself (as opposed
1643 to, say, some base class of CURTYPE). */
1646 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1647 const struct type
*curtype
, char *name
)
1649 const char *namespace_name
= TYPE_TAG_NAME (curtype
);
1650 struct block_symbol sym
;
1652 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
1653 block_for_pc (ax
->scope
),
1656 if (sym
.symbol
== NULL
)
1659 gen_var_ref (ax
, value
, sym
.symbol
);
1661 if (value
->optimized_out
)
1662 error (_("`%s' has been optimized out, cannot use"),
1663 SYMBOL_PRINT_NAME (sym
.symbol
));
1670 gen_aggregate_elt_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1671 struct type
*type
, char *field
)
1673 switch (TYPE_CODE (type
))
1675 case TYPE_CODE_STRUCT
:
1676 case TYPE_CODE_UNION
:
1677 return gen_struct_elt_for_reference (ax
, value
, type
, field
);
1679 case TYPE_CODE_NAMESPACE
:
1680 return gen_namespace_elt (ax
, value
, type
, field
);
1683 internal_error (__FILE__
, __LINE__
,
1684 _("non-aggregate type in gen_aggregate_elt_ref"));
1690 /* Generate code for GDB's magical `repeat' operator.
1691 LVALUE @ INT creates an array INT elements long, and whose elements
1692 have the same type as LVALUE, located in memory so that LVALUE is
1693 its first element. For example, argv[0]@argc gives you the array
1694 of command-line arguments.
1696 Unfortunately, because we have to know the types before we actually
1697 have a value for the expression, we can't implement this perfectly
1698 without changing the type system, having values that occupy two
1699 stack slots, doing weird things with sizeof, etc. So we require
1700 the right operand to be a constant expression. */
1702 gen_repeat (struct expression
*exp
, union exp_element
**pc
,
1703 struct agent_expr
*ax
, struct axs_value
*value
)
1705 struct axs_value value1
;
1707 /* We don't want to turn this into an rvalue, so no conversions
1709 gen_expr (exp
, pc
, ax
, &value1
);
1710 if (value1
.kind
!= axs_lvalue_memory
)
1711 error (_("Left operand of `@' must be an object in memory."));
1713 /* Evaluate the length; it had better be a constant. */
1715 struct value
*v
= const_expr (pc
);
1719 error (_("Right operand of `@' must be a "
1720 "constant, in agent expressions."));
1721 if (TYPE_CODE (value_type (v
)) != TYPE_CODE_INT
)
1722 error (_("Right operand of `@' must be an integer."));
1723 length
= value_as_long (v
);
1725 error (_("Right operand of `@' must be positive."));
1727 /* The top of the stack is already the address of the object, so
1728 all we need to do is frob the type of the lvalue. */
1730 /* FIXME-type-allocation: need a way to free this type when we are
1733 = lookup_array_range_type (value1
.type
, 0, length
- 1);
1735 value
->kind
= axs_lvalue_memory
;
1736 value
->type
= array
;
1742 /* Emit code for the `sizeof' operator.
1743 *PC should point at the start of the operand expression; we advance it
1744 to the first instruction after the operand. */
1746 gen_sizeof (struct expression
*exp
, union exp_element
**pc
,
1747 struct agent_expr
*ax
, struct axs_value
*value
,
1748 struct type
*size_type
)
1750 /* We don't care about the value of the operand expression; we only
1751 care about its type. However, in the current arrangement, the
1752 only way to find an expression's type is to generate code for it.
1753 So we generate code for the operand, and then throw it away,
1754 replacing it with code that simply pushes its size. */
1755 int start
= ax
->len
;
1757 gen_expr (exp
, pc
, ax
, value
);
1759 /* Throw away the code we just generated. */
1762 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
1763 value
->kind
= axs_rvalue
;
1764 value
->type
= size_type
;
1768 /* Generating bytecode from GDB expressions: general recursive thingy */
1771 /* A gen_expr function written by a Gen-X'er guy.
1772 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1774 gen_expr (struct expression
*exp
, union exp_element
**pc
,
1775 struct agent_expr
*ax
, struct axs_value
*value
)
1777 /* Used to hold the descriptions of operand expressions. */
1778 struct axs_value value1
, value2
, value3
;
1779 enum exp_opcode op
= (*pc
)[0].opcode
, op2
;
1780 int if1
, go1
, if2
, go2
, end
;
1781 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
1783 /* If we're looking at a constant expression, just push its value. */
1785 struct value
*v
= maybe_const_expr (pc
);
1789 ax_const_l (ax
, value_as_long (v
));
1790 value
->kind
= axs_rvalue
;
1791 value
->type
= check_typedef (value_type (v
));
1796 /* Otherwise, go ahead and generate code for it. */
1799 /* Binary arithmetic operators. */
1807 case BINOP_SUBSCRIPT
:
1808 case BINOP_BITWISE_AND
:
1809 case BINOP_BITWISE_IOR
:
1810 case BINOP_BITWISE_XOR
:
1812 case BINOP_NOTEQUAL
:
1818 gen_expr (exp
, pc
, ax
, &value1
);
1819 gen_usual_unary (ax
, &value1
);
1820 gen_expr_binop_rest (exp
, op
, pc
, ax
, value
, &value1
, &value2
);
1823 case BINOP_LOGICAL_AND
:
1825 /* Generate the obvious sequence of tests and jumps. */
1826 gen_expr (exp
, pc
, ax
, &value1
);
1827 gen_usual_unary (ax
, &value1
);
1828 if1
= ax_goto (ax
, aop_if_goto
);
1829 go1
= ax_goto (ax
, aop_goto
);
1830 ax_label (ax
, if1
, ax
->len
);
1831 gen_expr (exp
, pc
, ax
, &value2
);
1832 gen_usual_unary (ax
, &value2
);
1833 if2
= ax_goto (ax
, aop_if_goto
);
1834 go2
= ax_goto (ax
, aop_goto
);
1835 ax_label (ax
, if2
, ax
->len
);
1837 end
= ax_goto (ax
, aop_goto
);
1838 ax_label (ax
, go1
, ax
->len
);
1839 ax_label (ax
, go2
, ax
->len
);
1841 ax_label (ax
, end
, ax
->len
);
1842 value
->kind
= axs_rvalue
;
1843 value
->type
= int_type
;
1846 case BINOP_LOGICAL_OR
:
1848 /* Generate the obvious sequence of tests and jumps. */
1849 gen_expr (exp
, pc
, ax
, &value1
);
1850 gen_usual_unary (ax
, &value1
);
1851 if1
= ax_goto (ax
, aop_if_goto
);
1852 gen_expr (exp
, pc
, ax
, &value2
);
1853 gen_usual_unary (ax
, &value2
);
1854 if2
= ax_goto (ax
, aop_if_goto
);
1856 end
= ax_goto (ax
, aop_goto
);
1857 ax_label (ax
, if1
, ax
->len
);
1858 ax_label (ax
, if2
, ax
->len
);
1860 ax_label (ax
, end
, ax
->len
);
1861 value
->kind
= axs_rvalue
;
1862 value
->type
= int_type
;
1867 gen_expr (exp
, pc
, ax
, &value1
);
1868 gen_usual_unary (ax
, &value1
);
1869 /* For (A ? B : C), it's easiest to generate subexpression
1870 bytecodes in order, but if_goto jumps on true, so we invert
1871 the sense of A. Then we can do B by dropping through, and
1873 gen_logical_not (ax
, &value1
, int_type
);
1874 if1
= ax_goto (ax
, aop_if_goto
);
1875 gen_expr (exp
, pc
, ax
, &value2
);
1876 gen_usual_unary (ax
, &value2
);
1877 end
= ax_goto (ax
, aop_goto
);
1878 ax_label (ax
, if1
, ax
->len
);
1879 gen_expr (exp
, pc
, ax
, &value3
);
1880 gen_usual_unary (ax
, &value3
);
1881 ax_label (ax
, end
, ax
->len
);
1882 /* This is arbitary - what if B and C are incompatible types? */
1883 value
->type
= value2
.type
;
1884 value
->kind
= value2
.kind
;
1889 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1891 char *name
= internalvar_name ((*pc
)[1].internalvar
);
1892 struct trace_state_variable
*tsv
;
1895 gen_expr (exp
, pc
, ax
, value
);
1896 tsv
= find_trace_state_variable (name
);
1899 ax_tsv (ax
, aop_setv
, tsv
->number
);
1901 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1904 error (_("$%s is not a trace state variable, "
1905 "may not assign to it"), name
);
1908 error (_("May only assign to trace state variables"));
1911 case BINOP_ASSIGN_MODIFY
:
1913 op2
= (*pc
)[0].opcode
;
1916 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1918 char *name
= internalvar_name ((*pc
)[1].internalvar
);
1919 struct trace_state_variable
*tsv
;
1922 tsv
= find_trace_state_variable (name
);
1925 /* The tsv will be the left half of the binary operation. */
1926 ax_tsv (ax
, aop_getv
, tsv
->number
);
1928 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1929 /* Trace state variables are always 64-bit integers. */
1930 value1
.kind
= axs_rvalue
;
1931 value1
.type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
1932 /* Now do right half of expression. */
1933 gen_expr_binop_rest (exp
, op2
, pc
, ax
, value
, &value1
, &value2
);
1934 /* We have a result of the binary op, set the tsv. */
1935 ax_tsv (ax
, aop_setv
, tsv
->number
);
1937 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1940 error (_("$%s is not a trace state variable, "
1941 "may not assign to it"), name
);
1944 error (_("May only assign to trace state variables"));
1947 /* Note that we need to be a little subtle about generating code
1948 for comma. In C, we can do some optimizations here because
1949 we know the left operand is only being evaluated for effect.
1950 However, if the tracing kludge is in effect, then we always
1951 need to evaluate the left hand side fully, so that all the
1952 variables it mentions get traced. */
1955 gen_expr (exp
, pc
, ax
, &value1
);
1956 /* Don't just dispose of the left operand. We might be tracing,
1957 in which case we want to emit code to trace it if it's an
1959 gen_traced_pop (ax
, &value1
);
1960 gen_expr (exp
, pc
, ax
, value
);
1961 /* It's the consumer's responsibility to trace the right operand. */
1964 case OP_LONG
: /* some integer constant */
1966 struct type
*type
= (*pc
)[1].type
;
1967 LONGEST k
= (*pc
)[2].longconst
;
1970 gen_int_literal (ax
, value
, k
, type
);
1975 gen_var_ref (ax
, value
, (*pc
)[2].symbol
);
1977 if (value
->optimized_out
)
1978 error (_("`%s' has been optimized out, cannot use"),
1979 SYMBOL_PRINT_NAME ((*pc
)[2].symbol
));
1984 case OP_VAR_MSYM_VALUE
:
1985 gen_msym_var_ref (ax
, value
, (*pc
)[2].msymbol
, (*pc
)[1].objfile
);
1991 const char *name
= &(*pc
)[2].string
;
1994 (*pc
) += 4 + BYTES_TO_EXP_ELEM ((*pc
)[1].longconst
+ 1);
1995 reg
= user_reg_map_name_to_regnum (ax
->gdbarch
, name
, strlen (name
));
1997 internal_error (__FILE__
, __LINE__
,
1998 _("Register $%s not available"), name
);
1999 /* No support for tracing user registers yet. */
2000 if (reg
>= gdbarch_num_regs (ax
->gdbarch
)
2001 + gdbarch_num_pseudo_regs (ax
->gdbarch
))
2002 error (_("'%s' is a user-register; "
2003 "GDB cannot yet trace user-register contents."),
2005 value
->kind
= axs_lvalue_register
;
2007 value
->type
= register_type (ax
->gdbarch
, reg
);
2011 case OP_INTERNALVAR
:
2013 struct internalvar
*var
= (*pc
)[1].internalvar
;
2014 const char *name
= internalvar_name (var
);
2015 struct trace_state_variable
*tsv
;
2018 tsv
= find_trace_state_variable (name
);
2021 ax_tsv (ax
, aop_getv
, tsv
->number
);
2023 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2024 /* Trace state variables are always 64-bit integers. */
2025 value
->kind
= axs_rvalue
;
2026 value
->type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
2028 else if (! compile_internalvar_to_ax (var
, ax
, value
))
2029 error (_("$%s is not a trace state variable; GDB agent "
2030 "expressions cannot use convenience variables."), name
);
2034 /* Weirdo operator: see comments for gen_repeat for details. */
2036 /* Note that gen_repeat handles its own argument evaluation. */
2038 gen_repeat (exp
, pc
, ax
, value
);
2043 struct type
*type
= (*pc
)[1].type
;
2046 gen_expr (exp
, pc
, ax
, value
);
2047 gen_cast (ax
, value
, type
);
2051 case UNOP_CAST_TYPE
:
2058 offset
= *pc
- exp
->elts
;
2059 val
= evaluate_subexp (NULL
, exp
, &offset
, EVAL_AVOID_SIDE_EFFECTS
);
2060 type
= value_type (val
);
2061 *pc
= &exp
->elts
[offset
];
2063 gen_expr (exp
, pc
, ax
, value
);
2064 gen_cast (ax
, value
, type
);
2070 struct type
*type
= check_typedef ((*pc
)[1].type
);
2073 gen_expr (exp
, pc
, ax
, value
);
2075 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2076 already have the right value on the stack. For
2077 axs_lvalue_register, we must convert. */
2078 if (value
->kind
== axs_lvalue_register
)
2079 require_rvalue (ax
, value
);
2082 value
->kind
= axs_lvalue_memory
;
2086 case UNOP_MEMVAL_TYPE
:
2093 offset
= *pc
- exp
->elts
;
2094 val
= evaluate_subexp (NULL
, exp
, &offset
, EVAL_AVOID_SIDE_EFFECTS
);
2095 type
= value_type (val
);
2096 *pc
= &exp
->elts
[offset
];
2098 gen_expr (exp
, pc
, ax
, value
);
2100 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2101 already have the right value on the stack. For
2102 axs_lvalue_register, we must convert. */
2103 if (value
->kind
== axs_lvalue_register
)
2104 require_rvalue (ax
, value
);
2107 value
->kind
= axs_lvalue_memory
;
2113 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2114 gen_expr (exp
, pc
, ax
, value
);
2115 gen_usual_unary (ax
, value
);
2120 /* -FOO is equivalent to 0 - FOO. */
2121 gen_int_literal (ax
, &value1
, 0,
2122 builtin_type (ax
->gdbarch
)->builtin_int
);
2123 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
2124 gen_expr (exp
, pc
, ax
, &value2
);
2125 gen_usual_unary (ax
, &value2
);
2126 gen_usual_arithmetic (ax
, &value1
, &value2
);
2127 gen_binop (ax
, value
, &value1
, &value2
, aop_sub
, aop_sub
, 1, "negation");
2130 case UNOP_LOGICAL_NOT
:
2132 gen_expr (exp
, pc
, ax
, value
);
2133 gen_usual_unary (ax
, value
);
2134 gen_logical_not (ax
, value
, int_type
);
2137 case UNOP_COMPLEMENT
:
2139 gen_expr (exp
, pc
, ax
, value
);
2140 gen_usual_unary (ax
, value
);
2141 gen_integral_promotions (ax
, value
);
2142 gen_complement (ax
, value
);
2147 gen_expr (exp
, pc
, ax
, value
);
2148 gen_usual_unary (ax
, value
);
2149 if (!pointer_type (value
->type
))
2150 error (_("Argument of unary `*' is not a pointer."));
2156 gen_expr (exp
, pc
, ax
, value
);
2157 gen_address_of (value
);
2162 /* Notice that gen_sizeof handles its own operand, unlike most
2163 of the other unary operator functions. This is because we
2164 have to throw away the code we generate. */
2165 gen_sizeof (exp
, pc
, ax
, value
,
2166 builtin_type (ax
->gdbarch
)->builtin_int
);
2169 case STRUCTOP_STRUCT
:
2172 int length
= (*pc
)[1].longconst
;
2173 char *name
= &(*pc
)[2].string
;
2175 (*pc
) += 4 + BYTES_TO_EXP_ELEM (length
+ 1);
2176 gen_expr (exp
, pc
, ax
, value
);
2177 if (op
== STRUCTOP_STRUCT
)
2178 gen_struct_ref (ax
, value
, name
, ".", "structure or union");
2179 else if (op
== STRUCTOP_PTR
)
2180 gen_struct_ref (ax
, value
, name
, "->",
2181 "pointer to a structure or union");
2183 /* If this `if' chain doesn't handle it, then the case list
2184 shouldn't mention it, and we shouldn't be here. */
2185 internal_error (__FILE__
, __LINE__
,
2186 _("gen_expr: unhandled struct case"));
2192 struct symbol
*sym
, *func
;
2193 const struct block
*b
;
2194 const struct language_defn
*lang
;
2196 b
= block_for_pc (ax
->scope
);
2197 func
= block_linkage_function (b
);
2198 lang
= language_def (SYMBOL_LANGUAGE (func
));
2200 sym
= lookup_language_this (lang
, b
).symbol
;
2202 error (_("no `%s' found"), lang
->la_name_of_this
);
2204 gen_var_ref (ax
, value
, sym
);
2206 if (value
->optimized_out
)
2207 error (_("`%s' has been optimized out, cannot use"),
2208 SYMBOL_PRINT_NAME (sym
));
2216 struct type
*type
= (*pc
)[1].type
;
2217 int length
= longest_to_int ((*pc
)[2].longconst
);
2218 char *name
= &(*pc
)[3].string
;
2221 found
= gen_aggregate_elt_ref (ax
, value
, type
, name
);
2223 error (_("There is no field named %s"), name
);
2224 (*pc
) += 5 + BYTES_TO_EXP_ELEM (length
+ 1);
2231 error (_("Attempt to use a type name as an expression."));
2234 error (_("Unsupported operator %s (%d) in expression."),
2235 op_name (exp
, op
), op
);
2239 /* This handles the middle-to-right-side of code generation for binary
2240 expressions, which is shared between regular binary operations and
2241 assign-modify (+= and friends) expressions. */
2244 gen_expr_binop_rest (struct expression
*exp
,
2245 enum exp_opcode op
, union exp_element
**pc
,
2246 struct agent_expr
*ax
, struct axs_value
*value
,
2247 struct axs_value
*value1
, struct axs_value
*value2
)
2249 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2251 gen_expr (exp
, pc
, ax
, value2
);
2252 gen_usual_unary (ax
, value2
);
2253 gen_usual_arithmetic (ax
, value1
, value2
);
2257 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
2258 && pointer_type (value2
->type
))
2260 /* Swap the values and proceed normally. */
2261 ax_simple (ax
, aop_swap
);
2262 gen_ptradd (ax
, value
, value2
, value1
);
2264 else if (pointer_type (value1
->type
)
2265 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
2266 gen_ptradd (ax
, value
, value1
, value2
);
2268 gen_binop (ax
, value
, value1
, value2
,
2269 aop_add
, aop_add
, 1, "addition");
2272 if (pointer_type (value1
->type
)
2273 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
2274 gen_ptrsub (ax
,value
, value1
, value2
);
2275 else if (pointer_type (value1
->type
)
2276 && pointer_type (value2
->type
))
2277 /* FIXME --- result type should be ptrdiff_t */
2278 gen_ptrdiff (ax
, value
, value1
, value2
,
2279 builtin_type (ax
->gdbarch
)->builtin_long
);
2281 gen_binop (ax
, value
, value1
, value2
,
2282 aop_sub
, aop_sub
, 1, "subtraction");
2285 gen_binop (ax
, value
, value1
, value2
,
2286 aop_mul
, aop_mul
, 1, "multiplication");
2289 gen_binop (ax
, value
, value1
, value2
,
2290 aop_div_signed
, aop_div_unsigned
, 1, "division");
2293 gen_binop (ax
, value
, value1
, value2
,
2294 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
2297 gen_binop (ax
, value
, value1
, value2
,
2298 aop_lsh
, aop_lsh
, 1, "left shift");
2301 gen_binop (ax
, value
, value1
, value2
,
2302 aop_rsh_signed
, aop_rsh_unsigned
, 1, "right shift");
2304 case BINOP_SUBSCRIPT
:
2308 if (binop_types_user_defined_p (op
, value1
->type
, value2
->type
))
2310 error (_("cannot subscript requested type: "
2311 "cannot call user defined functions"));
2315 /* If the user attempts to subscript something that is not
2316 an array or pointer type (like a plain int variable for
2317 example), then report this as an error. */
2318 type
= check_typedef (value1
->type
);
2319 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
2320 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
2322 if (TYPE_NAME (type
))
2323 error (_("cannot subscript something of type `%s'"),
2326 error (_("cannot subscript requested type"));
2330 if (!is_integral_type (value2
->type
))
2331 error (_("Argument to arithmetic operation "
2332 "not a number or boolean."));
2334 gen_ptradd (ax
, value
, value1
, value2
);
2338 case BINOP_BITWISE_AND
:
2339 gen_binop (ax
, value
, value1
, value2
,
2340 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
2343 case BINOP_BITWISE_IOR
:
2344 gen_binop (ax
, value
, value1
, value2
,
2345 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
2348 case BINOP_BITWISE_XOR
:
2349 gen_binop (ax
, value
, value1
, value2
,
2350 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
2354 gen_equal (ax
, value
, value1
, value2
, int_type
);
2357 case BINOP_NOTEQUAL
:
2358 gen_equal (ax
, value
, value1
, value2
, int_type
);
2359 gen_logical_not (ax
, value
, int_type
);
2363 gen_less (ax
, value
, value1
, value2
, int_type
);
2367 ax_simple (ax
, aop_swap
);
2368 gen_less (ax
, value
, value1
, value2
, int_type
);
2372 ax_simple (ax
, aop_swap
);
2373 gen_less (ax
, value
, value1
, value2
, int_type
);
2374 gen_logical_not (ax
, value
, int_type
);
2378 gen_less (ax
, value
, value1
, value2
, int_type
);
2379 gen_logical_not (ax
, value
, int_type
);
2383 /* We should only list operators in the outer case statement
2384 that we actually handle in the inner case statement. */
2385 internal_error (__FILE__
, __LINE__
,
2386 _("gen_expr: op case sets don't match"));
2391 /* Given a single variable and a scope, generate bytecodes to trace
2392 its value. This is for use in situations where we have only a
2393 variable's name, and no parsed expression; for instance, when the
2394 name comes from a list of local variables of a function. */
2397 gen_trace_for_var (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2398 struct symbol
*var
, int trace_string
)
2400 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2401 struct axs_value value
;
2404 ax
->trace_string
= trace_string
;
2405 gen_var_ref (ax
.get (), &value
, var
);
2407 /* If there is no actual variable to trace, flag it by returning
2408 an empty agent expression. */
2409 if (value
.optimized_out
)
2410 return agent_expr_up ();
2412 /* Make sure we record the final object, and get rid of it. */
2413 gen_traced_pop (ax
.get (), &value
);
2415 /* Oh, and terminate. */
2416 ax_simple (ax
.get (), aop_end
);
2421 /* Generating bytecode from GDB expressions: driver */
2423 /* Given a GDB expression EXPR, return bytecode to trace its value.
2424 The result will use the `trace' and `trace_quick' bytecodes to
2425 record the value of all memory touched by the expression. The
2426 caller can then use the ax_reqs function to discover which
2427 registers it relies upon. */
2430 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
,
2433 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2434 union exp_element
*pc
;
2435 struct axs_value value
;
2439 ax
->trace_string
= trace_string
;
2440 value
.optimized_out
= 0;
2441 gen_expr (expr
, &pc
, ax
.get (), &value
);
2443 /* Make sure we record the final object, and get rid of it. */
2444 gen_traced_pop (ax
.get (), &value
);
2446 /* Oh, and terminate. */
2447 ax_simple (ax
.get (), aop_end
);
2452 /* Given a GDB expression EXPR, return a bytecode sequence that will
2453 evaluate and return a result. The bytecodes will do a direct
2454 evaluation, using the current data on the target, rather than
2455 recording blocks of memory and registers for later use, as
2456 gen_trace_for_expr does. The generated bytecode sequence leaves
2457 the result of expression evaluation on the top of the stack. */
2460 gen_eval_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2462 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2463 union exp_element
*pc
;
2464 struct axs_value value
;
2468 value
.optimized_out
= 0;
2469 gen_expr (expr
, &pc
, ax
.get (), &value
);
2471 require_rvalue (ax
.get (), &value
);
2473 /* Oh, and terminate. */
2474 ax_simple (ax
.get (), aop_end
);
2480 gen_trace_for_return_address (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2483 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2484 struct axs_value value
;
2487 ax
->trace_string
= trace_string
;
2489 gdbarch_gen_return_address (gdbarch
, ax
.get (), &value
, scope
);
2491 /* Make sure we record the final object, and get rid of it. */
2492 gen_traced_pop (ax
.get (), &value
);
2494 /* Oh, and terminate. */
2495 ax_simple (ax
.get (), aop_end
);
2500 /* Given a collection of printf-style arguments, generate code to
2501 evaluate the arguments and pass everything to a special
2505 gen_printf (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2506 CORE_ADDR function
, LONGEST channel
,
2507 const char *format
, int fmtlen
,
2508 struct format_piece
*frags
,
2509 int nargs
, struct expression
**exprs
)
2511 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2512 union exp_element
*pc
;
2513 struct axs_value value
;
2516 /* We're computing values, not doing side effects. */
2519 /* Evaluate and push the args on the stack in reverse order,
2520 for simplicity of collecting them on the target side. */
2521 for (tem
= nargs
- 1; tem
>= 0; --tem
)
2523 pc
= exprs
[tem
]->elts
;
2524 value
.optimized_out
= 0;
2525 gen_expr (exprs
[tem
], &pc
, ax
.get (), &value
);
2526 require_rvalue (ax
.get (), &value
);
2529 /* Push function and channel. */
2530 ax_const_l (ax
.get (), channel
);
2531 ax_const_l (ax
.get (), function
);
2533 /* Issue the printf bytecode proper. */
2534 ax_simple (ax
.get (), aop_printf
);
2535 ax_raw_byte (ax
.get (), nargs
);
2536 ax_string (ax
.get (), format
, fmtlen
);
2538 /* And terminate. */
2539 ax_simple (ax
.get (), aop_end
);
2545 agent_eval_command_one (const char *exp
, int eval
, CORE_ADDR pc
)
2548 int trace_string
= 0;
2553 exp
= decode_agent_options (exp
, &trace_string
);
2556 agent_expr_up agent
;
2559 if (!eval
&& strcmp (arg
, "$_ret") == 0)
2561 agent
= gen_trace_for_return_address (pc
, get_current_arch (),
2566 expression_up expr
= parse_exp_1 (&arg
, pc
, block_for_pc (pc
), 0);
2570 gdb_assert (trace_string
== 0);
2571 agent
= gen_eval_for_expr (pc
, expr
.get ());
2574 agent
= gen_trace_for_expr (pc
, expr
.get (), trace_string
);
2577 ax_reqs (agent
.get ());
2578 ax_print (gdb_stdout
, agent
.get ());
2580 /* It would be nice to call ax_reqs here to gather some general info
2581 about the expression, and then print out the result. */
2587 agent_command_1 (char *exp
, int eval
)
2589 /* We don't deal with overlay debugging at the moment. We need to
2590 think more carefully about this. If you copy this code into
2591 another command, change the error message; the user shouldn't
2592 have to know anything about agent expressions. */
2593 if (overlay_debugging
)
2594 error (_("GDB can't do agent expression translation with overlays."));
2597 error_no_arg (_("expression to translate"));
2599 if (check_for_argument (&exp
, "-at", sizeof ("-at") - 1))
2601 struct linespec_result canonical
;
2603 exp
= skip_spaces (exp
);
2605 event_location_up location
= new_linespec_location (&exp
);
2606 decode_line_full (location
.get (), DECODE_LINE_FUNFIRSTLINE
, NULL
,
2607 (struct symtab
*) NULL
, 0, &canonical
,
2609 exp
= skip_spaces (exp
);
2613 exp
= skip_spaces (exp
);
2615 for (const auto &lsal
: canonical
.lsals
)
2616 for (const auto &sal
: lsal
.sals
)
2617 agent_eval_command_one (exp
, eval
, sal
.pc
);
2620 agent_eval_command_one (exp
, eval
, get_frame_pc (get_current_frame ()));
2626 agent_command (char *exp
, int from_tty
)
2628 agent_command_1 (exp
, 0);
2631 /* Parse the given expression, compile it into an agent expression
2632 that does direct evaluation, and display the resulting
2636 agent_eval_command (char *exp
, int from_tty
)
2638 agent_command_1 (exp
, 1);
2641 /* Parse the given expression, compile it into an agent expression
2642 that does a printf, and display the resulting expression. */
2645 maint_agent_printf_command (char *exp
, int from_tty
)
2647 struct cleanup
*old_chain
= 0;
2648 struct expression
*argvec
[100];
2649 struct frame_info
*fi
= get_current_frame (); /* need current scope */
2650 const char *cmdrest
;
2651 const char *format_start
, *format_end
;
2652 struct format_piece
*fpieces
;
2655 /* We don't deal with overlay debugging at the moment. We need to
2656 think more carefully about this. If you copy this code into
2657 another command, change the error message; the user shouldn't
2658 have to know anything about agent expressions. */
2659 if (overlay_debugging
)
2660 error (_("GDB can't do agent expression translation with overlays."));
2663 error_no_arg (_("expression to translate"));
2667 cmdrest
= skip_spaces_const (cmdrest
);
2669 if (*cmdrest
++ != '"')
2670 error (_("Must start with a format string."));
2672 format_start
= cmdrest
;
2674 fpieces
= parse_format_string (&cmdrest
);
2676 old_chain
= make_cleanup (free_format_pieces_cleanup
, &fpieces
);
2678 format_end
= cmdrest
;
2680 if (*cmdrest
++ != '"')
2681 error (_("Bad format string, non-terminated '\"'."));
2683 cmdrest
= skip_spaces_const (cmdrest
);
2685 if (*cmdrest
!= ',' && *cmdrest
!= 0)
2686 error (_("Invalid argument syntax"));
2688 if (*cmdrest
== ',')
2690 cmdrest
= skip_spaces_const (cmdrest
);
2693 while (*cmdrest
!= '\0')
2698 expression_up expr
= parse_exp_1 (&cmd1
, 0, (struct block
*) 0, 1);
2699 argvec
[nargs
] = expr
.release ();
2702 if (*cmdrest
== ',')
2704 /* else complain? */
2708 agent_expr_up agent
= gen_printf (get_frame_pc (fi
), get_current_arch (),
2710 format_start
, format_end
- format_start
,
2711 fpieces
, nargs
, argvec
);
2712 ax_reqs (agent
.get ());
2713 ax_print (gdb_stdout
, agent
.get ());
2715 /* It would be nice to call ax_reqs here to gather some general info
2716 about the expression, and then print out the result. */
2718 do_cleanups (old_chain
);
2723 /* Initialization code. */
2725 void _initialize_ax_gdb (void);
2727 _initialize_ax_gdb (void)
2729 add_cmd ("agent", class_maintenance
, agent_command
,
2731 Translate an expression into remote agent bytecode for tracing.\n\
2732 Usage: maint agent [-at location,] EXPRESSION\n\
2733 If -at is given, generate remote agent bytecode for this location.\n\
2734 If not, generate remote agent bytecode for current frame pc address."),
2737 add_cmd ("agent-eval", class_maintenance
, agent_eval_command
,
2739 Translate an expression into remote agent bytecode for evaluation.\n\
2740 Usage: maint agent-eval [-at location,] EXPRESSION\n\
2741 If -at is given, generate remote agent bytecode for this location.\n\
2742 If not, generate remote agent bytecode for current frame pc address."),
2745 add_cmd ("agent-printf", class_maintenance
, maint_agent_printf_command
,
2746 _("Translate an expression into remote "
2747 "agent bytecode for evaluation and display the bytecodes."),