1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "expression.h"
34 #include "gdb_string.h"
37 #include "user-regs.h"
39 #include "dictionary.h"
40 #include "breakpoint.h"
41 #include "tracepoint.h"
42 #include "cp-support.h"
44 /* To make sense of this file, you should read doc/agentexpr.texi.
45 Then look at the types and enums in ax-gdb.h. For the code itself,
46 look at gen_expr, towards the bottom; that's the main function that
47 looks at the GDB expressions and calls everything else to generate
50 I'm beginning to wonder whether it wouldn't be nicer to internally
51 generate trees, with types, and then spit out the bytecode in
52 linear form afterwards; we could generate fewer `swap', `ext', and
53 `zero_ext' bytecodes that way; it would make good constant folding
54 easier, too. But at the moment, I think we should be willing to
55 pay for the simplicity of this code with less-than-optimal bytecode
58 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
62 /* Prototypes for local functions. */
64 /* There's a standard order to the arguments of these functions:
65 union exp_element ** --- pointer into expression
66 struct agent_expr * --- agent expression buffer to generate code into
67 struct axs_value * --- describes value left on top of stack */
69 static struct value
*const_var_ref (struct symbol
*var
);
70 static struct value
*const_expr (union exp_element
**pc
);
71 static struct value
*maybe_const_expr (union exp_element
**pc
);
73 static void gen_traced_pop (struct gdbarch
*, struct agent_expr
*, struct axs_value
*);
75 static void gen_sign_extend (struct agent_expr
*, struct type
*);
76 static void gen_extend (struct agent_expr
*, struct type
*);
77 static void gen_fetch (struct agent_expr
*, struct type
*);
78 static void gen_left_shift (struct agent_expr
*, int);
81 static void gen_frame_args_address (struct gdbarch
*, struct agent_expr
*);
82 static void gen_frame_locals_address (struct gdbarch
*, struct agent_expr
*);
83 static void gen_offset (struct agent_expr
*ax
, int offset
);
84 static void gen_sym_offset (struct agent_expr
*, struct symbol
*);
85 static void gen_var_ref (struct gdbarch
*, struct agent_expr
*ax
,
86 struct axs_value
*value
, struct symbol
*var
);
89 static void gen_int_literal (struct agent_expr
*ax
,
90 struct axs_value
*value
,
91 LONGEST k
, struct type
*type
);
94 static void require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
);
95 static void gen_usual_unary (struct expression
*exp
, struct agent_expr
*ax
,
96 struct axs_value
*value
);
97 static int type_wider_than (struct type
*type1
, struct type
*type2
);
98 static struct type
*max_type (struct type
*type1
, struct type
*type2
);
99 static void gen_conversion (struct agent_expr
*ax
,
100 struct type
*from
, struct type
*to
);
101 static int is_nontrivial_conversion (struct type
*from
, struct type
*to
);
102 static void gen_usual_arithmetic (struct expression
*exp
,
103 struct agent_expr
*ax
,
104 struct axs_value
*value1
,
105 struct axs_value
*value2
);
106 static void gen_integral_promotions (struct expression
*exp
,
107 struct agent_expr
*ax
,
108 struct axs_value
*value
);
109 static void gen_cast (struct agent_expr
*ax
,
110 struct axs_value
*value
, struct type
*type
);
111 static void gen_scale (struct agent_expr
*ax
,
112 enum agent_op op
, struct type
*type
);
113 static void gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
114 struct axs_value
*value1
, struct axs_value
*value2
);
115 static void gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
116 struct axs_value
*value1
, struct axs_value
*value2
);
117 static void gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
118 struct axs_value
*value1
, struct axs_value
*value2
,
119 struct type
*result_type
);
120 static void gen_binop (struct agent_expr
*ax
,
121 struct axs_value
*value
,
122 struct axs_value
*value1
,
123 struct axs_value
*value2
,
125 enum agent_op op_unsigned
, int may_carry
, char *name
);
126 static void gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
127 struct type
*result_type
);
128 static void gen_complement (struct agent_expr
*ax
, struct axs_value
*value
);
129 static void gen_deref (struct agent_expr
*, struct axs_value
*);
130 static void gen_address_of (struct agent_expr
*, struct axs_value
*);
131 static void gen_bitfield_ref (struct expression
*exp
, struct agent_expr
*ax
,
132 struct axs_value
*value
,
133 struct type
*type
, int start
, int end
);
134 static void gen_primitive_field (struct expression
*exp
,
135 struct agent_expr
*ax
,
136 struct axs_value
*value
,
137 int offset
, int fieldno
, struct type
*type
);
138 static int gen_struct_ref_recursive (struct expression
*exp
,
139 struct agent_expr
*ax
,
140 struct axs_value
*value
,
141 char *field
, int offset
,
143 static void gen_struct_ref (struct expression
*exp
, struct agent_expr
*ax
,
144 struct axs_value
*value
,
146 char *operator_name
, char *operand_name
);
147 static void gen_static_field (struct gdbarch
*gdbarch
,
148 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 (struct expression
*exp
, union exp_element
**pc
,
156 struct agent_expr
*ax
, struct axs_value
*value
);
157 static void gen_expr_binop_rest (struct expression
*exp
,
158 enum exp_opcode op
, union exp_element
**pc
,
159 struct agent_expr
*ax
,
160 struct axs_value
*value
,
161 struct axs_value
*value1
,
162 struct axs_value
*value2
);
164 static void agent_command (char *exp
, int from_tty
);
167 /* Detecting constant expressions. */
169 /* If the variable reference at *PC is a constant, return its value.
170 Otherwise, return zero.
172 Hey, Wally! How can a variable reference be a constant?
174 Well, Beav, this function really handles the OP_VAR_VALUE operator,
175 not specifically variable references. GDB uses OP_VAR_VALUE to
176 refer to any kind of symbolic reference: function names, enum
177 elements, and goto labels are all handled through the OP_VAR_VALUE
178 operator, even though they're constants. It makes sense given the
181 Gee, Wally, don'cha wonder sometimes if data representations that
182 subvert commonly accepted definitions of terms in favor of heavily
183 context-specific interpretations are really just a tool of the
184 programming hegemony to preserve their power and exclude the
187 static struct value
*
188 const_var_ref (struct symbol
*var
)
190 struct type
*type
= SYMBOL_TYPE (var
);
192 switch (SYMBOL_CLASS (var
))
195 return value_from_longest (type
, (LONGEST
) SYMBOL_VALUE (var
));
198 return value_from_pointer (type
, (CORE_ADDR
) SYMBOL_VALUE_ADDRESS (var
));
206 /* If the expression starting at *PC has a constant value, return it.
207 Otherwise, return zero. If we return a value, then *PC will be
208 advanced to the end of it. If we return zero, *PC could be
210 static struct value
*
211 const_expr (union exp_element
**pc
)
213 enum exp_opcode op
= (*pc
)->opcode
;
220 struct type
*type
= (*pc
)[1].type
;
221 LONGEST k
= (*pc
)[2].longconst
;
224 return value_from_longest (type
, k
);
229 struct value
*v
= const_var_ref ((*pc
)[2].symbol
);
235 /* We could add more operators in here. */
239 v1
= const_expr (pc
);
241 return value_neg (v1
);
251 /* Like const_expr, but guarantee also that *PC is undisturbed if the
252 expression is not constant. */
253 static struct value
*
254 maybe_const_expr (union exp_element
**pc
)
256 union exp_element
*tentative_pc
= *pc
;
257 struct value
*v
= const_expr (&tentative_pc
);
259 /* If we got a value, then update the real PC. */
267 /* Generating bytecode from GDB expressions: general assumptions */
269 /* Here are a few general assumptions made throughout the code; if you
270 want to make a change that contradicts one of these, then you'd
271 better scan things pretty thoroughly.
273 - We assume that all values occupy one stack element. For example,
274 sometimes we'll swap to get at the left argument to a binary
275 operator. If we decide that void values should occupy no stack
276 elements, or that synthetic arrays (whose size is determined at
277 run time, created by the `@' operator) should occupy two stack
278 elements (address and length), then this will cause trouble.
280 - We assume the stack elements are infinitely wide, and that we
281 don't have to worry what happens if the user requests an
282 operation that is wider than the actual interpreter's stack.
283 That is, it's up to the interpreter to handle directly all the
284 integer widths the user has access to. (Woe betide the language
287 - We don't support side effects. Thus, we don't have to worry about
288 GCC's generalized lvalues, function calls, etc.
290 - We don't support floating point. Many places where we switch on
291 some type don't bother to include cases for floating point; there
292 may be even more subtle ways this assumption exists. For
293 example, the arguments to % must be integers.
295 - We assume all subexpressions have a static, unchanging type. If
296 we tried to support convenience variables, this would be a
299 - All values on the stack should always be fully zero- or
302 (I wasn't sure whether to choose this or its opposite --- that
303 only addresses are assumed extended --- but it turns out that
304 neither convention completely eliminates spurious extend
305 operations (if everything is always extended, then you have to
306 extend after add, because it could overflow; if nothing is
307 extended, then you end up producing extends whenever you change
308 sizes), and this is simpler.) */
311 /* Generating bytecode from GDB expressions: the `trace' kludge */
313 /* The compiler in this file is a general-purpose mechanism for
314 translating GDB expressions into bytecode. One ought to be able to
315 find a million and one uses for it.
317 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
318 of expediency. Let he who is without sin cast the first stone.
320 For the data tracing facility, we need to insert `trace' bytecodes
321 before each data fetch; this records all the memory that the
322 expression touches in the course of evaluation, so that memory will
323 be available when the user later tries to evaluate the expression
326 This should be done (I think) in a post-processing pass, that walks
327 an arbitrary agent expression and inserts `trace' operations at the
328 appropriate points. But it's much faster to just hack them
329 directly into the code. And since we're in a crunch, that's what
332 Setting the flag trace_kludge to non-zero enables the code that
333 emits the trace bytecodes at the appropriate points. */
336 /* Scan for all static fields in the given class, including any base
337 classes, and generate tracing bytecodes for each. */
340 gen_trace_static_fields (struct gdbarch
*gdbarch
,
341 struct agent_expr
*ax
,
344 int i
, nbases
= TYPE_N_BASECLASSES (type
);
345 struct axs_value value
;
347 CHECK_TYPEDEF (type
);
349 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
351 if (field_is_static (&TYPE_FIELD (type
, i
)))
353 gen_static_field (gdbarch
, ax
, &value
, type
, i
);
354 if (value
.optimized_out
)
358 case axs_lvalue_memory
:
360 int length
= TYPE_LENGTH (check_typedef (value
.type
));
362 ax_const_l (ax
, length
);
363 ax_simple (ax
, aop_trace
);
367 case axs_lvalue_register
:
368 /* We don't actually need the register's value to be pushed,
369 just note that we need it to be collected. */
370 ax_reg_mask (ax
, value
.u
.reg
);
378 /* Now scan through base classes recursively. */
379 for (i
= 0; i
< nbases
; i
++)
381 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
383 gen_trace_static_fields (gdbarch
, ax
, basetype
);
387 /* Trace the lvalue on the stack, if it needs it. In either case, pop
388 the value. Useful on the left side of a comma, and at the end of
389 an expression being used for tracing. */
391 gen_traced_pop (struct gdbarch
*gdbarch
,
392 struct agent_expr
*ax
, struct axs_value
*value
)
398 /* We don't trace rvalues, just the lvalues necessary to
399 produce them. So just dispose of this value. */
400 ax_simple (ax
, aop_pop
);
403 case axs_lvalue_memory
:
405 int length
= TYPE_LENGTH (check_typedef (value
->type
));
407 /* There's no point in trying to use a trace_quick bytecode
408 here, since "trace_quick SIZE pop" is three bytes, whereas
409 "const8 SIZE trace" is also three bytes, does the same
410 thing, and the simplest code which generates that will also
411 work correctly for objects with large sizes. */
412 ax_const_l (ax
, length
);
413 ax_simple (ax
, aop_trace
);
417 case axs_lvalue_register
:
418 /* We don't actually need the register's value to be on the
419 stack, and the target will get heartburn if the register is
420 larger than will fit in a stack, so just mark it for
421 collection and be done with it. */
422 ax_reg_mask (ax
, value
->u
.reg
);
426 /* If we're not tracing, just pop the value. */
427 ax_simple (ax
, aop_pop
);
429 /* To trace C++ classes with static fields stored elsewhere. */
431 && (TYPE_CODE (value
->type
) == TYPE_CODE_STRUCT
432 || TYPE_CODE (value
->type
) == TYPE_CODE_UNION
))
433 gen_trace_static_fields (gdbarch
, ax
, value
->type
);
438 /* Generating bytecode from GDB expressions: helper functions */
440 /* Assume that the lower bits of the top of the stack is a value of
441 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
443 gen_sign_extend (struct agent_expr
*ax
, struct type
*type
)
445 /* Do we need to sign-extend this? */
446 if (!TYPE_UNSIGNED (type
))
447 ax_ext (ax
, TYPE_LENGTH (type
) * TARGET_CHAR_BIT
);
451 /* Assume the lower bits of the top of the stack hold a value of type
452 TYPE, and the upper bits are garbage. Sign-extend or truncate as
455 gen_extend (struct agent_expr
*ax
, struct type
*type
)
457 int bits
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
460 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, bits
));
464 /* Assume that the top of the stack contains a value of type "pointer
465 to TYPE"; generate code to fetch its value. Note that TYPE is the
466 target type, not the pointer type. */
468 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
472 /* Record the area of memory we're about to fetch. */
473 ax_trace_quick (ax
, TYPE_LENGTH (type
));
476 switch (TYPE_CODE (type
))
484 /* It's a scalar value, so we know how to dereference it. How
485 many bytes long is it? */
486 switch (TYPE_LENGTH (type
))
488 case 8 / TARGET_CHAR_BIT
:
489 ax_simple (ax
, aop_ref8
);
491 case 16 / TARGET_CHAR_BIT
:
492 ax_simple (ax
, aop_ref16
);
494 case 32 / TARGET_CHAR_BIT
:
495 ax_simple (ax
, aop_ref32
);
497 case 64 / TARGET_CHAR_BIT
:
498 ax_simple (ax
, aop_ref64
);
501 /* Either our caller shouldn't have asked us to dereference
502 that pointer (other code's fault), or we're not
503 implementing something we should be (this code's fault).
504 In any case, it's a bug the user shouldn't see. */
506 internal_error (__FILE__
, __LINE__
,
507 _("gen_fetch: strange size"));
510 gen_sign_extend (ax
, type
);
514 /* Either our caller shouldn't have asked us to dereference that
515 pointer (other code's fault), or we're not implementing
516 something we should be (this code's fault). In any case,
517 it's a bug the user shouldn't see. */
518 internal_error (__FILE__
, __LINE__
,
519 _("gen_fetch: bad type code"));
524 /* Generate code to left shift the top of the stack by DISTANCE bits, or
525 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
526 unsigned (logical) right shifts. */
528 gen_left_shift (struct agent_expr
*ax
, int distance
)
532 ax_const_l (ax
, distance
);
533 ax_simple (ax
, aop_lsh
);
535 else if (distance
< 0)
537 ax_const_l (ax
, -distance
);
538 ax_simple (ax
, aop_rsh_unsigned
);
544 /* Generating bytecode from GDB expressions: symbol references */
546 /* Generate code to push the base address of the argument portion of
547 the top stack frame. */
549 gen_frame_args_address (struct gdbarch
*gdbarch
, struct agent_expr
*ax
)
552 LONGEST frame_offset
;
554 gdbarch_virtual_frame_pointer (gdbarch
,
555 ax
->scope
, &frame_reg
, &frame_offset
);
556 ax_reg (ax
, frame_reg
);
557 gen_offset (ax
, frame_offset
);
561 /* Generate code to push the base address of the locals portion of the
564 gen_frame_locals_address (struct gdbarch
*gdbarch
, struct agent_expr
*ax
)
567 LONGEST frame_offset
;
569 gdbarch_virtual_frame_pointer (gdbarch
,
570 ax
->scope
, &frame_reg
, &frame_offset
);
571 ax_reg (ax
, frame_reg
);
572 gen_offset (ax
, frame_offset
);
576 /* Generate code to add OFFSET to the top of the stack. Try to
577 generate short and readable code. We use this for getting to
578 variables on the stack, and structure members. If we were
579 programming in ML, it would be clearer why these are the same
582 gen_offset (struct agent_expr
*ax
, int offset
)
584 /* It would suffice to simply push the offset and add it, but this
585 makes it easier to read positive and negative offsets in the
589 ax_const_l (ax
, offset
);
590 ax_simple (ax
, aop_add
);
594 ax_const_l (ax
, -offset
);
595 ax_simple (ax
, aop_sub
);
600 /* In many cases, a symbol's value is the offset from some other
601 address (stack frame, base register, etc.) Generate code to add
602 VAR's value to the top of the stack. */
604 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
606 gen_offset (ax
, SYMBOL_VALUE (var
));
610 /* Generate code for a variable reference to AX. The variable is the
611 symbol VAR. Set VALUE to describe the result. */
614 gen_var_ref (struct gdbarch
*gdbarch
, struct agent_expr
*ax
,
615 struct axs_value
*value
, struct symbol
*var
)
617 /* Dereference any typedefs. */
618 value
->type
= check_typedef (SYMBOL_TYPE (var
));
619 value
->optimized_out
= 0;
621 /* I'm imitating the code in read_var_value. */
622 switch (SYMBOL_CLASS (var
))
624 case LOC_CONST
: /* A constant, like an enum value. */
625 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE (var
));
626 value
->kind
= axs_rvalue
;
629 case LOC_LABEL
: /* A goto label, being used as a value. */
630 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
631 value
->kind
= axs_rvalue
;
634 case LOC_CONST_BYTES
:
635 internal_error (__FILE__
, __LINE__
,
636 _("gen_var_ref: LOC_CONST_BYTES symbols are not supported"));
638 /* Variable at a fixed location in memory. Easy. */
640 /* Push the address of the variable. */
641 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (var
));
642 value
->kind
= axs_lvalue_memory
;
645 case LOC_ARG
: /* var lives in argument area of frame */
646 gen_frame_args_address (gdbarch
, ax
);
647 gen_sym_offset (ax
, var
);
648 value
->kind
= axs_lvalue_memory
;
651 case LOC_REF_ARG
: /* As above, but the frame slot really
652 holds the address of the variable. */
653 gen_frame_args_address (gdbarch
, ax
);
654 gen_sym_offset (ax
, var
);
655 /* Don't assume any particular pointer size. */
656 gen_fetch (ax
, builtin_type (gdbarch
)->builtin_data_ptr
);
657 value
->kind
= axs_lvalue_memory
;
660 case LOC_LOCAL
: /* var lives in locals area of frame */
661 gen_frame_locals_address (gdbarch
, ax
);
662 gen_sym_offset (ax
, var
);
663 value
->kind
= axs_lvalue_memory
;
667 error (_("Cannot compute value of typedef `%s'."),
668 SYMBOL_PRINT_NAME (var
));
672 ax_const_l (ax
, BLOCK_START (SYMBOL_BLOCK_VALUE (var
)));
673 value
->kind
= axs_rvalue
;
677 /* Don't generate any code at all; in the process of treating
678 this as an lvalue or rvalue, the caller will generate the
680 value
->kind
= axs_lvalue_register
;
681 value
->u
.reg
= SYMBOL_REGISTER_OPS (var
)->register_number (var
, gdbarch
);
684 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
685 register, not on the stack. Simpler than LOC_REGISTER
686 because it's just like any other case where the thing
687 has a real address. */
688 case LOC_REGPARM_ADDR
:
689 ax_reg (ax
, SYMBOL_REGISTER_OPS (var
)->register_number (var
, gdbarch
));
690 value
->kind
= axs_lvalue_memory
;
695 struct minimal_symbol
*msym
696 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var
), NULL
, NULL
);
699 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var
));
701 /* Push the address of the variable. */
702 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (msym
));
703 value
->kind
= axs_lvalue_memory
;
708 /* FIXME: cagney/2004-01-26: It should be possible to
709 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
710 Unfortunately DWARF 2 stores the frame-base (instead of the
711 function) location in a function's symbol. Oops! For the
712 moment enable this when/where applicable. */
713 SYMBOL_COMPUTED_OPS (var
)->tracepoint_var_ref (var
, gdbarch
, ax
, value
);
716 case LOC_OPTIMIZED_OUT
:
717 /* Flag this, but don't say anything; leave it up to callers to
719 value
->optimized_out
= 1;
723 error (_("Cannot find value of botched symbol `%s'."),
724 SYMBOL_PRINT_NAME (var
));
731 /* Generating bytecode from GDB expressions: literals */
734 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
738 value
->kind
= axs_rvalue
;
739 value
->type
= check_typedef (type
);
744 /* Generating bytecode from GDB expressions: unary conversions, casts */
746 /* Take what's on the top of the stack (as described by VALUE), and
747 try to make an rvalue out of it. Signal an error if we can't do
750 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
752 /* Only deal with scalars, structs and such may be too large
753 to fit in a stack entry. */
754 value
->type
= check_typedef (value
->type
);
755 if (TYPE_CODE (value
->type
) == TYPE_CODE_ARRAY
756 || TYPE_CODE (value
->type
) == TYPE_CODE_STRUCT
757 || TYPE_CODE (value
->type
) == TYPE_CODE_UNION
758 || TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
759 error (_("Value not scalar: cannot be an rvalue."));
764 /* It's already an rvalue. */
767 case axs_lvalue_memory
:
768 /* The top of stack is the address of the object. Dereference. */
769 gen_fetch (ax
, value
->type
);
772 case axs_lvalue_register
:
773 /* There's nothing on the stack, but value->u.reg is the
774 register number containing the value.
776 When we add floating-point support, this is going to have to
777 change. What about SPARC register pairs, for example? */
778 ax_reg (ax
, value
->u
.reg
);
779 gen_extend (ax
, value
->type
);
783 value
->kind
= axs_rvalue
;
787 /* Assume the top of the stack is described by VALUE, and perform the
788 usual unary conversions. This is motivated by ANSI 6.2.2, but of
789 course GDB expressions are not ANSI; they're the mishmash union of
790 a bunch of languages. Rah.
792 NOTE! This function promises to produce an rvalue only when the
793 incoming value is of an appropriate type. In other words, the
794 consumer of the value this function produces may assume the value
795 is an rvalue only after checking its type.
797 The immediate issue is that if the user tries to use a structure or
798 union as an operand of, say, the `+' operator, we don't want to try
799 to convert that structure to an rvalue; require_rvalue will bomb on
800 structs and unions. Rather, we want to simply pass the struct
801 lvalue through unchanged, and let `+' raise an error. */
804 gen_usual_unary (struct expression
*exp
, struct agent_expr
*ax
,
805 struct axs_value
*value
)
807 /* We don't have to generate any code for the usual integral
808 conversions, since values are always represented as full-width on
809 the stack. Should we tweak the type? */
811 /* Some types require special handling. */
812 switch (TYPE_CODE (value
->type
))
814 /* Functions get converted to a pointer to the function. */
816 value
->type
= lookup_pointer_type (value
->type
);
817 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
820 /* Arrays get converted to a pointer to their first element, and
821 are no longer an lvalue. */
822 case TYPE_CODE_ARRAY
:
824 struct type
*elements
= TYPE_TARGET_TYPE (value
->type
);
826 value
->type
= lookup_pointer_type (elements
);
827 value
->kind
= axs_rvalue
;
828 /* We don't need to generate any code; the address of the array
829 is also the address of its first element. */
833 /* Don't try to convert structures and unions to rvalues. Let the
834 consumer signal an error. */
835 case TYPE_CODE_STRUCT
:
836 case TYPE_CODE_UNION
:
839 /* If the value is an enum or a bool, call it an integer. */
842 value
->type
= builtin_type (exp
->gdbarch
)->builtin_int
;
846 /* If the value is an lvalue, dereference it. */
847 require_rvalue (ax
, value
);
851 /* Return non-zero iff the type TYPE1 is considered "wider" than the
852 type TYPE2, according to the rules described in gen_usual_arithmetic. */
854 type_wider_than (struct type
*type1
, struct type
*type2
)
856 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)
857 || (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
858 && TYPE_UNSIGNED (type1
)
859 && !TYPE_UNSIGNED (type2
)));
863 /* Return the "wider" of the two types TYPE1 and TYPE2. */
865 max_type (struct type
*type1
, struct type
*type2
)
867 return type_wider_than (type1
, type2
) ? type1
: type2
;
871 /* Generate code to convert a scalar value of type FROM to type TO. */
873 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
875 /* Perhaps there is a more graceful way to state these rules. */
877 /* If we're converting to a narrower type, then we need to clear out
879 if (TYPE_LENGTH (to
) < TYPE_LENGTH (from
))
880 gen_extend (ax
, from
);
882 /* If the two values have equal width, but different signednesses,
883 then we need to extend. */
884 else if (TYPE_LENGTH (to
) == TYPE_LENGTH (from
))
886 if (TYPE_UNSIGNED (from
) != TYPE_UNSIGNED (to
))
890 /* If we're converting to a wider type, and becoming unsigned, then
891 we need to zero out any possible sign bits. */
892 else if (TYPE_LENGTH (to
) > TYPE_LENGTH (from
))
894 if (TYPE_UNSIGNED (to
))
900 /* Return non-zero iff the type FROM will require any bytecodes to be
901 emitted to be converted to the type TO. */
903 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
905 struct agent_expr
*ax
= new_agent_expr (NULL
, 0);
908 /* Actually generate the code, and see if anything came out. At the
909 moment, it would be trivial to replicate the code in
910 gen_conversion here, but in the future, when we're supporting
911 floating point and the like, it may not be. Doing things this
912 way allows this function to be independent of the logic in
914 gen_conversion (ax
, from
, to
);
915 nontrivial
= ax
->len
> 0;
916 free_agent_expr (ax
);
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 expression
*exp
, struct agent_expr
*ax
,
928 struct axs_value
*value1
, 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 (exp
->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 expression
*exp
, struct agent_expr
*ax
,
965 struct axs_value
*value
)
967 const struct builtin_type
*builtin
= builtin_type (exp
->gdbarch
);
969 if (!type_wider_than (value
->type
, builtin
->builtin_int
))
971 gen_conversion (ax
, value
->type
, builtin
->builtin_int
);
972 value
->type
= builtin
->builtin_int
;
974 else if (!type_wider_than (value
->type
, builtin
->builtin_unsigned_int
))
976 gen_conversion (ax
, value
->type
, builtin
->builtin_unsigned_int
);
977 value
->type
= builtin
->builtin_unsigned_int
;
982 /* Generate code for a cast to TYPE. */
984 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
986 /* GCC does allow casts to yield lvalues, so this should be fixed
987 before merging these changes into the trunk. */
988 require_rvalue (ax
, value
);
989 /* Dereference typedefs. */
990 type
= check_typedef (type
);
992 switch (TYPE_CODE (type
))
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
, enum agent_op op
,
1142 enum agent_op op_unsigned
, int may_carry
, char *name
)
1144 /* We only handle INT op INT. */
1145 if ((TYPE_CODE (value1
->type
) != TYPE_CODE_INT
)
1146 || (TYPE_CODE (value2
->type
) != TYPE_CODE_INT
))
1147 error (_("Invalid combination of types in %s."), name
);
1150 TYPE_UNSIGNED (value1
->type
) ? op_unsigned
: op
);
1152 gen_extend (ax
, value1
->type
); /* catch overflow */
1153 value
->type
= value1
->type
;
1154 value
->kind
= axs_rvalue
;
1159 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
1160 struct type
*result_type
)
1162 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
1163 && TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1164 error (_("Invalid type of operand to `!'."));
1166 ax_simple (ax
, aop_log_not
);
1167 value
->type
= result_type
;
1172 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1174 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
)
1175 error (_("Invalid type of operand to `~'."));
1177 ax_simple (ax
, aop_bit_not
);
1178 gen_extend (ax
, value
->type
);
1183 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1185 /* Dereference the value on the top of the stack. */
1187 gen_deref (struct agent_expr
*ax
, struct axs_value
*value
)
1189 /* The caller should check the type, because several operators use
1190 this, and we don't know what error message to generate. */
1191 if (!pointer_type (value
->type
))
1192 internal_error (__FILE__
, __LINE__
,
1193 _("gen_deref: expected a pointer"));
1195 /* We've got an rvalue now, which is a pointer. We want to yield an
1196 lvalue, whose address is exactly that pointer. So we don't
1197 actually emit any code; we just change the type from "Pointer to
1198 T" to "T", and mark the value as an lvalue in memory. Leave it
1199 to the consumer to actually dereference it. */
1200 value
->type
= check_typedef (TYPE_TARGET_TYPE (value
->type
));
1201 if (TYPE_CODE (value
->type
) == TYPE_CODE_VOID
)
1202 error (_("Attempt to dereference a generic pointer."));
1203 value
->kind
= ((TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1204 ? axs_rvalue
: axs_lvalue_memory
);
1208 /* Produce the address of the lvalue on the top of the stack. */
1210 gen_address_of (struct agent_expr
*ax
, struct axs_value
*value
)
1212 /* Special case for taking the address of a function. The ANSI
1213 standard describes this as a special case, too, so this
1214 arrangement is not without motivation. */
1215 if (TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1216 /* The value's already an rvalue on the stack, so we just need to
1218 value
->type
= lookup_pointer_type (value
->type
);
1220 switch (value
->kind
)
1223 error (_("Operand of `&' is an rvalue, which has no address."));
1225 case axs_lvalue_register
:
1226 error (_("Operand of `&' is in a register, and has no address."));
1228 case axs_lvalue_memory
:
1229 value
->kind
= axs_rvalue
;
1230 value
->type
= lookup_pointer_type (value
->type
);
1235 /* Generate code to push the value of a bitfield of a structure whose
1236 address is on the top of the stack. START and END give the
1237 starting and one-past-ending *bit* numbers of the field within the
1240 gen_bitfield_ref (struct expression
*exp
, struct agent_expr
*ax
,
1241 struct axs_value
*value
, struct type
*type
,
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 (exp
->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 expression
*exp
,
1401 struct agent_expr
*ax
, struct axs_value
*value
,
1402 int offset
, int fieldno
, struct type
*type
)
1404 /* Is this a bitfield? */
1405 if (TYPE_FIELD_PACKED (type
, fieldno
))
1406 gen_bitfield_ref (exp
, ax
, value
, TYPE_FIELD_TYPE (type
, fieldno
),
1407 (offset
* TARGET_CHAR_BIT
1408 + TYPE_FIELD_BITPOS (type
, fieldno
)),
1409 (offset
* TARGET_CHAR_BIT
1410 + TYPE_FIELD_BITPOS (type
, fieldno
)
1411 + TYPE_FIELD_BITSIZE (type
, fieldno
)));
1414 gen_offset (ax
, offset
1415 + TYPE_FIELD_BITPOS (type
, fieldno
) / TARGET_CHAR_BIT
);
1416 value
->kind
= axs_lvalue_memory
;
1417 value
->type
= TYPE_FIELD_TYPE (type
, fieldno
);
1421 /* Search for the given field in either the given type or one of its
1422 base classes. Return 1 if found, 0 if not. */
1425 gen_struct_ref_recursive (struct expression
*exp
, struct agent_expr
*ax
,
1426 struct axs_value
*value
,
1427 char *field
, int offset
, struct type
*type
)
1430 int nbases
= TYPE_N_BASECLASSES (type
);
1432 CHECK_TYPEDEF (type
);
1434 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
1436 char *this_name
= TYPE_FIELD_NAME (type
, i
);
1440 if (strcmp (field
, this_name
) == 0)
1442 /* Note that bytecodes for the struct's base (aka
1443 "this") will have been generated already, which will
1444 be unnecessary but not harmful if the static field is
1445 being handled as a global. */
1446 if (field_is_static (&TYPE_FIELD (type
, i
)))
1448 gen_static_field (exp
->gdbarch
, ax
, value
, type
, i
);
1449 if (value
->optimized_out
)
1450 error (_("static field `%s' has been optimized out, cannot use"),
1455 gen_primitive_field (exp
, ax
, value
, offset
, i
, type
);
1458 #if 0 /* is this right? */
1459 if (this_name
[0] == '\0')
1460 internal_error (__FILE__
, __LINE__
,
1461 _("find_field: anonymous unions not supported"));
1466 /* Now scan through base classes recursively. */
1467 for (i
= 0; i
< nbases
; i
++)
1469 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1471 rslt
= gen_struct_ref_recursive (exp
, ax
, value
, field
,
1472 offset
+ TYPE_BASECLASS_BITPOS (type
, i
) / TARGET_CHAR_BIT
,
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 expression
*exp
, struct agent_expr
*ax
,
1489 struct axs_value
*value
, char *field
,
1490 char *operator_name
, 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
);
1501 gen_deref (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 (exp
, 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 expression
*exp
,
1526 struct agent_expr
*ax
, struct axs_value
*value
,
1527 const struct type
*curtype
, char *name
);
1529 gen_maybe_namespace_elt (struct expression
*exp
,
1530 struct agent_expr
*ax
, struct axs_value
*value
,
1531 const struct type
*curtype
, char *name
);
1534 gen_static_field (struct gdbarch
*gdbarch
,
1535 struct agent_expr
*ax
, struct axs_value
*value
,
1536 struct type
*type
, int fieldno
)
1538 if (TYPE_FIELD_LOC_KIND (type
, fieldno
) == FIELD_LOC_KIND_PHYSADDR
)
1540 ax_const_l (ax
, TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
1541 value
->kind
= axs_lvalue_memory
;
1542 value
->type
= TYPE_FIELD_TYPE (type
, fieldno
);
1543 value
->optimized_out
= 0;
1547 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1548 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0);
1552 gen_var_ref (gdbarch
, ax
, value
, sym
);
1554 /* Don't error if the value was optimized out, we may be
1555 scanning all static fields and just want to pass over this
1556 and continue with the rest. */
1560 /* Silently assume this was optimized out; class printing
1561 will let the user know why the data is missing. */
1562 value
->optimized_out
= 1;
1568 gen_struct_elt_for_reference (struct expression
*exp
,
1569 struct agent_expr
*ax
, struct axs_value
*value
,
1570 struct type
*type
, char *fieldname
)
1572 struct type
*t
= type
;
1575 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1576 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1577 internal_error (__FILE__
, __LINE__
,
1578 _("non-aggregate type to gen_struct_elt_for_reference"));
1580 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1582 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1584 if (t_field_name
&& strcmp (t_field_name
, fieldname
) == 0)
1586 if (field_is_static (&TYPE_FIELD (t
, i
)))
1588 gen_static_field (exp
->gdbarch
, ax
, value
, t
, i
);
1589 if (value
->optimized_out
)
1590 error (_("static field `%s' has been optimized out, cannot use"),
1594 if (TYPE_FIELD_PACKED (t
, i
))
1595 error (_("pointers to bitfield members not allowed"));
1597 /* FIXME we need a way to do "want_address" equivalent */
1599 error (_("Cannot reference non-static field \"%s\""), fieldname
);
1603 /* FIXME add other scoped-reference cases here */
1605 /* Do a last-ditch lookup. */
1606 return gen_maybe_namespace_elt (exp
, ax
, value
, type
, fieldname
);
1609 /* C++: Return the member NAME of the namespace given by the type
1613 gen_namespace_elt (struct expression
*exp
,
1614 struct agent_expr
*ax
, struct axs_value
*value
,
1615 const struct type
*curtype
, char *name
)
1617 int found
= gen_maybe_namespace_elt (exp
, ax
, value
, curtype
, name
);
1620 error (_("No symbol \"%s\" in namespace \"%s\"."),
1621 name
, TYPE_TAG_NAME (curtype
));
1626 /* A helper function used by value_namespace_elt and
1627 value_struct_elt_for_reference. It looks up NAME inside the
1628 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1629 is a class and NAME refers to a type in CURTYPE itself (as opposed
1630 to, say, some base class of CURTYPE). */
1633 gen_maybe_namespace_elt (struct expression
*exp
,
1634 struct agent_expr
*ax
, struct axs_value
*value
,
1635 const struct type
*curtype
, char *name
)
1637 const char *namespace_name
= TYPE_TAG_NAME (curtype
);
1640 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
1641 block_for_pc (ax
->scope
),
1647 gen_var_ref (exp
->gdbarch
, ax
, value
, sym
);
1649 if (value
->optimized_out
)
1650 error (_("`%s' has been optimized out, cannot use"),
1651 SYMBOL_PRINT_NAME (sym
));
1658 gen_aggregate_elt_ref (struct expression
*exp
,
1659 struct agent_expr
*ax
, struct axs_value
*value
,
1660 struct type
*type
, char *field
,
1661 char *operator_name
, char *operand_name
)
1663 switch (TYPE_CODE (type
))
1665 case TYPE_CODE_STRUCT
:
1666 case TYPE_CODE_UNION
:
1667 return gen_struct_elt_for_reference (exp
, ax
, value
, type
, field
);
1669 case TYPE_CODE_NAMESPACE
:
1670 return gen_namespace_elt (exp
, ax
, value
, type
, field
);
1673 internal_error (__FILE__
, __LINE__
,
1674 _("non-aggregate type in gen_aggregate_elt_ref"));
1680 /* Generate code for GDB's magical `repeat' operator.
1681 LVALUE @ INT creates an array INT elements long, and whose elements
1682 have the same type as LVALUE, located in memory so that LVALUE is
1683 its first element. For example, argv[0]@argc gives you the array
1684 of command-line arguments.
1686 Unfortunately, because we have to know the types before we actually
1687 have a value for the expression, we can't implement this perfectly
1688 without changing the type system, having values that occupy two
1689 stack slots, doing weird things with sizeof, etc. So we require
1690 the right operand to be a constant expression. */
1692 gen_repeat (struct expression
*exp
, union exp_element
**pc
,
1693 struct agent_expr
*ax
, struct axs_value
*value
)
1695 struct axs_value value1
;
1697 /* We don't want to turn this into an rvalue, so no conversions
1699 gen_expr (exp
, pc
, ax
, &value1
);
1700 if (value1
.kind
!= axs_lvalue_memory
)
1701 error (_("Left operand of `@' must be an object in memory."));
1703 /* Evaluate the length; it had better be a constant. */
1705 struct value
*v
= const_expr (pc
);
1709 error (_("Right operand of `@' must be a constant, in agent expressions."));
1710 if (TYPE_CODE (value_type (v
)) != TYPE_CODE_INT
)
1711 error (_("Right operand of `@' must be an integer."));
1712 length
= value_as_long (v
);
1714 error (_("Right operand of `@' must be positive."));
1716 /* The top of the stack is already the address of the object, so
1717 all we need to do is frob the type of the lvalue. */
1719 /* FIXME-type-allocation: need a way to free this type when we are
1722 = lookup_array_range_type (value1
.type
, 0, length
- 1);
1724 value
->kind
= axs_lvalue_memory
;
1725 value
->type
= array
;
1731 /* Emit code for the `sizeof' operator.
1732 *PC should point at the start of the operand expression; we advance it
1733 to the first instruction after the operand. */
1735 gen_sizeof (struct expression
*exp
, union exp_element
**pc
,
1736 struct agent_expr
*ax
, struct axs_value
*value
,
1737 struct type
*size_type
)
1739 /* We don't care about the value of the operand expression; we only
1740 care about its type. However, in the current arrangement, the
1741 only way to find an expression's type is to generate code for it.
1742 So we generate code for the operand, and then throw it away,
1743 replacing it with code that simply pushes its size. */
1744 int start
= ax
->len
;
1746 gen_expr (exp
, pc
, ax
, value
);
1748 /* Throw away the code we just generated. */
1751 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
1752 value
->kind
= axs_rvalue
;
1753 value
->type
= size_type
;
1757 /* Generating bytecode from GDB expressions: general recursive thingy */
1760 /* A gen_expr function written by a Gen-X'er guy.
1761 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1763 gen_expr (struct expression
*exp
, union exp_element
**pc
,
1764 struct agent_expr
*ax
, struct axs_value
*value
)
1766 /* Used to hold the descriptions of operand expressions. */
1767 struct axs_value value1
, value2
, value3
;
1768 enum exp_opcode op
= (*pc
)[0].opcode
, op2
;
1769 int if1
, go1
, if2
, go2
, end
;
1770 struct type
*int_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1772 /* If we're looking at a constant expression, just push its value. */
1774 struct value
*v
= maybe_const_expr (pc
);
1778 ax_const_l (ax
, value_as_long (v
));
1779 value
->kind
= axs_rvalue
;
1780 value
->type
= check_typedef (value_type (v
));
1785 /* Otherwise, go ahead and generate code for it. */
1788 /* Binary arithmetic operators. */
1796 case BINOP_SUBSCRIPT
:
1797 case BINOP_BITWISE_AND
:
1798 case BINOP_BITWISE_IOR
:
1799 case BINOP_BITWISE_XOR
:
1801 case BINOP_NOTEQUAL
:
1807 gen_expr (exp
, pc
, ax
, &value1
);
1808 gen_usual_unary (exp
, ax
, &value1
);
1809 gen_expr_binop_rest (exp
, op
, pc
, ax
, value
, &value1
, &value2
);
1812 case BINOP_LOGICAL_AND
:
1814 /* Generate the obvious sequence of tests and jumps. */
1815 gen_expr (exp
, pc
, ax
, &value1
);
1816 gen_usual_unary (exp
, ax
, &value1
);
1817 if1
= ax_goto (ax
, aop_if_goto
);
1818 go1
= ax_goto (ax
, aop_goto
);
1819 ax_label (ax
, if1
, ax
->len
);
1820 gen_expr (exp
, pc
, ax
, &value2
);
1821 gen_usual_unary (exp
, ax
, &value2
);
1822 if2
= ax_goto (ax
, aop_if_goto
);
1823 go2
= ax_goto (ax
, aop_goto
);
1824 ax_label (ax
, if2
, ax
->len
);
1826 end
= ax_goto (ax
, aop_goto
);
1827 ax_label (ax
, go1
, ax
->len
);
1828 ax_label (ax
, go2
, ax
->len
);
1830 ax_label (ax
, end
, ax
->len
);
1831 value
->kind
= axs_rvalue
;
1832 value
->type
= int_type
;
1835 case BINOP_LOGICAL_OR
:
1837 /* Generate the obvious sequence of tests and jumps. */
1838 gen_expr (exp
, pc
, ax
, &value1
);
1839 gen_usual_unary (exp
, ax
, &value1
);
1840 if1
= ax_goto (ax
, aop_if_goto
);
1841 gen_expr (exp
, pc
, ax
, &value2
);
1842 gen_usual_unary (exp
, ax
, &value2
);
1843 if2
= ax_goto (ax
, aop_if_goto
);
1845 end
= ax_goto (ax
, aop_goto
);
1846 ax_label (ax
, if1
, ax
->len
);
1847 ax_label (ax
, if2
, ax
->len
);
1849 ax_label (ax
, end
, ax
->len
);
1850 value
->kind
= axs_rvalue
;
1851 value
->type
= int_type
;
1856 gen_expr (exp
, pc
, ax
, &value1
);
1857 gen_usual_unary (exp
, ax
, &value1
);
1858 /* For (A ? B : C), it's easiest to generate subexpression
1859 bytecodes in order, but if_goto jumps on true, so we invert
1860 the sense of A. Then we can do B by dropping through, and
1862 gen_logical_not (ax
, &value1
, int_type
);
1863 if1
= ax_goto (ax
, aop_if_goto
);
1864 gen_expr (exp
, pc
, ax
, &value2
);
1865 gen_usual_unary (exp
, ax
, &value2
);
1866 end
= ax_goto (ax
, aop_goto
);
1867 ax_label (ax
, if1
, ax
->len
);
1868 gen_expr (exp
, pc
, ax
, &value3
);
1869 gen_usual_unary (exp
, ax
, &value3
);
1870 ax_label (ax
, end
, ax
->len
);
1871 /* This is arbitary - what if B and C are incompatible types? */
1872 value
->type
= value2
.type
;
1873 value
->kind
= value2
.kind
;
1878 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1880 char *name
= internalvar_name ((*pc
)[1].internalvar
);
1881 struct trace_state_variable
*tsv
;
1884 gen_expr (exp
, pc
, ax
, value
);
1885 tsv
= find_trace_state_variable (name
);
1888 ax_tsv (ax
, aop_setv
, tsv
->number
);
1890 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1893 error (_("$%s is not a trace state variable, may not assign to it"), name
);
1896 error (_("May only assign to trace state variables"));
1899 case BINOP_ASSIGN_MODIFY
:
1901 op2
= (*pc
)[0].opcode
;
1904 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1906 char *name
= internalvar_name ((*pc
)[1].internalvar
);
1907 struct trace_state_variable
*tsv
;
1910 tsv
= find_trace_state_variable (name
);
1913 /* The tsv will be the left half of the binary operation. */
1914 ax_tsv (ax
, aop_getv
, tsv
->number
);
1916 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1917 /* Trace state variables are always 64-bit integers. */
1918 value1
.kind
= axs_rvalue
;
1919 value1
.type
= builtin_type (exp
->gdbarch
)->builtin_long_long
;
1920 /* Now do right half of expression. */
1921 gen_expr_binop_rest (exp
, op2
, pc
, ax
, value
, &value1
, &value2
);
1922 /* We have a result of the binary op, set the tsv. */
1923 ax_tsv (ax
, aop_setv
, tsv
->number
);
1925 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1928 error (_("$%s is not a trace state variable, may not assign to it"), name
);
1931 error (_("May only assign to trace state variables"));
1934 /* Note that we need to be a little subtle about generating code
1935 for comma. In C, we can do some optimizations here because
1936 we know the left operand is only being evaluated for effect.
1937 However, if the tracing kludge is in effect, then we always
1938 need to evaluate the left hand side fully, so that all the
1939 variables it mentions get traced. */
1942 gen_expr (exp
, pc
, ax
, &value1
);
1943 /* Don't just dispose of the left operand. We might be tracing,
1944 in which case we want to emit code to trace it if it's an
1946 gen_traced_pop (exp
->gdbarch
, ax
, &value1
);
1947 gen_expr (exp
, pc
, ax
, value
);
1948 /* It's the consumer's responsibility to trace the right operand. */
1951 case OP_LONG
: /* some integer constant */
1953 struct type
*type
= (*pc
)[1].type
;
1954 LONGEST k
= (*pc
)[2].longconst
;
1957 gen_int_literal (ax
, value
, k
, type
);
1962 gen_var_ref (exp
->gdbarch
, ax
, value
, (*pc
)[2].symbol
);
1964 if (value
->optimized_out
)
1965 error (_("`%s' has been optimized out, cannot use"),
1966 SYMBOL_PRINT_NAME ((*pc
)[2].symbol
));
1973 const char *name
= &(*pc
)[2].string
;
1976 (*pc
) += 4 + BYTES_TO_EXP_ELEM ((*pc
)[1].longconst
+ 1);
1977 reg
= user_reg_map_name_to_regnum (exp
->gdbarch
, name
, strlen (name
));
1979 internal_error (__FILE__
, __LINE__
,
1980 _("Register $%s not available"), name
);
1981 if (reg
>= gdbarch_num_regs (exp
->gdbarch
))
1982 error (_("'%s' is a pseudo-register; "
1983 "GDB cannot yet trace pseudoregister contents."),
1985 value
->kind
= axs_lvalue_register
;
1987 value
->type
= register_type (exp
->gdbarch
, reg
);
1991 case OP_INTERNALVAR
:
1993 const char *name
= internalvar_name ((*pc
)[1].internalvar
);
1994 struct trace_state_variable
*tsv
;
1997 tsv
= find_trace_state_variable (name
);
2000 ax_tsv (ax
, aop_getv
, tsv
->number
);
2002 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2003 /* Trace state variables are always 64-bit integers. */
2004 value
->kind
= axs_rvalue
;
2005 value
->type
= builtin_type (exp
->gdbarch
)->builtin_long_long
;
2008 error (_("$%s is not a trace state variable; GDB agent 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
);
2031 struct type
*type
= check_typedef ((*pc
)[1].type
);
2034 gen_expr (exp
, pc
, ax
, value
);
2035 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
2036 it's just a hack for dealing with minsyms; you take some
2037 integer constant, pretend it's the address of an lvalue of
2038 the given type, and dereference it. */
2039 if (value
->kind
!= axs_rvalue
)
2040 /* This would be weird. */
2041 internal_error (__FILE__
, __LINE__
,
2042 _("gen_expr: OP_MEMVAL operand isn't an rvalue???"));
2044 value
->kind
= axs_lvalue_memory
;
2050 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2051 gen_expr (exp
, pc
, ax
, value
);
2052 gen_usual_unary (exp
, ax
, value
);
2057 /* -FOO is equivalent to 0 - FOO. */
2058 gen_int_literal (ax
, &value1
, 0,
2059 builtin_type (exp
->gdbarch
)->builtin_int
);
2060 gen_usual_unary (exp
, ax
, &value1
); /* shouldn't do much */
2061 gen_expr (exp
, pc
, ax
, &value2
);
2062 gen_usual_unary (exp
, ax
, &value2
);
2063 gen_usual_arithmetic (exp
, ax
, &value1
, &value2
);
2064 gen_binop (ax
, value
, &value1
, &value2
, aop_sub
, aop_sub
, 1, "negation");
2067 case UNOP_LOGICAL_NOT
:
2069 gen_expr (exp
, pc
, ax
, value
);
2070 gen_usual_unary (exp
, ax
, value
);
2071 gen_logical_not (ax
, value
, int_type
);
2074 case UNOP_COMPLEMENT
:
2076 gen_expr (exp
, pc
, ax
, value
);
2077 gen_usual_unary (exp
, ax
, value
);
2078 gen_integral_promotions (exp
, ax
, value
);
2079 gen_complement (ax
, value
);
2084 gen_expr (exp
, pc
, ax
, value
);
2085 gen_usual_unary (exp
, ax
, value
);
2086 if (!pointer_type (value
->type
))
2087 error (_("Argument of unary `*' is not a pointer."));
2088 gen_deref (ax
, value
);
2093 gen_expr (exp
, pc
, ax
, value
);
2094 gen_address_of (ax
, value
);
2099 /* Notice that gen_sizeof handles its own operand, unlike most
2100 of the other unary operator functions. This is because we
2101 have to throw away the code we generate. */
2102 gen_sizeof (exp
, pc
, ax
, value
,
2103 builtin_type (exp
->gdbarch
)->builtin_int
);
2106 case STRUCTOP_STRUCT
:
2109 int length
= (*pc
)[1].longconst
;
2110 char *name
= &(*pc
)[2].string
;
2112 (*pc
) += 4 + BYTES_TO_EXP_ELEM (length
+ 1);
2113 gen_expr (exp
, pc
, ax
, value
);
2114 if (op
== STRUCTOP_STRUCT
)
2115 gen_struct_ref (exp
, ax
, value
, name
, ".", "structure or union");
2116 else if (op
== STRUCTOP_PTR
)
2117 gen_struct_ref (exp
, ax
, value
, name
, "->",
2118 "pointer to a structure or union");
2120 /* If this `if' chain doesn't handle it, then the case list
2121 shouldn't mention it, and we shouldn't be here. */
2122 internal_error (__FILE__
, __LINE__
,
2123 _("gen_expr: unhandled struct case"));
2130 struct symbol
*func
, *sym
;
2133 func
= block_linkage_function (block_for_pc (ax
->scope
));
2134 this_name
= language_def (SYMBOL_LANGUAGE (func
))->la_name_of_this
;
2135 b
= SYMBOL_BLOCK_VALUE (func
);
2137 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2138 symbol instead of the LOC_ARG one (if both exist). */
2139 sym
= lookup_block_symbol (b
, this_name
, VAR_DOMAIN
);
2141 error (_("no `%s' found"), this_name
);
2143 gen_var_ref (exp
->gdbarch
, ax
, value
, sym
);
2145 if (value
->optimized_out
)
2146 error (_("`%s' has been optimized out, cannot use"),
2147 SYMBOL_PRINT_NAME (sym
));
2155 struct type
*type
= (*pc
)[1].type
;
2156 int length
= longest_to_int ((*pc
)[2].longconst
);
2157 char *name
= &(*pc
)[3].string
;
2160 found
= gen_aggregate_elt_ref (exp
, ax
, value
, type
, name
,
2163 error (_("There is no field named %s"), name
);
2164 (*pc
) += 5 + BYTES_TO_EXP_ELEM (length
+ 1);
2169 error (_("Attempt to use a type name as an expression."));
2172 error (_("Unsupported operator %s (%d) in expression."),
2173 op_string (op
), op
);
2177 /* This handles the middle-to-right-side of code generation for binary
2178 expressions, which is shared between regular binary operations and
2179 assign-modify (+= and friends) expressions. */
2182 gen_expr_binop_rest (struct expression
*exp
,
2183 enum exp_opcode op
, union exp_element
**pc
,
2184 struct agent_expr
*ax
, struct axs_value
*value
,
2185 struct axs_value
*value1
, struct axs_value
*value2
)
2187 struct type
*int_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2189 gen_expr (exp
, pc
, ax
, value2
);
2190 gen_usual_unary (exp
, ax
, value2
);
2191 gen_usual_arithmetic (exp
, ax
, value1
, value2
);
2195 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
2196 && pointer_type (value2
->type
))
2198 /* Swap the values and proceed normally. */
2199 ax_simple (ax
, aop_swap
);
2200 gen_ptradd (ax
, value
, value2
, value1
);
2202 else if (pointer_type (value1
->type
)
2203 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
2204 gen_ptradd (ax
, value
, value1
, value2
);
2206 gen_binop (ax
, value
, value1
, value2
,
2207 aop_add
, aop_add
, 1, "addition");
2210 if (pointer_type (value1
->type
)
2211 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
2212 gen_ptrsub (ax
,value
, value1
, value2
);
2213 else if (pointer_type (value1
->type
)
2214 && pointer_type (value2
->type
))
2215 /* FIXME --- result type should be ptrdiff_t */
2216 gen_ptrdiff (ax
, value
, value1
, value2
,
2217 builtin_type (exp
->gdbarch
)->builtin_long
);
2219 gen_binop (ax
, value
, value1
, value2
,
2220 aop_sub
, aop_sub
, 1, "subtraction");
2223 gen_binop (ax
, value
, value1
, value2
,
2224 aop_mul
, aop_mul
, 1, "multiplication");
2227 gen_binop (ax
, value
, value1
, value2
,
2228 aop_div_signed
, aop_div_unsigned
, 1, "division");
2231 gen_binop (ax
, value
, value1
, value2
,
2232 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
2235 gen_binop (ax
, value
, value1
, value2
,
2236 aop_lsh
, aop_lsh
, 1, "left shift");
2239 gen_binop (ax
, value
, value1
, value2
,
2240 aop_rsh_signed
, aop_rsh_unsigned
, 1, "right shift");
2242 case BINOP_SUBSCRIPT
:
2246 if (binop_types_user_defined_p (op
, value1
->type
, value2
->type
))
2249 cannot subscript requested type: cannot call user defined functions"));
2253 /* If the user attempts to subscript something that is not
2254 an array or pointer type (like a plain int variable for
2255 example), then report this as an error. */
2256 type
= check_typedef (value1
->type
);
2257 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
2258 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
2260 if (TYPE_NAME (type
))
2261 error (_("cannot subscript something of type `%s'"),
2264 error (_("cannot subscript requested type"));
2268 if (!is_integral_type (value2
->type
))
2269 error (_("Argument to arithmetic operation not a number or boolean."));
2271 gen_ptradd (ax
, value
, value1
, value2
);
2272 gen_deref (ax
, value
);
2275 case BINOP_BITWISE_AND
:
2276 gen_binop (ax
, value
, value1
, value2
,
2277 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
2280 case BINOP_BITWISE_IOR
:
2281 gen_binop (ax
, value
, value1
, value2
,
2282 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
2285 case BINOP_BITWISE_XOR
:
2286 gen_binop (ax
, value
, value1
, value2
,
2287 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
2291 gen_equal (ax
, value
, value1
, value2
, int_type
);
2294 case BINOP_NOTEQUAL
:
2295 gen_equal (ax
, value
, value1
, value2
, int_type
);
2296 gen_logical_not (ax
, value
, int_type
);
2300 gen_less (ax
, value
, value1
, value2
, int_type
);
2304 ax_simple (ax
, aop_swap
);
2305 gen_less (ax
, value
, value1
, value2
, int_type
);
2309 ax_simple (ax
, aop_swap
);
2310 gen_less (ax
, value
, value1
, value2
, int_type
);
2311 gen_logical_not (ax
, value
, int_type
);
2315 gen_less (ax
, value
, value1
, value2
, int_type
);
2316 gen_logical_not (ax
, value
, int_type
);
2320 /* We should only list operators in the outer case statement
2321 that we actually handle in the inner case statement. */
2322 internal_error (__FILE__
, __LINE__
,
2323 _("gen_expr: op case sets don't match"));
2328 /* Given a single variable and a scope, generate bytecodes to trace
2329 its value. This is for use in situations where we have only a
2330 variable's name, and no parsed expression; for instance, when the
2331 name comes from a list of local variables of a function. */
2334 gen_trace_for_var (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2337 struct cleanup
*old_chain
= 0;
2338 struct agent_expr
*ax
= new_agent_expr (gdbarch
, scope
);
2339 struct axs_value value
;
2341 old_chain
= make_cleanup_free_agent_expr (ax
);
2344 gen_var_ref (gdbarch
, ax
, &value
, var
);
2346 /* If there is no actual variable to trace, flag it by returning
2347 an empty agent expression. */
2348 if (value
.optimized_out
)
2350 do_cleanups (old_chain
);
2354 /* Make sure we record the final object, and get rid of it. */
2355 gen_traced_pop (gdbarch
, ax
, &value
);
2357 /* Oh, and terminate. */
2358 ax_simple (ax
, aop_end
);
2360 /* We have successfully built the agent expr, so cancel the cleanup
2361 request. If we add more cleanups that we always want done, this
2362 will have to get more complicated. */
2363 discard_cleanups (old_chain
);
2367 /* Generating bytecode from GDB expressions: driver */
2369 /* Given a GDB expression EXPR, return bytecode to trace its value.
2370 The result will use the `trace' and `trace_quick' bytecodes to
2371 record the value of all memory touched by the expression. The
2372 caller can then use the ax_reqs function to discover which
2373 registers it relies upon. */
2375 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2377 struct cleanup
*old_chain
= 0;
2378 struct agent_expr
*ax
= new_agent_expr (expr
->gdbarch
, scope
);
2379 union exp_element
*pc
;
2380 struct axs_value value
;
2382 old_chain
= make_cleanup_free_agent_expr (ax
);
2386 value
.optimized_out
= 0;
2387 gen_expr (expr
, &pc
, ax
, &value
);
2389 /* Make sure we record the final object, and get rid of it. */
2390 gen_traced_pop (expr
->gdbarch
, ax
, &value
);
2392 /* Oh, and terminate. */
2393 ax_simple (ax
, aop_end
);
2395 /* We have successfully built the agent expr, so cancel the cleanup
2396 request. If we add more cleanups that we always want done, this
2397 will have to get more complicated. */
2398 discard_cleanups (old_chain
);
2402 /* Given a GDB expression EXPR, return a bytecode sequence that will
2403 evaluate and return a result. The bytecodes will do a direct
2404 evaluation, using the current data on the target, rather than
2405 recording blocks of memory and registers for later use, as
2406 gen_trace_for_expr does. The generated bytecode sequence leaves
2407 the result of expression evaluation on the top of the stack. */
2410 gen_eval_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2412 struct cleanup
*old_chain
= 0;
2413 struct agent_expr
*ax
= new_agent_expr (expr
->gdbarch
, scope
);
2414 union exp_element
*pc
;
2415 struct axs_value value
;
2417 old_chain
= make_cleanup_free_agent_expr (ax
);
2421 value
.optimized_out
= 0;
2422 gen_expr (expr
, &pc
, ax
, &value
);
2424 require_rvalue (ax
, &value
);
2426 /* Oh, and terminate. */
2427 ax_simple (ax
, aop_end
);
2429 /* We have successfully built the agent expr, so cancel the cleanup
2430 request. If we add more cleanups that we always want done, this
2431 will have to get more complicated. */
2432 discard_cleanups (old_chain
);
2437 agent_command (char *exp
, int from_tty
)
2439 struct cleanup
*old_chain
= 0;
2440 struct expression
*expr
;
2441 struct agent_expr
*agent
;
2442 struct frame_info
*fi
= get_current_frame (); /* need current scope */
2444 /* We don't deal with overlay debugging at the moment. We need to
2445 think more carefully about this. If you copy this code into
2446 another command, change the error message; the user shouldn't
2447 have to know anything about agent expressions. */
2448 if (overlay_debugging
)
2449 error (_("GDB can't do agent expression translation with overlays."));
2452 error_no_arg (_("expression to translate"));
2454 expr
= parse_expression (exp
);
2455 old_chain
= make_cleanup (free_current_contents
, &expr
);
2456 agent
= gen_trace_for_expr (get_frame_pc (fi
), expr
);
2457 make_cleanup_free_agent_expr (agent
);
2459 ax_print (gdb_stdout
, agent
);
2461 /* It would be nice to call ax_reqs here to gather some general info
2462 about the expression, and then print out the result. */
2464 do_cleanups (old_chain
);
2468 /* Parse the given expression, compile it into an agent expression
2469 that does direct evaluation, and display the resulting
2473 agent_eval_command (char *exp
, int from_tty
)
2475 struct cleanup
*old_chain
= 0;
2476 struct expression
*expr
;
2477 struct agent_expr
*agent
;
2478 struct frame_info
*fi
= get_current_frame (); /* need current scope */
2480 /* We don't deal with overlay debugging at the moment. We need to
2481 think more carefully about this. If you copy this code into
2482 another command, change the error message; the user shouldn't
2483 have to know anything about agent expressions. */
2484 if (overlay_debugging
)
2485 error (_("GDB can't do agent expression translation with overlays."));
2488 error_no_arg (_("expression to translate"));
2490 expr
= parse_expression (exp
);
2491 old_chain
= make_cleanup (free_current_contents
, &expr
);
2492 agent
= gen_eval_for_expr (get_frame_pc (fi
), expr
);
2493 make_cleanup_free_agent_expr (agent
);
2495 ax_print (gdb_stdout
, agent
);
2497 /* It would be nice to call ax_reqs here to gather some general info
2498 about the expression, and then print out the result. */
2500 do_cleanups (old_chain
);
2505 /* Initialization code. */
2507 void _initialize_ax_gdb (void);
2509 _initialize_ax_gdb (void)
2511 add_cmd ("agent", class_maintenance
, agent_command
,
2512 _("Translate an expression into remote agent bytecode for tracing."),
2515 add_cmd ("agent-eval", class_maintenance
, agent_eval_command
,
2516 _("Translate an expression into remote agent bytecode for evaluation."),
This page took 0.083353 seconds and 4 git commands to generate.