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
));
739 /* Generating bytecode from GDB expressions: literals */
742 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
746 value
->kind
= axs_rvalue
;
747 value
->type
= check_typedef (type
);
752 /* Generating bytecode from GDB expressions: unary conversions, casts */
754 /* Take what's on the top of the stack (as described by VALUE), and
755 try to make an rvalue out of it. Signal an error if we can't do
758 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
760 /* Only deal with scalars, structs and such may be too large
761 to fit in a stack entry. */
762 value
->type
= check_typedef (value
->type
);
763 if (TYPE_CODE (value
->type
) == TYPE_CODE_ARRAY
764 || TYPE_CODE (value
->type
) == TYPE_CODE_STRUCT
765 || TYPE_CODE (value
->type
) == TYPE_CODE_UNION
766 || TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
767 error (_("Value not scalar: cannot be an rvalue."));
772 /* It's already an rvalue. */
775 case axs_lvalue_memory
:
776 /* The top of stack is the address of the object. Dereference. */
777 gen_fetch (ax
, value
->type
);
780 case axs_lvalue_register
:
781 /* There's nothing on the stack, but value->u.reg is the
782 register number containing the value.
784 When we add floating-point support, this is going to have to
785 change. What about SPARC register pairs, for example? */
786 ax_reg (ax
, value
->u
.reg
);
787 gen_extend (ax
, value
->type
);
791 value
->kind
= axs_rvalue
;
795 /* Assume the top of the stack is described by VALUE, and perform the
796 usual unary conversions. This is motivated by ANSI 6.2.2, but of
797 course GDB expressions are not ANSI; they're the mishmash union of
798 a bunch of languages. Rah.
800 NOTE! This function promises to produce an rvalue only when the
801 incoming value is of an appropriate type. In other words, the
802 consumer of the value this function produces may assume the value
803 is an rvalue only after checking its type.
805 The immediate issue is that if the user tries to use a structure or
806 union as an operand of, say, the `+' operator, we don't want to try
807 to convert that structure to an rvalue; require_rvalue will bomb on
808 structs and unions. Rather, we want to simply pass the struct
809 lvalue through unchanged, and let `+' raise an error. */
812 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
814 /* We don't have to generate any code for the usual integral
815 conversions, since values are always represented as full-width on
816 the stack. Should we tweak the type? */
818 /* Some types require special handling. */
819 switch (TYPE_CODE (value
->type
))
821 /* Functions get converted to a pointer to the function. */
823 value
->type
= lookup_pointer_type (value
->type
);
824 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
827 /* Arrays get converted to a pointer to their first element, and
828 are no longer an lvalue. */
829 case TYPE_CODE_ARRAY
:
831 struct type
*elements
= TYPE_TARGET_TYPE (value
->type
);
833 value
->type
= lookup_pointer_type (elements
);
834 value
->kind
= axs_rvalue
;
835 /* We don't need to generate any code; the address of the array
836 is also the address of its first element. */
840 /* Don't try to convert structures and unions to rvalues. Let the
841 consumer signal an error. */
842 case TYPE_CODE_STRUCT
:
843 case TYPE_CODE_UNION
:
847 /* If the value is an lvalue, dereference it. */
848 require_rvalue (ax
, value
);
852 /* Return non-zero iff the type TYPE1 is considered "wider" than the
853 type TYPE2, according to the rules described in gen_usual_arithmetic. */
855 type_wider_than (struct type
*type1
, struct type
*type2
)
857 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)
858 || (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
859 && TYPE_UNSIGNED (type1
)
860 && !TYPE_UNSIGNED (type2
)));
864 /* Return the "wider" of the two types TYPE1 and TYPE2. */
866 max_type (struct type
*type1
, struct type
*type2
)
868 return type_wider_than (type1
, type2
) ? type1
: type2
;
872 /* Generate code to convert a scalar value of type FROM to type TO. */
874 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
876 /* Perhaps there is a more graceful way to state these rules. */
878 /* If we're converting to a narrower type, then we need to clear out
880 if (TYPE_LENGTH (to
) < TYPE_LENGTH (from
))
883 /* If the two values have equal width, but different signednesses,
884 then we need to extend. */
885 else if (TYPE_LENGTH (to
) == TYPE_LENGTH (from
))
887 if (TYPE_UNSIGNED (from
) != TYPE_UNSIGNED (to
))
891 /* If we're converting to a wider type, and becoming unsigned, then
892 we need to zero out any possible sign bits. */
893 else if (TYPE_LENGTH (to
) > TYPE_LENGTH (from
))
895 if (TYPE_UNSIGNED (to
))
901 /* Return non-zero iff the type FROM will require any bytecodes to be
902 emitted to be converted to the type TO. */
904 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
906 agent_expr_up
ax (new agent_expr (NULL
, 0));
909 /* Actually generate the code, and see if anything came out. At the
910 moment, it would be trivial to replicate the code in
911 gen_conversion here, but in the future, when we're supporting
912 floating point and the like, it may not be. Doing things this
913 way allows this function to be independent of the logic in
915 gen_conversion (ax
.get (), from
, to
);
916 nontrivial
= ax
->len
> 0;
921 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
922 6.2.1.5) for the two operands of an arithmetic operator. This
923 effectively finds a "least upper bound" type for the two arguments,
924 and promotes each argument to that type. *VALUE1 and *VALUE2
925 describe the values as they are passed in, and as they are left. */
927 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
928 struct axs_value
*value2
)
930 /* Do the usual binary conversions. */
931 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
932 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
934 /* The ANSI integral promotions seem to work this way: Order the
935 integer types by size, and then by signedness: an n-bit
936 unsigned type is considered "wider" than an n-bit signed
937 type. Promote to the "wider" of the two types, and always
938 promote at least to int. */
939 struct type
*target
= max_type (builtin_type (ax
->gdbarch
)->builtin_int
,
940 max_type (value1
->type
, value2
->type
));
942 /* Deal with value2, on the top of the stack. */
943 gen_conversion (ax
, value2
->type
, target
);
945 /* Deal with value1, not on the top of the stack. Don't
946 generate the `swap' instructions if we're not actually going
948 if (is_nontrivial_conversion (value1
->type
, target
))
950 ax_simple (ax
, aop_swap
);
951 gen_conversion (ax
, value1
->type
, target
);
952 ax_simple (ax
, aop_swap
);
955 value1
->type
= value2
->type
= check_typedef (target
);
960 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
961 the value on the top of the stack, as described by VALUE. Assume
962 the value has integral type. */
964 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
966 const struct builtin_type
*builtin
= builtin_type (ax
->gdbarch
);
968 if (!type_wider_than (value
->type
, builtin
->builtin_int
))
970 gen_conversion (ax
, value
->type
, builtin
->builtin_int
);
971 value
->type
= builtin
->builtin_int
;
973 else if (!type_wider_than (value
->type
, builtin
->builtin_unsigned_int
))
975 gen_conversion (ax
, value
->type
, builtin
->builtin_unsigned_int
);
976 value
->type
= builtin
->builtin_unsigned_int
;
981 /* Generate code for a cast to TYPE. */
983 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
985 /* GCC does allow casts to yield lvalues, so this should be fixed
986 before merging these changes into the trunk. */
987 require_rvalue (ax
, value
);
988 /* Dereference typedefs. */
989 type
= check_typedef (type
);
991 switch (TYPE_CODE (type
))
995 case TYPE_CODE_RVALUE_REF
:
996 /* It's implementation-defined, and I'll bet this is what GCC
1000 case TYPE_CODE_ARRAY
:
1001 case TYPE_CODE_STRUCT
:
1002 case TYPE_CODE_UNION
:
1003 case TYPE_CODE_FUNC
:
1004 error (_("Invalid type cast: intended type must be scalar."));
1006 case TYPE_CODE_ENUM
:
1007 case TYPE_CODE_BOOL
:
1008 /* We don't have to worry about the size of the value, because
1009 all our integral values are fully sign-extended, and when
1010 casting pointers we can do anything we like. Is there any
1011 way for us to know what GCC actually does with a cast like
1016 gen_conversion (ax
, value
->type
, type
);
1019 case TYPE_CODE_VOID
:
1020 /* We could pop the value, and rely on everyone else to check
1021 the type and notice that this value doesn't occupy a stack
1022 slot. But for now, leave the value on the stack, and
1023 preserve the "value == stack element" assumption. */
1027 error (_("Casts to requested type are not yet implemented."));
1035 /* Generating bytecode from GDB expressions: arithmetic */
1037 /* Scale the integer on the top of the stack by the size of the target
1038 of the pointer type TYPE. */
1040 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
1042 struct type
*element
= TYPE_TARGET_TYPE (type
);
1044 if (TYPE_LENGTH (element
) != 1)
1046 ax_const_l (ax
, TYPE_LENGTH (element
));
1052 /* Generate code for pointer arithmetic PTR + INT. */
1054 gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
1055 struct axs_value
*value1
, struct axs_value
*value2
)
1057 gdb_assert (pointer_type (value1
->type
));
1058 gdb_assert (TYPE_CODE (value2
->type
) == TYPE_CODE_INT
);
1060 gen_scale (ax
, aop_mul
, value1
->type
);
1061 ax_simple (ax
, aop_add
);
1062 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1063 value
->type
= value1
->type
;
1064 value
->kind
= axs_rvalue
;
1068 /* Generate code for pointer arithmetic PTR - INT. */
1070 gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
1071 struct axs_value
*value1
, struct axs_value
*value2
)
1073 gdb_assert (pointer_type (value1
->type
));
1074 gdb_assert (TYPE_CODE (value2
->type
) == TYPE_CODE_INT
);
1076 gen_scale (ax
, aop_mul
, value1
->type
);
1077 ax_simple (ax
, aop_sub
);
1078 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1079 value
->type
= value1
->type
;
1080 value
->kind
= axs_rvalue
;
1084 /* Generate code for pointer arithmetic PTR - PTR. */
1086 gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
1087 struct axs_value
*value1
, struct axs_value
*value2
,
1088 struct type
*result_type
)
1090 gdb_assert (pointer_type (value1
->type
));
1091 gdb_assert (pointer_type (value2
->type
));
1093 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1
->type
))
1094 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2
->type
)))
1096 First argument of `-' is a pointer, but second argument is neither\n\
1097 an integer nor a pointer of the same type."));
1099 ax_simple (ax
, aop_sub
);
1100 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1101 value
->type
= result_type
;
1102 value
->kind
= axs_rvalue
;
1106 gen_equal (struct agent_expr
*ax
, struct axs_value
*value
,
1107 struct axs_value
*value1
, struct axs_value
*value2
,
1108 struct type
*result_type
)
1110 if (pointer_type (value1
->type
) || pointer_type (value2
->type
))
1111 ax_simple (ax
, aop_equal
);
1113 gen_binop (ax
, value
, value1
, value2
,
1114 aop_equal
, aop_equal
, 0, "equal");
1115 value
->type
= result_type
;
1116 value
->kind
= axs_rvalue
;
1120 gen_less (struct agent_expr
*ax
, struct axs_value
*value
,
1121 struct axs_value
*value1
, struct axs_value
*value2
,
1122 struct type
*result_type
)
1124 if (pointer_type (value1
->type
) || pointer_type (value2
->type
))
1125 ax_simple (ax
, aop_less_unsigned
);
1127 gen_binop (ax
, value
, value1
, value2
,
1128 aop_less_signed
, aop_less_unsigned
, 0, "less than");
1129 value
->type
= result_type
;
1130 value
->kind
= axs_rvalue
;
1133 /* Generate code for a binary operator that doesn't do pointer magic.
1134 We set VALUE to describe the result value; we assume VALUE1 and
1135 VALUE2 describe the two operands, and that they've undergone the
1136 usual binary conversions. MAY_CARRY should be non-zero iff the
1137 result needs to be extended. NAME is the English name of the
1138 operator, used in error messages */
1140 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1141 struct axs_value
*value1
, struct axs_value
*value2
,
1142 enum agent_op op
, enum agent_op op_unsigned
,
1143 int may_carry
, const char *name
)
1145 /* We only handle INT op INT. */
1146 if ((TYPE_CODE (value1
->type
) != TYPE_CODE_INT
)
1147 || (TYPE_CODE (value2
->type
) != TYPE_CODE_INT
))
1148 error (_("Invalid combination of types in %s."), name
);
1151 TYPE_UNSIGNED (value1
->type
) ? op_unsigned
: op
);
1153 gen_extend (ax
, value1
->type
); /* catch overflow */
1154 value
->type
= value1
->type
;
1155 value
->kind
= axs_rvalue
;
1160 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
1161 struct type
*result_type
)
1163 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
1164 && TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1165 error (_("Invalid type of operand to `!'."));
1167 ax_simple (ax
, aop_log_not
);
1168 value
->type
= result_type
;
1173 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1175 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
)
1176 error (_("Invalid type of operand to `~'."));
1178 ax_simple (ax
, aop_bit_not
);
1179 gen_extend (ax
, value
->type
);
1184 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1186 /* Dereference the value on the top of the stack. */
1188 gen_deref (struct axs_value
*value
)
1190 /* The caller should check the type, because several operators use
1191 this, and we don't know what error message to generate. */
1192 if (!pointer_type (value
->type
))
1193 internal_error (__FILE__
, __LINE__
,
1194 _("gen_deref: expected a pointer"));
1196 /* We've got an rvalue now, which is a pointer. We want to yield an
1197 lvalue, whose address is exactly that pointer. So we don't
1198 actually emit any code; we just change the type from "Pointer to
1199 T" to "T", and mark the value as an lvalue in memory. Leave it
1200 to the consumer to actually dereference it. */
1201 value
->type
= check_typedef (TYPE_TARGET_TYPE (value
->type
));
1202 if (TYPE_CODE (value
->type
) == TYPE_CODE_VOID
)
1203 error (_("Attempt to dereference a generic pointer."));
1204 value
->kind
= ((TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1205 ? axs_rvalue
: axs_lvalue_memory
);
1209 /* Produce the address of the lvalue on the top of the stack. */
1211 gen_address_of (struct axs_value
*value
)
1213 /* Special case for taking the address of a function. The ANSI
1214 standard describes this as a special case, too, so this
1215 arrangement is not without motivation. */
1216 if (TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1217 /* The value's already an rvalue on the stack, so we just need to
1219 value
->type
= lookup_pointer_type (value
->type
);
1221 switch (value
->kind
)
1224 error (_("Operand of `&' is an rvalue, which has no address."));
1226 case axs_lvalue_register
:
1227 error (_("Operand of `&' is in a register, and has no address."));
1229 case axs_lvalue_memory
:
1230 value
->kind
= axs_rvalue
;
1231 value
->type
= lookup_pointer_type (value
->type
);
1236 /* Generate code to push the value of a bitfield of a structure whose
1237 address is on the top of the stack. START and END give the
1238 starting and one-past-ending *bit* numbers of the field within the
1241 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1242 struct type
*type
, int start
, int end
)
1244 /* Note that ops[i] fetches 8 << i bits. */
1245 static enum agent_op ops
[]
1246 = {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1247 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1249 /* We don't want to touch any byte that the bitfield doesn't
1250 actually occupy; we shouldn't make any accesses we're not
1251 explicitly permitted to. We rely here on the fact that the
1252 bytecode `ref' operators work on unaligned addresses.
1254 It takes some fancy footwork to get the stack to work the way
1255 we'd like. Say we're retrieving a bitfield that requires three
1256 fetches. Initially, the stack just contains the address:
1258 For the first fetch, we duplicate the address
1260 then add the byte offset, do the fetch, and shift and mask as
1261 needed, yielding a fragment of the value, properly aligned for
1262 the final bitwise or:
1264 then we swap, and repeat the process:
1265 frag1 addr --- address on top
1266 frag1 addr addr --- duplicate it
1267 frag1 addr frag2 --- get second fragment
1268 frag1 frag2 addr --- swap again
1269 frag1 frag2 frag3 --- get third fragment
1270 Notice that, since the third fragment is the last one, we don't
1271 bother duplicating the address this time. Now we have all the
1272 fragments on the stack, and we can simply `or' them together,
1273 yielding the final value of the bitfield. */
1275 /* The first and one-after-last bits in the field, but rounded down
1276 and up to byte boundaries. */
1277 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1278 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1282 /* current bit offset within the structure */
1285 /* The index in ops of the opcode we're considering. */
1288 /* The number of fragments we generated in the process. Probably
1289 equal to the number of `one' bits in bytesize, but who cares? */
1292 /* Dereference any typedefs. */
1293 type
= check_typedef (type
);
1295 /* Can we fetch the number of bits requested at all? */
1296 if ((end
- start
) > ((1 << num_ops
) * 8))
1297 internal_error (__FILE__
, __LINE__
,
1298 _("gen_bitfield_ref: bitfield too wide"));
1300 /* Note that we know here that we only need to try each opcode once.
1301 That may not be true on machines with weird byte sizes. */
1302 offset
= bound_start
;
1304 for (op
= num_ops
- 1; op
>= 0; op
--)
1306 /* number of bits that ops[op] would fetch */
1307 int op_size
= 8 << op
;
1309 /* The stack at this point, from bottom to top, contains zero or
1310 more fragments, then the address. */
1312 /* Does this fetch fit within the bitfield? */
1313 if (offset
+ op_size
<= bound_end
)
1315 /* Is this the last fragment? */
1316 int last_frag
= (offset
+ op_size
== bound_end
);
1319 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1321 /* Add the offset. */
1322 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1326 /* Record the area of memory we're about to fetch. */
1327 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1330 /* Perform the fetch. */
1331 ax_simple (ax
, ops
[op
]);
1333 /* Shift the bits we have to their proper position.
1334 gen_left_shift will generate right shifts when the operand
1337 A big-endian field diagram to ponder:
1338 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1339 +------++------++------++------++------++------++------++------+
1340 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1342 bit number 16 32 48 53
1343 These are bit numbers as supplied by GDB. Note that the
1344 bit numbers run from right to left once you've fetched the
1347 A little-endian field diagram to ponder:
1348 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1349 +------++------++------++------++------++------++------++------+
1350 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1352 bit number 48 32 16 4 0
1354 In both cases, the most significant end is on the left
1355 (i.e. normal numeric writing order), which means that you
1356 don't go crazy thinking about `left' and `right' shifts.
1358 We don't have to worry about masking yet:
1359 - If they contain garbage off the least significant end, then we
1360 must be looking at the low end of the field, and the right
1361 shift will wipe them out.
1362 - If they contain garbage off the most significant end, then we
1363 must be looking at the most significant end of the word, and
1364 the sign/zero extension will wipe them out.
1365 - If we're in the interior of the word, then there is no garbage
1366 on either end, because the ref operators zero-extend. */
1367 if (gdbarch_byte_order (ax
->gdbarch
) == BFD_ENDIAN_BIG
)
1368 gen_left_shift (ax
, end
- (offset
+ op_size
));
1370 gen_left_shift (ax
, offset
- start
);
1373 /* Bring the copy of the address up to the top. */
1374 ax_simple (ax
, aop_swap
);
1381 /* Generate enough bitwise `or' operations to combine all the
1382 fragments we left on the stack. */
1383 while (fragment_count
-- > 1)
1384 ax_simple (ax
, aop_bit_or
);
1386 /* Sign- or zero-extend the value as appropriate. */
1387 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1389 /* This is *not* an lvalue. Ugh. */
1390 value
->kind
= axs_rvalue
;
1394 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1395 is an accumulated offset (in bytes), will be nonzero for objects
1396 embedded in other objects, like C++ base classes. Behavior should
1397 generally follow value_primitive_field. */
1400 gen_primitive_field (struct agent_expr
*ax
, struct axs_value
*value
,
1401 int offset
, int fieldno
, struct type
*type
)
1403 /* Is this a bitfield? */
1404 if (TYPE_FIELD_PACKED (type
, fieldno
))
1405 gen_bitfield_ref (ax
, value
, TYPE_FIELD_TYPE (type
, fieldno
),
1406 (offset
* TARGET_CHAR_BIT
1407 + TYPE_FIELD_BITPOS (type
, fieldno
)),
1408 (offset
* TARGET_CHAR_BIT
1409 + TYPE_FIELD_BITPOS (type
, fieldno
)
1410 + TYPE_FIELD_BITSIZE (type
, fieldno
)));
1413 gen_offset (ax
, offset
1414 + TYPE_FIELD_BITPOS (type
, fieldno
) / TARGET_CHAR_BIT
);
1415 value
->kind
= axs_lvalue_memory
;
1416 value
->type
= TYPE_FIELD_TYPE (type
, fieldno
);
1420 /* Search for the given field in either the given type or one of its
1421 base classes. Return 1 if found, 0 if not. */
1424 gen_struct_ref_recursive (struct agent_expr
*ax
, struct axs_value
*value
,
1425 const char *field
, int offset
, struct type
*type
)
1428 int nbases
= TYPE_N_BASECLASSES (type
);
1430 type
= check_typedef (type
);
1432 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
1434 const char *this_name
= TYPE_FIELD_NAME (type
, i
);
1438 if (strcmp (field
, this_name
) == 0)
1440 /* Note that bytecodes for the struct's base (aka
1441 "this") will have been generated already, which will
1442 be unnecessary but not harmful if the static field is
1443 being handled as a global. */
1444 if (field_is_static (&TYPE_FIELD (type
, i
)))
1446 gen_static_field (ax
, value
, type
, i
);
1447 if (value
->optimized_out
)
1448 error (_("static field `%s' has been "
1449 "optimized out, cannot use"),
1454 gen_primitive_field (ax
, value
, offset
, i
, type
);
1457 #if 0 /* is this right? */
1458 if (this_name
[0] == '\0')
1459 internal_error (__FILE__
, __LINE__
,
1460 _("find_field: anonymous unions not supported"));
1465 /* Now scan through base classes recursively. */
1466 for (i
= 0; i
< nbases
; i
++)
1468 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1470 rslt
= gen_struct_ref_recursive (ax
, value
, field
,
1471 offset
+ TYPE_BASECLASS_BITPOS (type
, i
)
1478 /* Not found anywhere, flag so caller can complain. */
1482 /* Generate code to reference the member named FIELD of a structure or
1483 union. The top of the stack, as described by VALUE, should have
1484 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1485 the operator being compiled, and OPERAND_NAME is the kind of thing
1486 it operates on; we use them in error messages. */
1488 gen_struct_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1489 const char *field
, const char *operator_name
,
1490 const char *operand_name
)
1495 /* Follow pointers until we reach a non-pointer. These aren't the C
1496 semantics, but they're what the normal GDB evaluator does, so we
1497 should at least be consistent. */
1498 while (pointer_type (value
->type
))
1500 require_rvalue (ax
, value
);
1503 type
= check_typedef (value
->type
);
1505 /* This must yield a structure or a union. */
1506 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
1507 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
1508 error (_("The left operand of `%s' is not a %s."),
1509 operator_name
, operand_name
);
1511 /* And it must be in memory; we don't deal with structure rvalues,
1512 or structures living in registers. */
1513 if (value
->kind
!= axs_lvalue_memory
)
1514 error (_("Structure does not live in memory."));
1516 /* Search through fields and base classes recursively. */
1517 found
= gen_struct_ref_recursive (ax
, value
, field
, 0, type
);
1520 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1521 field
, TYPE_TAG_NAME (type
));
1525 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1526 const struct type
*curtype
, char *name
);
1528 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1529 const struct type
*curtype
, char *name
);
1532 gen_static_field (struct agent_expr
*ax
, struct axs_value
*value
,
1533 struct type
*type
, int fieldno
)
1535 if (TYPE_FIELD_LOC_KIND (type
, fieldno
) == FIELD_LOC_KIND_PHYSADDR
)
1537 ax_const_l (ax
, TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
1538 value
->kind
= axs_lvalue_memory
;
1539 value
->type
= TYPE_FIELD_TYPE (type
, fieldno
);
1540 value
->optimized_out
= 0;
1544 const char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1545 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0).symbol
;
1549 gen_var_ref (ax
, value
, sym
);
1551 /* Don't error if the value was optimized out, we may be
1552 scanning all static fields and just want to pass over this
1553 and continue with the rest. */
1557 /* Silently assume this was optimized out; class printing
1558 will let the user know why the data is missing. */
1559 value
->optimized_out
= 1;
1565 gen_struct_elt_for_reference (struct agent_expr
*ax
, struct axs_value
*value
,
1566 struct type
*type
, char *fieldname
)
1568 struct type
*t
= type
;
1571 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1572 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1573 internal_error (__FILE__
, __LINE__
,
1574 _("non-aggregate type to gen_struct_elt_for_reference"));
1576 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1578 const char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1580 if (t_field_name
&& strcmp (t_field_name
, fieldname
) == 0)
1582 if (field_is_static (&TYPE_FIELD (t
, i
)))
1584 gen_static_field (ax
, value
, t
, i
);
1585 if (value
->optimized_out
)
1586 error (_("static field `%s' has been "
1587 "optimized out, cannot use"),
1591 if (TYPE_FIELD_PACKED (t
, i
))
1592 error (_("pointers to bitfield members not allowed"));
1594 /* FIXME we need a way to do "want_address" equivalent */
1596 error (_("Cannot reference non-static field \"%s\""), fieldname
);
1600 /* FIXME add other scoped-reference cases here */
1602 /* Do a last-ditch lookup. */
1603 return gen_maybe_namespace_elt (ax
, value
, type
, fieldname
);
1606 /* C++: Return the member NAME of the namespace given by the type
1610 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1611 const struct type
*curtype
, char *name
)
1613 int found
= gen_maybe_namespace_elt (ax
, value
, curtype
, name
);
1616 error (_("No symbol \"%s\" in namespace \"%s\"."),
1617 name
, TYPE_TAG_NAME (curtype
));
1622 /* A helper function used by value_namespace_elt and
1623 value_struct_elt_for_reference. It looks up NAME inside the
1624 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1625 is a class and NAME refers to a type in CURTYPE itself (as opposed
1626 to, say, some base class of CURTYPE). */
1629 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1630 const struct type
*curtype
, char *name
)
1632 const char *namespace_name
= TYPE_TAG_NAME (curtype
);
1633 struct block_symbol sym
;
1635 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
1636 block_for_pc (ax
->scope
),
1639 if (sym
.symbol
== NULL
)
1642 gen_var_ref (ax
, value
, sym
.symbol
);
1644 if (value
->optimized_out
)
1645 error (_("`%s' has been optimized out, cannot use"),
1646 SYMBOL_PRINT_NAME (sym
.symbol
));
1653 gen_aggregate_elt_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1654 struct type
*type
, char *field
)
1656 switch (TYPE_CODE (type
))
1658 case TYPE_CODE_STRUCT
:
1659 case TYPE_CODE_UNION
:
1660 return gen_struct_elt_for_reference (ax
, value
, type
, field
);
1662 case TYPE_CODE_NAMESPACE
:
1663 return gen_namespace_elt (ax
, value
, type
, field
);
1666 internal_error (__FILE__
, __LINE__
,
1667 _("non-aggregate type in gen_aggregate_elt_ref"));
1673 /* Generate code for GDB's magical `repeat' operator.
1674 LVALUE @ INT creates an array INT elements long, and whose elements
1675 have the same type as LVALUE, located in memory so that LVALUE is
1676 its first element. For example, argv[0]@argc gives you the array
1677 of command-line arguments.
1679 Unfortunately, because we have to know the types before we actually
1680 have a value for the expression, we can't implement this perfectly
1681 without changing the type system, having values that occupy two
1682 stack slots, doing weird things with sizeof, etc. So we require
1683 the right operand to be a constant expression. */
1685 gen_repeat (struct expression
*exp
, union exp_element
**pc
,
1686 struct agent_expr
*ax
, struct axs_value
*value
)
1688 struct axs_value value1
;
1690 /* We don't want to turn this into an rvalue, so no conversions
1692 gen_expr (exp
, pc
, ax
, &value1
);
1693 if (value1
.kind
!= axs_lvalue_memory
)
1694 error (_("Left operand of `@' must be an object in memory."));
1696 /* Evaluate the length; it had better be a constant. */
1698 struct value
*v
= const_expr (pc
);
1702 error (_("Right operand of `@' must be a "
1703 "constant, in agent expressions."));
1704 if (TYPE_CODE (value_type (v
)) != TYPE_CODE_INT
)
1705 error (_("Right operand of `@' must be an integer."));
1706 length
= value_as_long (v
);
1708 error (_("Right operand of `@' must be positive."));
1710 /* The top of the stack is already the address of the object, so
1711 all we need to do is frob the type of the lvalue. */
1713 /* FIXME-type-allocation: need a way to free this type when we are
1716 = lookup_array_range_type (value1
.type
, 0, length
- 1);
1718 value
->kind
= axs_lvalue_memory
;
1719 value
->type
= array
;
1725 /* Emit code for the `sizeof' operator.
1726 *PC should point at the start of the operand expression; we advance it
1727 to the first instruction after the operand. */
1729 gen_sizeof (struct expression
*exp
, union exp_element
**pc
,
1730 struct agent_expr
*ax
, struct axs_value
*value
,
1731 struct type
*size_type
)
1733 /* We don't care about the value of the operand expression; we only
1734 care about its type. However, in the current arrangement, the
1735 only way to find an expression's type is to generate code for it.
1736 So we generate code for the operand, and then throw it away,
1737 replacing it with code that simply pushes its size. */
1738 int start
= ax
->len
;
1740 gen_expr (exp
, pc
, ax
, value
);
1742 /* Throw away the code we just generated. */
1745 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
1746 value
->kind
= axs_rvalue
;
1747 value
->type
= size_type
;
1751 /* Generating bytecode from GDB expressions: general recursive thingy */
1754 /* A gen_expr function written by a Gen-X'er guy.
1755 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1757 gen_expr (struct expression
*exp
, union exp_element
**pc
,
1758 struct agent_expr
*ax
, struct axs_value
*value
)
1760 /* Used to hold the descriptions of operand expressions. */
1761 struct axs_value value1
, value2
, value3
;
1762 enum exp_opcode op
= (*pc
)[0].opcode
, op2
;
1763 int if1
, go1
, if2
, go2
, end
;
1764 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
1766 /* If we're looking at a constant expression, just push its value. */
1768 struct value
*v
= maybe_const_expr (pc
);
1772 ax_const_l (ax
, value_as_long (v
));
1773 value
->kind
= axs_rvalue
;
1774 value
->type
= check_typedef (value_type (v
));
1779 /* Otherwise, go ahead and generate code for it. */
1782 /* Binary arithmetic operators. */
1790 case BINOP_SUBSCRIPT
:
1791 case BINOP_BITWISE_AND
:
1792 case BINOP_BITWISE_IOR
:
1793 case BINOP_BITWISE_XOR
:
1795 case BINOP_NOTEQUAL
:
1801 gen_expr (exp
, pc
, ax
, &value1
);
1802 gen_usual_unary (ax
, &value1
);
1803 gen_expr_binop_rest (exp
, op
, pc
, ax
, value
, &value1
, &value2
);
1806 case BINOP_LOGICAL_AND
:
1808 /* Generate the obvious sequence of tests and jumps. */
1809 gen_expr (exp
, pc
, ax
, &value1
);
1810 gen_usual_unary (ax
, &value1
);
1811 if1
= ax_goto (ax
, aop_if_goto
);
1812 go1
= ax_goto (ax
, aop_goto
);
1813 ax_label (ax
, if1
, ax
->len
);
1814 gen_expr (exp
, pc
, ax
, &value2
);
1815 gen_usual_unary (ax
, &value2
);
1816 if2
= ax_goto (ax
, aop_if_goto
);
1817 go2
= ax_goto (ax
, aop_goto
);
1818 ax_label (ax
, if2
, ax
->len
);
1820 end
= ax_goto (ax
, aop_goto
);
1821 ax_label (ax
, go1
, ax
->len
);
1822 ax_label (ax
, go2
, ax
->len
);
1824 ax_label (ax
, end
, ax
->len
);
1825 value
->kind
= axs_rvalue
;
1826 value
->type
= int_type
;
1829 case BINOP_LOGICAL_OR
:
1831 /* Generate the obvious sequence of tests and jumps. */
1832 gen_expr (exp
, pc
, ax
, &value1
);
1833 gen_usual_unary (ax
, &value1
);
1834 if1
= ax_goto (ax
, aop_if_goto
);
1835 gen_expr (exp
, pc
, ax
, &value2
);
1836 gen_usual_unary (ax
, &value2
);
1837 if2
= ax_goto (ax
, aop_if_goto
);
1839 end
= ax_goto (ax
, aop_goto
);
1840 ax_label (ax
, if1
, ax
->len
);
1841 ax_label (ax
, if2
, ax
->len
);
1843 ax_label (ax
, end
, ax
->len
);
1844 value
->kind
= axs_rvalue
;
1845 value
->type
= int_type
;
1850 gen_expr (exp
, pc
, ax
, &value1
);
1851 gen_usual_unary (ax
, &value1
);
1852 /* For (A ? B : C), it's easiest to generate subexpression
1853 bytecodes in order, but if_goto jumps on true, so we invert
1854 the sense of A. Then we can do B by dropping through, and
1856 gen_logical_not (ax
, &value1
, int_type
);
1857 if1
= ax_goto (ax
, aop_if_goto
);
1858 gen_expr (exp
, pc
, ax
, &value2
);
1859 gen_usual_unary (ax
, &value2
);
1860 end
= ax_goto (ax
, aop_goto
);
1861 ax_label (ax
, if1
, ax
->len
);
1862 gen_expr (exp
, pc
, ax
, &value3
);
1863 gen_usual_unary (ax
, &value3
);
1864 ax_label (ax
, end
, ax
->len
);
1865 /* This is arbitary - what if B and C are incompatible types? */
1866 value
->type
= value2
.type
;
1867 value
->kind
= value2
.kind
;
1872 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1874 char *name
= internalvar_name ((*pc
)[1].internalvar
);
1875 struct trace_state_variable
*tsv
;
1878 gen_expr (exp
, pc
, ax
, value
);
1879 tsv
= find_trace_state_variable (name
);
1882 ax_tsv (ax
, aop_setv
, tsv
->number
);
1884 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1887 error (_("$%s is not a trace state variable, "
1888 "may not assign to it"), name
);
1891 error (_("May only assign to trace state variables"));
1894 case BINOP_ASSIGN_MODIFY
:
1896 op2
= (*pc
)[0].opcode
;
1899 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1901 char *name
= internalvar_name ((*pc
)[1].internalvar
);
1902 struct trace_state_variable
*tsv
;
1905 tsv
= find_trace_state_variable (name
);
1908 /* The tsv will be the left half of the binary operation. */
1909 ax_tsv (ax
, aop_getv
, tsv
->number
);
1911 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1912 /* Trace state variables are always 64-bit integers. */
1913 value1
.kind
= axs_rvalue
;
1914 value1
.type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
1915 /* Now do right half of expression. */
1916 gen_expr_binop_rest (exp
, op2
, pc
, ax
, value
, &value1
, &value2
);
1917 /* We have a result of the binary op, set the tsv. */
1918 ax_tsv (ax
, aop_setv
, tsv
->number
);
1920 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1923 error (_("$%s is not a trace state variable, "
1924 "may not assign to it"), name
);
1927 error (_("May only assign to trace state variables"));
1930 /* Note that we need to be a little subtle about generating code
1931 for comma. In C, we can do some optimizations here because
1932 we know the left operand is only being evaluated for effect.
1933 However, if the tracing kludge is in effect, then we always
1934 need to evaluate the left hand side fully, so that all the
1935 variables it mentions get traced. */
1938 gen_expr (exp
, pc
, ax
, &value1
);
1939 /* Don't just dispose of the left operand. We might be tracing,
1940 in which case we want to emit code to trace it if it's an
1942 gen_traced_pop (ax
, &value1
);
1943 gen_expr (exp
, pc
, ax
, value
);
1944 /* It's the consumer's responsibility to trace the right operand. */
1947 case OP_LONG
: /* some integer constant */
1949 struct type
*type
= (*pc
)[1].type
;
1950 LONGEST k
= (*pc
)[2].longconst
;
1953 gen_int_literal (ax
, value
, k
, type
);
1958 gen_var_ref (ax
, value
, (*pc
)[2].symbol
);
1960 if (value
->optimized_out
)
1961 error (_("`%s' has been optimized out, cannot use"),
1962 SYMBOL_PRINT_NAME ((*pc
)[2].symbol
));
1969 const char *name
= &(*pc
)[2].string
;
1972 (*pc
) += 4 + BYTES_TO_EXP_ELEM ((*pc
)[1].longconst
+ 1);
1973 reg
= user_reg_map_name_to_regnum (ax
->gdbarch
, name
, strlen (name
));
1975 internal_error (__FILE__
, __LINE__
,
1976 _("Register $%s not available"), name
);
1977 /* No support for tracing user registers yet. */
1978 if (reg
>= gdbarch_num_regs (ax
->gdbarch
)
1979 + gdbarch_num_pseudo_regs (ax
->gdbarch
))
1980 error (_("'%s' is a user-register; "
1981 "GDB cannot yet trace user-register contents."),
1983 value
->kind
= axs_lvalue_register
;
1985 value
->type
= register_type (ax
->gdbarch
, reg
);
1989 case OP_INTERNALVAR
:
1991 struct internalvar
*var
= (*pc
)[1].internalvar
;
1992 const char *name
= internalvar_name (var
);
1993 struct trace_state_variable
*tsv
;
1996 tsv
= find_trace_state_variable (name
);
1999 ax_tsv (ax
, aop_getv
, tsv
->number
);
2001 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2002 /* Trace state variables are always 64-bit integers. */
2003 value
->kind
= axs_rvalue
;
2004 value
->type
= builtin_type (ax
->gdbarch
)->builtin_long_long
;
2006 else if (! compile_internalvar_to_ax (var
, ax
, value
))
2007 error (_("$%s is not a trace state variable; GDB agent "
2008 "expressions cannot use convenience variables."), name
);
2012 /* Weirdo operator: see comments for gen_repeat for details. */
2014 /* Note that gen_repeat handles its own argument evaluation. */
2016 gen_repeat (exp
, pc
, ax
, value
);
2021 struct type
*type
= (*pc
)[1].type
;
2024 gen_expr (exp
, pc
, ax
, value
);
2025 gen_cast (ax
, value
, type
);
2029 case UNOP_CAST_TYPE
:
2036 offset
= *pc
- exp
->elts
;
2037 val
= evaluate_subexp (NULL
, exp
, &offset
, EVAL_AVOID_SIDE_EFFECTS
);
2038 type
= value_type (val
);
2039 *pc
= &exp
->elts
[offset
];
2041 gen_expr (exp
, pc
, ax
, value
);
2042 gen_cast (ax
, value
, type
);
2048 struct type
*type
= check_typedef ((*pc
)[1].type
);
2051 gen_expr (exp
, pc
, ax
, value
);
2053 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2054 already have the right value on the stack. For
2055 axs_lvalue_register, we must convert. */
2056 if (value
->kind
== axs_lvalue_register
)
2057 require_rvalue (ax
, value
);
2060 value
->kind
= axs_lvalue_memory
;
2064 case UNOP_MEMVAL_TYPE
:
2071 offset
= *pc
- exp
->elts
;
2072 val
= evaluate_subexp (NULL
, exp
, &offset
, EVAL_AVOID_SIDE_EFFECTS
);
2073 type
= value_type (val
);
2074 *pc
= &exp
->elts
[offset
];
2076 gen_expr (exp
, pc
, ax
, value
);
2078 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2079 already have the right value on the stack. For
2080 axs_lvalue_register, we must convert. */
2081 if (value
->kind
== axs_lvalue_register
)
2082 require_rvalue (ax
, value
);
2085 value
->kind
= axs_lvalue_memory
;
2091 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2092 gen_expr (exp
, pc
, ax
, value
);
2093 gen_usual_unary (ax
, value
);
2098 /* -FOO is equivalent to 0 - FOO. */
2099 gen_int_literal (ax
, &value1
, 0,
2100 builtin_type (ax
->gdbarch
)->builtin_int
);
2101 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
2102 gen_expr (exp
, pc
, ax
, &value2
);
2103 gen_usual_unary (ax
, &value2
);
2104 gen_usual_arithmetic (ax
, &value1
, &value2
);
2105 gen_binop (ax
, value
, &value1
, &value2
, aop_sub
, aop_sub
, 1, "negation");
2108 case UNOP_LOGICAL_NOT
:
2110 gen_expr (exp
, pc
, ax
, value
);
2111 gen_usual_unary (ax
, value
);
2112 gen_logical_not (ax
, value
, int_type
);
2115 case UNOP_COMPLEMENT
:
2117 gen_expr (exp
, pc
, ax
, value
);
2118 gen_usual_unary (ax
, value
);
2119 gen_integral_promotions (ax
, value
);
2120 gen_complement (ax
, value
);
2125 gen_expr (exp
, pc
, ax
, value
);
2126 gen_usual_unary (ax
, value
);
2127 if (!pointer_type (value
->type
))
2128 error (_("Argument of unary `*' is not a pointer."));
2134 gen_expr (exp
, pc
, ax
, value
);
2135 gen_address_of (value
);
2140 /* Notice that gen_sizeof handles its own operand, unlike most
2141 of the other unary operator functions. This is because we
2142 have to throw away the code we generate. */
2143 gen_sizeof (exp
, pc
, ax
, value
,
2144 builtin_type (ax
->gdbarch
)->builtin_int
);
2147 case STRUCTOP_STRUCT
:
2150 int length
= (*pc
)[1].longconst
;
2151 char *name
= &(*pc
)[2].string
;
2153 (*pc
) += 4 + BYTES_TO_EXP_ELEM (length
+ 1);
2154 gen_expr (exp
, pc
, ax
, value
);
2155 if (op
== STRUCTOP_STRUCT
)
2156 gen_struct_ref (ax
, value
, name
, ".", "structure or union");
2157 else if (op
== STRUCTOP_PTR
)
2158 gen_struct_ref (ax
, value
, name
, "->",
2159 "pointer to a structure or union");
2161 /* If this `if' chain doesn't handle it, then the case list
2162 shouldn't mention it, and we shouldn't be here. */
2163 internal_error (__FILE__
, __LINE__
,
2164 _("gen_expr: unhandled struct case"));
2170 struct symbol
*sym
, *func
;
2171 const struct block
*b
;
2172 const struct language_defn
*lang
;
2174 b
= block_for_pc (ax
->scope
);
2175 func
= block_linkage_function (b
);
2176 lang
= language_def (SYMBOL_LANGUAGE (func
));
2178 sym
= lookup_language_this (lang
, b
).symbol
;
2180 error (_("no `%s' found"), lang
->la_name_of_this
);
2182 gen_var_ref (ax
, value
, sym
);
2184 if (value
->optimized_out
)
2185 error (_("`%s' has been optimized out, cannot use"),
2186 SYMBOL_PRINT_NAME (sym
));
2194 struct type
*type
= (*pc
)[1].type
;
2195 int length
= longest_to_int ((*pc
)[2].longconst
);
2196 char *name
= &(*pc
)[3].string
;
2199 found
= gen_aggregate_elt_ref (ax
, value
, type
, name
);
2201 error (_("There is no field named %s"), name
);
2202 (*pc
) += 5 + BYTES_TO_EXP_ELEM (length
+ 1);
2209 error (_("Attempt to use a type name as an expression."));
2212 error (_("Unsupported operator %s (%d) in expression."),
2213 op_name (exp
, op
), op
);
2217 /* This handles the middle-to-right-side of code generation for binary
2218 expressions, which is shared between regular binary operations and
2219 assign-modify (+= and friends) expressions. */
2222 gen_expr_binop_rest (struct expression
*exp
,
2223 enum exp_opcode op
, union exp_element
**pc
,
2224 struct agent_expr
*ax
, struct axs_value
*value
,
2225 struct axs_value
*value1
, struct axs_value
*value2
)
2227 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2229 gen_expr (exp
, pc
, ax
, value2
);
2230 gen_usual_unary (ax
, value2
);
2231 gen_usual_arithmetic (ax
, value1
, value2
);
2235 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
2236 && pointer_type (value2
->type
))
2238 /* Swap the values and proceed normally. */
2239 ax_simple (ax
, aop_swap
);
2240 gen_ptradd (ax
, value
, value2
, value1
);
2242 else if (pointer_type (value1
->type
)
2243 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
2244 gen_ptradd (ax
, value
, value1
, value2
);
2246 gen_binop (ax
, value
, value1
, value2
,
2247 aop_add
, aop_add
, 1, "addition");
2250 if (pointer_type (value1
->type
)
2251 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
2252 gen_ptrsub (ax
,value
, value1
, value2
);
2253 else if (pointer_type (value1
->type
)
2254 && pointer_type (value2
->type
))
2255 /* FIXME --- result type should be ptrdiff_t */
2256 gen_ptrdiff (ax
, value
, value1
, value2
,
2257 builtin_type (ax
->gdbarch
)->builtin_long
);
2259 gen_binop (ax
, value
, value1
, value2
,
2260 aop_sub
, aop_sub
, 1, "subtraction");
2263 gen_binop (ax
, value
, value1
, value2
,
2264 aop_mul
, aop_mul
, 1, "multiplication");
2267 gen_binop (ax
, value
, value1
, value2
,
2268 aop_div_signed
, aop_div_unsigned
, 1, "division");
2271 gen_binop (ax
, value
, value1
, value2
,
2272 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
2275 gen_binop (ax
, value
, value1
, value2
,
2276 aop_lsh
, aop_lsh
, 1, "left shift");
2279 gen_binop (ax
, value
, value1
, value2
,
2280 aop_rsh_signed
, aop_rsh_unsigned
, 1, "right shift");
2282 case BINOP_SUBSCRIPT
:
2286 if (binop_types_user_defined_p (op
, value1
->type
, value2
->type
))
2288 error (_("cannot subscript requested type: "
2289 "cannot call user defined functions"));
2293 /* If the user attempts to subscript something that is not
2294 an array or pointer type (like a plain int variable for
2295 example), then report this as an error. */
2296 type
= check_typedef (value1
->type
);
2297 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
2298 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
2300 if (TYPE_NAME (type
))
2301 error (_("cannot subscript something of type `%s'"),
2304 error (_("cannot subscript requested type"));
2308 if (!is_integral_type (value2
->type
))
2309 error (_("Argument to arithmetic operation "
2310 "not a number or boolean."));
2312 gen_ptradd (ax
, value
, value1
, value2
);
2316 case BINOP_BITWISE_AND
:
2317 gen_binop (ax
, value
, value1
, value2
,
2318 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
2321 case BINOP_BITWISE_IOR
:
2322 gen_binop (ax
, value
, value1
, value2
,
2323 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
2326 case BINOP_BITWISE_XOR
:
2327 gen_binop (ax
, value
, value1
, value2
,
2328 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
2332 gen_equal (ax
, value
, value1
, value2
, int_type
);
2335 case BINOP_NOTEQUAL
:
2336 gen_equal (ax
, value
, value1
, value2
, int_type
);
2337 gen_logical_not (ax
, value
, int_type
);
2341 gen_less (ax
, value
, value1
, value2
, int_type
);
2345 ax_simple (ax
, aop_swap
);
2346 gen_less (ax
, value
, value1
, value2
, int_type
);
2350 ax_simple (ax
, aop_swap
);
2351 gen_less (ax
, value
, value1
, value2
, int_type
);
2352 gen_logical_not (ax
, value
, int_type
);
2356 gen_less (ax
, value
, value1
, value2
, int_type
);
2357 gen_logical_not (ax
, value
, int_type
);
2361 /* We should only list operators in the outer case statement
2362 that we actually handle in the inner case statement. */
2363 internal_error (__FILE__
, __LINE__
,
2364 _("gen_expr: op case sets don't match"));
2369 /* Given a single variable and a scope, generate bytecodes to trace
2370 its value. This is for use in situations where we have only a
2371 variable's name, and no parsed expression; for instance, when the
2372 name comes from a list of local variables of a function. */
2375 gen_trace_for_var (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2376 struct symbol
*var
, int trace_string
)
2378 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2379 struct axs_value value
;
2382 ax
->trace_string
= trace_string
;
2383 gen_var_ref (ax
.get (), &value
, var
);
2385 /* If there is no actual variable to trace, flag it by returning
2386 an empty agent expression. */
2387 if (value
.optimized_out
)
2388 return agent_expr_up ();
2390 /* Make sure we record the final object, and get rid of it. */
2391 gen_traced_pop (ax
.get (), &value
);
2393 /* Oh, and terminate. */
2394 ax_simple (ax
.get (), aop_end
);
2399 /* Generating bytecode from GDB expressions: driver */
2401 /* Given a GDB expression EXPR, return bytecode to trace its value.
2402 The result will use the `trace' and `trace_quick' bytecodes to
2403 record the value of all memory touched by the expression. The
2404 caller can then use the ax_reqs function to discover which
2405 registers it relies upon. */
2408 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
,
2411 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2412 union exp_element
*pc
;
2413 struct axs_value value
;
2417 ax
->trace_string
= trace_string
;
2418 value
.optimized_out
= 0;
2419 gen_expr (expr
, &pc
, ax
.get (), &value
);
2421 /* Make sure we record the final object, and get rid of it. */
2422 gen_traced_pop (ax
.get (), &value
);
2424 /* Oh, and terminate. */
2425 ax_simple (ax
.get (), aop_end
);
2430 /* Given a GDB expression EXPR, return a bytecode sequence that will
2431 evaluate and return a result. The bytecodes will do a direct
2432 evaluation, using the current data on the target, rather than
2433 recording blocks of memory and registers for later use, as
2434 gen_trace_for_expr does. The generated bytecode sequence leaves
2435 the result of expression evaluation on the top of the stack. */
2438 gen_eval_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2440 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2441 union exp_element
*pc
;
2442 struct axs_value value
;
2446 value
.optimized_out
= 0;
2447 gen_expr (expr
, &pc
, ax
.get (), &value
);
2449 require_rvalue (ax
.get (), &value
);
2451 /* Oh, and terminate. */
2452 ax_simple (ax
.get (), aop_end
);
2458 gen_trace_for_return_address (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2461 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2462 struct axs_value value
;
2465 ax
->trace_string
= trace_string
;
2467 gdbarch_gen_return_address (gdbarch
, ax
.get (), &value
, scope
);
2469 /* Make sure we record the final object, and get rid of it. */
2470 gen_traced_pop (ax
.get (), &value
);
2472 /* Oh, and terminate. */
2473 ax_simple (ax
.get (), aop_end
);
2478 /* Given a collection of printf-style arguments, generate code to
2479 evaluate the arguments and pass everything to a special
2483 gen_printf (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2484 CORE_ADDR function
, LONGEST channel
,
2485 const char *format
, int fmtlen
,
2486 struct format_piece
*frags
,
2487 int nargs
, struct expression
**exprs
)
2489 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2490 union exp_element
*pc
;
2491 struct axs_value value
;
2494 /* We're computing values, not doing side effects. */
2497 /* Evaluate and push the args on the stack in reverse order,
2498 for simplicity of collecting them on the target side. */
2499 for (tem
= nargs
- 1; tem
>= 0; --tem
)
2501 pc
= exprs
[tem
]->elts
;
2502 value
.optimized_out
= 0;
2503 gen_expr (exprs
[tem
], &pc
, ax
.get (), &value
);
2504 require_rvalue (ax
.get (), &value
);
2507 /* Push function and channel. */
2508 ax_const_l (ax
.get (), channel
);
2509 ax_const_l (ax
.get (), function
);
2511 /* Issue the printf bytecode proper. */
2512 ax_simple (ax
.get (), aop_printf
);
2513 ax_raw_byte (ax
.get (), nargs
);
2514 ax_string (ax
.get (), format
, fmtlen
);
2516 /* And terminate. */
2517 ax_simple (ax
.get (), aop_end
);
2523 agent_eval_command_one (const char *exp
, int eval
, CORE_ADDR pc
)
2526 int trace_string
= 0;
2531 exp
= decode_agent_options (exp
, &trace_string
);
2534 agent_expr_up agent
;
2537 if (!eval
&& strcmp (arg
, "$_ret") == 0)
2539 agent
= gen_trace_for_return_address (pc
, get_current_arch (),
2544 expression_up expr
= parse_exp_1 (&arg
, pc
, block_for_pc (pc
), 0);
2548 gdb_assert (trace_string
== 0);
2549 agent
= gen_eval_for_expr (pc
, expr
.get ());
2552 agent
= gen_trace_for_expr (pc
, expr
.get (), trace_string
);
2555 ax_reqs (agent
.get ());
2556 ax_print (gdb_stdout
, agent
.get ());
2558 /* It would be nice to call ax_reqs here to gather some general info
2559 about the expression, and then print out the result. */
2565 agent_command_1 (char *exp
, int eval
)
2567 /* We don't deal with overlay debugging at the moment. We need to
2568 think more carefully about this. If you copy this code into
2569 another command, change the error message; the user shouldn't
2570 have to know anything about agent expressions. */
2571 if (overlay_debugging
)
2572 error (_("GDB can't do agent expression translation with overlays."));
2575 error_no_arg (_("expression to translate"));
2577 if (check_for_argument (&exp
, "-at", sizeof ("-at") - 1))
2579 struct linespec_result canonical
;
2581 exp
= skip_spaces (exp
);
2583 event_location_up location
= new_linespec_location (&exp
);
2584 decode_line_full (location
.get (), DECODE_LINE_FUNFIRSTLINE
, NULL
,
2585 (struct symtab
*) NULL
, 0, &canonical
,
2587 exp
= skip_spaces (exp
);
2591 exp
= skip_spaces (exp
);
2593 for (const auto &lsal
: canonical
.lsals
)
2594 for (const auto &sal
: lsal
.sals
)
2595 agent_eval_command_one (exp
, eval
, sal
.pc
);
2598 agent_eval_command_one (exp
, eval
, get_frame_pc (get_current_frame ()));
2604 agent_command (char *exp
, int from_tty
)
2606 agent_command_1 (exp
, 0);
2609 /* Parse the given expression, compile it into an agent expression
2610 that does direct evaluation, and display the resulting
2614 agent_eval_command (char *exp
, int from_tty
)
2616 agent_command_1 (exp
, 1);
2619 /* Parse the given expression, compile it into an agent expression
2620 that does a printf, and display the resulting expression. */
2623 maint_agent_printf_command (char *exp
, int from_tty
)
2625 struct cleanup
*old_chain
= 0;
2626 struct expression
*argvec
[100];
2627 struct frame_info
*fi
= get_current_frame (); /* need current scope */
2628 const char *cmdrest
;
2629 const char *format_start
, *format_end
;
2630 struct format_piece
*fpieces
;
2633 /* We don't deal with overlay debugging at the moment. We need to
2634 think more carefully about this. If you copy this code into
2635 another command, change the error message; the user shouldn't
2636 have to know anything about agent expressions. */
2637 if (overlay_debugging
)
2638 error (_("GDB can't do agent expression translation with overlays."));
2641 error_no_arg (_("expression to translate"));
2645 cmdrest
= skip_spaces_const (cmdrest
);
2647 if (*cmdrest
++ != '"')
2648 error (_("Must start with a format string."));
2650 format_start
= cmdrest
;
2652 fpieces
= parse_format_string (&cmdrest
);
2654 old_chain
= make_cleanup (free_format_pieces_cleanup
, &fpieces
);
2656 format_end
= cmdrest
;
2658 if (*cmdrest
++ != '"')
2659 error (_("Bad format string, non-terminated '\"'."));
2661 cmdrest
= skip_spaces_const (cmdrest
);
2663 if (*cmdrest
!= ',' && *cmdrest
!= 0)
2664 error (_("Invalid argument syntax"));
2666 if (*cmdrest
== ',')
2668 cmdrest
= skip_spaces_const (cmdrest
);
2671 while (*cmdrest
!= '\0')
2676 expression_up expr
= parse_exp_1 (&cmd1
, 0, (struct block
*) 0, 1);
2677 argvec
[nargs
] = expr
.release ();
2680 if (*cmdrest
== ',')
2682 /* else complain? */
2686 agent_expr_up agent
= gen_printf (get_frame_pc (fi
), get_current_arch (),
2688 format_start
, format_end
- format_start
,
2689 fpieces
, nargs
, argvec
);
2690 ax_reqs (agent
.get ());
2691 ax_print (gdb_stdout
, agent
.get ());
2693 /* It would be nice to call ax_reqs here to gather some general info
2694 about the expression, and then print out the result. */
2696 do_cleanups (old_chain
);
2701 /* Initialization code. */
2703 void _initialize_ax_gdb (void);
2705 _initialize_ax_gdb (void)
2707 add_cmd ("agent", class_maintenance
, agent_command
,
2709 Translate an expression into remote agent bytecode for tracing.\n\
2710 Usage: maint agent [-at location,] EXPRESSION\n\
2711 If -at is given, generate remote agent bytecode for this location.\n\
2712 If not, generate remote agent bytecode for current frame pc address."),
2715 add_cmd ("agent-eval", class_maintenance
, agent_eval_command
,
2717 Translate an expression into remote agent bytecode for evaluation.\n\
2718 Usage: maint agent-eval [-at location,] EXPRESSION\n\
2719 If -at is given, generate remote agent bytecode for this location.\n\
2720 If not, generate remote agent bytecode for current frame pc address."),
2723 add_cmd ("agent-printf", class_maintenance
, maint_agent_printf_command
,
2724 _("Translate an expression into remote "
2725 "agent bytecode for evaluation and display the bytecodes."),