1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2007, 2008, 2009, 2010, 2011
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
*,
76 static void gen_sign_extend (struct agent_expr
*, struct type
*);
77 static void gen_extend (struct agent_expr
*, struct type
*);
78 static void gen_fetch (struct agent_expr
*, struct type
*);
79 static void gen_left_shift (struct agent_expr
*, int);
82 static void gen_frame_args_address (struct gdbarch
*, struct agent_expr
*);
83 static void gen_frame_locals_address (struct gdbarch
*, struct agent_expr
*);
84 static void gen_offset (struct agent_expr
*ax
, int offset
);
85 static void gen_sym_offset (struct agent_expr
*, struct symbol
*);
86 static void gen_var_ref (struct gdbarch
*, struct agent_expr
*ax
,
87 struct axs_value
*value
, struct symbol
*var
);
90 static void gen_int_literal (struct agent_expr
*ax
,
91 struct axs_value
*value
,
92 LONGEST k
, struct type
*type
);
95 static void require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
);
96 static void gen_usual_unary (struct expression
*exp
, struct agent_expr
*ax
,
97 struct axs_value
*value
);
98 static int type_wider_than (struct type
*type1
, struct type
*type2
);
99 static struct type
*max_type (struct type
*type1
, struct type
*type2
);
100 static void gen_conversion (struct agent_expr
*ax
,
101 struct type
*from
, struct type
*to
);
102 static int is_nontrivial_conversion (struct type
*from
, struct type
*to
);
103 static void gen_usual_arithmetic (struct expression
*exp
,
104 struct agent_expr
*ax
,
105 struct axs_value
*value1
,
106 struct axs_value
*value2
);
107 static void gen_integral_promotions (struct expression
*exp
,
108 struct agent_expr
*ax
,
109 struct axs_value
*value
);
110 static void gen_cast (struct agent_expr
*ax
,
111 struct axs_value
*value
, struct type
*type
);
112 static void gen_scale (struct agent_expr
*ax
,
113 enum agent_op op
, struct type
*type
);
114 static void gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
115 struct axs_value
*value1
, struct axs_value
*value2
);
116 static void gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
117 struct axs_value
*value1
, struct axs_value
*value2
);
118 static void gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
119 struct axs_value
*value1
, struct axs_value
*value2
,
120 struct type
*result_type
);
121 static void gen_binop (struct agent_expr
*ax
,
122 struct axs_value
*value
,
123 struct axs_value
*value1
,
124 struct axs_value
*value2
,
126 enum agent_op op_unsigned
, int may_carry
, char *name
);
127 static void gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
128 struct type
*result_type
);
129 static void gen_complement (struct agent_expr
*ax
, struct axs_value
*value
);
130 static void gen_deref (struct agent_expr
*, struct axs_value
*);
131 static void gen_address_of (struct agent_expr
*, struct axs_value
*);
132 static void gen_bitfield_ref (struct expression
*exp
, struct agent_expr
*ax
,
133 struct axs_value
*value
,
134 struct type
*type
, int start
, int end
);
135 static void gen_primitive_field (struct expression
*exp
,
136 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 expression
*exp
,
140 struct agent_expr
*ax
,
141 struct axs_value
*value
,
142 char *field
, int offset
,
144 static void gen_struct_ref (struct expression
*exp
, struct agent_expr
*ax
,
145 struct axs_value
*value
,
147 char *operator_name
, char *operand_name
);
148 static void gen_static_field (struct gdbarch
*gdbarch
,
149 struct agent_expr
*ax
, struct axs_value
*value
,
150 struct type
*type
, int fieldno
);
151 static void gen_repeat (struct expression
*exp
, union exp_element
**pc
,
152 struct agent_expr
*ax
, struct axs_value
*value
);
153 static void gen_sizeof (struct expression
*exp
, union exp_element
**pc
,
154 struct agent_expr
*ax
, struct axs_value
*value
,
155 struct type
*size_type
);
156 static void gen_expr (struct expression
*exp
, union exp_element
**pc
,
157 struct agent_expr
*ax
, struct axs_value
*value
);
158 static void gen_expr_binop_rest (struct expression
*exp
,
159 enum exp_opcode op
, union exp_element
**pc
,
160 struct agent_expr
*ax
,
161 struct axs_value
*value
,
162 struct axs_value
*value1
,
163 struct axs_value
*value2
);
165 static void agent_command (char *exp
, int from_tty
);
168 /* Detecting constant expressions. */
170 /* If the variable reference at *PC is a constant, return its value.
171 Otherwise, return zero.
173 Hey, Wally! How can a variable reference be a constant?
175 Well, Beav, this function really handles the OP_VAR_VALUE operator,
176 not specifically variable references. GDB uses OP_VAR_VALUE to
177 refer to any kind of symbolic reference: function names, enum
178 elements, and goto labels are all handled through the OP_VAR_VALUE
179 operator, even though they're constants. It makes sense given the
182 Gee, Wally, don'cha wonder sometimes if data representations that
183 subvert commonly accepted definitions of terms in favor of heavily
184 context-specific interpretations are really just a tool of the
185 programming hegemony to preserve their power and exclude the
188 static struct value
*
189 const_var_ref (struct symbol
*var
)
191 struct type
*type
= SYMBOL_TYPE (var
);
193 switch (SYMBOL_CLASS (var
))
196 return value_from_longest (type
, (LONGEST
) SYMBOL_VALUE (var
));
199 return value_from_pointer (type
, (CORE_ADDR
) SYMBOL_VALUE_ADDRESS (var
));
207 /* If the expression starting at *PC has a constant value, return it.
208 Otherwise, return zero. If we return a value, then *PC will be
209 advanced to the end of it. If we return zero, *PC could be
211 static struct value
*
212 const_expr (union exp_element
**pc
)
214 enum exp_opcode op
= (*pc
)->opcode
;
221 struct type
*type
= (*pc
)[1].type
;
222 LONGEST k
= (*pc
)[2].longconst
;
225 return value_from_longest (type
, k
);
230 struct value
*v
= const_var_ref ((*pc
)[2].symbol
);
236 /* We could add more operators in here. */
240 v1
= const_expr (pc
);
242 return value_neg (v1
);
252 /* Like const_expr, but guarantee also that *PC is undisturbed if the
253 expression is not constant. */
254 static struct value
*
255 maybe_const_expr (union exp_element
**pc
)
257 union exp_element
*tentative_pc
= *pc
;
258 struct value
*v
= const_expr (&tentative_pc
);
260 /* If we got a value, then update the real PC. */
268 /* Generating bytecode from GDB expressions: general assumptions */
270 /* Here are a few general assumptions made throughout the code; if you
271 want to make a change that contradicts one of these, then you'd
272 better scan things pretty thoroughly.
274 - We assume that all values occupy one stack element. For example,
275 sometimes we'll swap to get at the left argument to a binary
276 operator. If we decide that void values should occupy no stack
277 elements, or that synthetic arrays (whose size is determined at
278 run time, created by the `@' operator) should occupy two stack
279 elements (address and length), then this will cause trouble.
281 - We assume the stack elements are infinitely wide, and that we
282 don't have to worry what happens if the user requests an
283 operation that is wider than the actual interpreter's stack.
284 That is, it's up to the interpreter to handle directly all the
285 integer widths the user has access to. (Woe betide the language
288 - We don't support side effects. Thus, we don't have to worry about
289 GCC's generalized lvalues, function calls, etc.
291 - We don't support floating point. Many places where we switch on
292 some type don't bother to include cases for floating point; there
293 may be even more subtle ways this assumption exists. For
294 example, the arguments to % must be integers.
296 - We assume all subexpressions have a static, unchanging type. If
297 we tried to support convenience variables, this would be a
300 - All values on the stack should always be fully zero- or
303 (I wasn't sure whether to choose this or its opposite --- that
304 only addresses are assumed extended --- but it turns out that
305 neither convention completely eliminates spurious extend
306 operations (if everything is always extended, then you have to
307 extend after add, because it could overflow; if nothing is
308 extended, then you end up producing extends whenever you change
309 sizes), and this is simpler.) */
312 /* Generating bytecode from GDB expressions: the `trace' kludge */
314 /* The compiler in this file is a general-purpose mechanism for
315 translating GDB expressions into bytecode. One ought to be able to
316 find a million and one uses for it.
318 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
319 of expediency. Let he who is without sin cast the first stone.
321 For the data tracing facility, we need to insert `trace' bytecodes
322 before each data fetch; this records all the memory that the
323 expression touches in the course of evaluation, so that memory will
324 be available when the user later tries to evaluate the expression
327 This should be done (I think) in a post-processing pass, that walks
328 an arbitrary agent expression and inserts `trace' operations at the
329 appropriate points. But it's much faster to just hack them
330 directly into the code. And since we're in a crunch, that's what
333 Setting the flag trace_kludge to non-zero enables the code that
334 emits the trace bytecodes at the appropriate points. */
337 /* Scan for all static fields in the given class, including any base
338 classes, and generate tracing bytecodes for each. */
341 gen_trace_static_fields (struct gdbarch
*gdbarch
,
342 struct agent_expr
*ax
,
345 int i
, nbases
= TYPE_N_BASECLASSES (type
);
346 struct axs_value value
;
348 CHECK_TYPEDEF (type
);
350 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
352 if (field_is_static (&TYPE_FIELD (type
, i
)))
354 gen_static_field (gdbarch
, ax
, &value
, type
, i
);
355 if (value
.optimized_out
)
359 case axs_lvalue_memory
:
361 int length
= TYPE_LENGTH (check_typedef (value
.type
));
363 ax_const_l (ax
, length
);
364 ax_simple (ax
, aop_trace
);
368 case axs_lvalue_register
:
369 /* We don't actually need the register's value to be pushed,
370 just note that we need it to be collected. */
371 ax_reg_mask (ax
, value
.u
.reg
);
379 /* Now scan through base classes recursively. */
380 for (i
= 0; i
< nbases
; i
++)
382 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
384 gen_trace_static_fields (gdbarch
, ax
, basetype
);
388 /* Trace the lvalue on the stack, if it needs it. In either case, pop
389 the value. Useful on the left side of a comma, and at the end of
390 an expression being used for tracing. */
392 gen_traced_pop (struct gdbarch
*gdbarch
,
393 struct agent_expr
*ax
, struct axs_value
*value
)
399 /* We don't trace rvalues, just the lvalues necessary to
400 produce them. So just dispose of this value. */
401 ax_simple (ax
, aop_pop
);
404 case axs_lvalue_memory
:
406 int length
= TYPE_LENGTH (check_typedef (value
->type
));
408 /* There's no point in trying to use a trace_quick bytecode
409 here, since "trace_quick SIZE pop" is three bytes, whereas
410 "const8 SIZE trace" is also three bytes, does the same
411 thing, and the simplest code which generates that will also
412 work correctly for objects with large sizes. */
413 ax_const_l (ax
, length
);
414 ax_simple (ax
, aop_trace
);
418 case axs_lvalue_register
:
419 /* We don't actually need the register's value to be on the
420 stack, and the target will get heartburn if the register is
421 larger than will fit in a stack, so just mark it for
422 collection and be done with it. */
423 ax_reg_mask (ax
, value
->u
.reg
);
427 /* If we're not tracing, just pop the value. */
428 ax_simple (ax
, aop_pop
);
430 /* To trace C++ classes with static fields stored elsewhere. */
432 && (TYPE_CODE (value
->type
) == TYPE_CODE_STRUCT
433 || TYPE_CODE (value
->type
) == TYPE_CODE_UNION
))
434 gen_trace_static_fields (gdbarch
, ax
, value
->type
);
439 /* Generating bytecode from GDB expressions: helper functions */
441 /* Assume that the lower bits of the top of the stack is a value of
442 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
444 gen_sign_extend (struct agent_expr
*ax
, struct type
*type
)
446 /* Do we need to sign-extend this? */
447 if (!TYPE_UNSIGNED (type
))
448 ax_ext (ax
, TYPE_LENGTH (type
) * TARGET_CHAR_BIT
);
452 /* Assume the lower bits of the top of the stack hold a value of type
453 TYPE, and the upper bits are garbage. Sign-extend or truncate as
456 gen_extend (struct agent_expr
*ax
, struct type
*type
)
458 int bits
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
461 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, bits
));
465 /* Assume that the top of the stack contains a value of type "pointer
466 to TYPE"; generate code to fetch its value. Note that TYPE is the
467 target type, not the pointer type. */
469 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
473 /* Record the area of memory we're about to fetch. */
474 ax_trace_quick (ax
, TYPE_LENGTH (type
));
477 switch (TYPE_CODE (type
))
485 /* It's a scalar value, so we know how to dereference it. How
486 many bytes long is it? */
487 switch (TYPE_LENGTH (type
))
489 case 8 / TARGET_CHAR_BIT
:
490 ax_simple (ax
, aop_ref8
);
492 case 16 / TARGET_CHAR_BIT
:
493 ax_simple (ax
, aop_ref16
);
495 case 32 / TARGET_CHAR_BIT
:
496 ax_simple (ax
, aop_ref32
);
498 case 64 / TARGET_CHAR_BIT
:
499 ax_simple (ax
, aop_ref64
);
502 /* Either our caller shouldn't have asked us to dereference
503 that pointer (other code's fault), or we're not
504 implementing something we should be (this code's fault).
505 In any case, it's a bug the user shouldn't see. */
507 internal_error (__FILE__
, __LINE__
,
508 _("gen_fetch: strange size"));
511 gen_sign_extend (ax
, type
);
515 /* Either our caller shouldn't have asked us to dereference that
516 pointer (other code's fault), or we're not implementing
517 something we should be (this code's fault). In any case,
518 it's a bug the user shouldn't see. */
519 internal_error (__FILE__
, __LINE__
,
520 _("gen_fetch: bad type code"));
525 /* Generate code to left shift the top of the stack by DISTANCE bits, or
526 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
527 unsigned (logical) right shifts. */
529 gen_left_shift (struct agent_expr
*ax
, int distance
)
533 ax_const_l (ax
, distance
);
534 ax_simple (ax
, aop_lsh
);
536 else if (distance
< 0)
538 ax_const_l (ax
, -distance
);
539 ax_simple (ax
, aop_rsh_unsigned
);
545 /* Generating bytecode from GDB expressions: symbol references */
547 /* Generate code to push the base address of the argument portion of
548 the top stack frame. */
550 gen_frame_args_address (struct gdbarch
*gdbarch
, struct agent_expr
*ax
)
553 LONGEST frame_offset
;
555 gdbarch_virtual_frame_pointer (gdbarch
,
556 ax
->scope
, &frame_reg
, &frame_offset
);
557 ax_reg (ax
, frame_reg
);
558 gen_offset (ax
, frame_offset
);
562 /* Generate code to push the base address of the locals portion of the
565 gen_frame_locals_address (struct gdbarch
*gdbarch
, struct agent_expr
*ax
)
568 LONGEST frame_offset
;
570 gdbarch_virtual_frame_pointer (gdbarch
,
571 ax
->scope
, &frame_reg
, &frame_offset
);
572 ax_reg (ax
, frame_reg
);
573 gen_offset (ax
, frame_offset
);
577 /* Generate code to add OFFSET to the top of the stack. Try to
578 generate short and readable code. We use this for getting to
579 variables on the stack, and structure members. If we were
580 programming in ML, it would be clearer why these are the same
583 gen_offset (struct agent_expr
*ax
, int offset
)
585 /* It would suffice to simply push the offset and add it, but this
586 makes it easier to read positive and negative offsets in the
590 ax_const_l (ax
, offset
);
591 ax_simple (ax
, aop_add
);
595 ax_const_l (ax
, -offset
);
596 ax_simple (ax
, aop_sub
);
601 /* In many cases, a symbol's value is the offset from some other
602 address (stack frame, base register, etc.) Generate code to add
603 VAR's value to the top of the stack. */
605 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
607 gen_offset (ax
, SYMBOL_VALUE (var
));
611 /* Generate code for a variable reference to AX. The variable is the
612 symbol VAR. Set VALUE to describe the result. */
615 gen_var_ref (struct gdbarch
*gdbarch
, struct agent_expr
*ax
,
616 struct axs_value
*value
, struct symbol
*var
)
618 /* Dereference any typedefs. */
619 value
->type
= check_typedef (SYMBOL_TYPE (var
));
620 value
->optimized_out
= 0;
622 /* I'm imitating the code in read_var_value. */
623 switch (SYMBOL_CLASS (var
))
625 case LOC_CONST
: /* A constant, like an enum value. */
626 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE (var
));
627 value
->kind
= axs_rvalue
;
630 case LOC_LABEL
: /* A goto label, being used as a value. */
631 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
632 value
->kind
= axs_rvalue
;
635 case LOC_CONST_BYTES
:
636 internal_error (__FILE__
, __LINE__
,
637 _("gen_var_ref: LOC_CONST_BYTES "
638 "symbols are not supported"));
640 /* Variable at a fixed location in memory. Easy. */
642 /* Push the address of the variable. */
643 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (var
));
644 value
->kind
= axs_lvalue_memory
;
647 case LOC_ARG
: /* var lives in argument area of frame */
648 gen_frame_args_address (gdbarch
, ax
);
649 gen_sym_offset (ax
, var
);
650 value
->kind
= axs_lvalue_memory
;
653 case LOC_REF_ARG
: /* As above, but the frame slot really
654 holds the address of the variable. */
655 gen_frame_args_address (gdbarch
, ax
);
656 gen_sym_offset (ax
, var
);
657 /* Don't assume any particular pointer size. */
658 gen_fetch (ax
, builtin_type (gdbarch
)->builtin_data_ptr
);
659 value
->kind
= axs_lvalue_memory
;
662 case LOC_LOCAL
: /* var lives in locals area of frame */
663 gen_frame_locals_address (gdbarch
, ax
);
664 gen_sym_offset (ax
, var
);
665 value
->kind
= axs_lvalue_memory
;
669 error (_("Cannot compute value of typedef `%s'."),
670 SYMBOL_PRINT_NAME (var
));
674 ax_const_l (ax
, BLOCK_START (SYMBOL_BLOCK_VALUE (var
)));
675 value
->kind
= axs_rvalue
;
679 /* Don't generate any code at all; in the process of treating
680 this as an lvalue or rvalue, the caller will generate the
682 value
->kind
= axs_lvalue_register
;
683 value
->u
.reg
= SYMBOL_REGISTER_OPS (var
)->register_number (var
, gdbarch
);
686 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
687 register, not on the stack. Simpler than LOC_REGISTER
688 because it's just like any other case where the thing
689 has a real address. */
690 case LOC_REGPARM_ADDR
:
691 ax_reg (ax
, SYMBOL_REGISTER_OPS (var
)->register_number (var
, gdbarch
));
692 value
->kind
= axs_lvalue_memory
;
697 struct minimal_symbol
*msym
698 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var
), NULL
, NULL
);
701 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var
));
703 /* Push the address of the variable. */
704 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (msym
));
705 value
->kind
= axs_lvalue_memory
;
710 /* FIXME: cagney/2004-01-26: It should be possible to
711 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
712 Unfortunately DWARF 2 stores the frame-base (instead of the
713 function) location in a function's symbol. Oops! For the
714 moment enable this when/where applicable. */
715 SYMBOL_COMPUTED_OPS (var
)->tracepoint_var_ref (var
, gdbarch
, ax
, value
);
718 case LOC_OPTIMIZED_OUT
:
719 /* Flag this, but don't say anything; leave it up to callers to
721 value
->optimized_out
= 1;
725 error (_("Cannot find value of botched symbol `%s'."),
726 SYMBOL_PRINT_NAME (var
));
733 /* Generating bytecode from GDB expressions: literals */
736 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
740 value
->kind
= axs_rvalue
;
741 value
->type
= check_typedef (type
);
746 /* Generating bytecode from GDB expressions: unary conversions, casts */
748 /* Take what's on the top of the stack (as described by VALUE), and
749 try to make an rvalue out of it. Signal an error if we can't do
752 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
754 /* Only deal with scalars, structs and such may be too large
755 to fit in a stack entry. */
756 value
->type
= check_typedef (value
->type
);
757 if (TYPE_CODE (value
->type
) == TYPE_CODE_ARRAY
758 || TYPE_CODE (value
->type
) == TYPE_CODE_STRUCT
759 || TYPE_CODE (value
->type
) == TYPE_CODE_UNION
760 || TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
761 error (_("Value not scalar: cannot be an rvalue."));
766 /* It's already an rvalue. */
769 case axs_lvalue_memory
:
770 /* The top of stack is the address of the object. Dereference. */
771 gen_fetch (ax
, value
->type
);
774 case axs_lvalue_register
:
775 /* There's nothing on the stack, but value->u.reg is the
776 register number containing the value.
778 When we add floating-point support, this is going to have to
779 change. What about SPARC register pairs, for example? */
780 ax_reg (ax
, value
->u
.reg
);
781 gen_extend (ax
, value
->type
);
785 value
->kind
= axs_rvalue
;
789 /* Assume the top of the stack is described by VALUE, and perform the
790 usual unary conversions. This is motivated by ANSI 6.2.2, but of
791 course GDB expressions are not ANSI; they're the mishmash union of
792 a bunch of languages. Rah.
794 NOTE! This function promises to produce an rvalue only when the
795 incoming value is of an appropriate type. In other words, the
796 consumer of the value this function produces may assume the value
797 is an rvalue only after checking its type.
799 The immediate issue is that if the user tries to use a structure or
800 union as an operand of, say, the `+' operator, we don't want to try
801 to convert that structure to an rvalue; require_rvalue will bomb on
802 structs and unions. Rather, we want to simply pass the struct
803 lvalue through unchanged, and let `+' raise an error. */
806 gen_usual_unary (struct expression
*exp
, struct agent_expr
*ax
,
807 struct axs_value
*value
)
809 /* We don't have to generate any code for the usual integral
810 conversions, since values are always represented as full-width on
811 the stack. Should we tweak the type? */
813 /* Some types require special handling. */
814 switch (TYPE_CODE (value
->type
))
816 /* Functions get converted to a pointer to the function. */
818 value
->type
= lookup_pointer_type (value
->type
);
819 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
822 /* Arrays get converted to a pointer to their first element, and
823 are no longer an lvalue. */
824 case TYPE_CODE_ARRAY
:
826 struct type
*elements
= TYPE_TARGET_TYPE (value
->type
);
828 value
->type
= lookup_pointer_type (elements
);
829 value
->kind
= axs_rvalue
;
830 /* We don't need to generate any code; the address of the array
831 is also the address of its first element. */
835 /* Don't try to convert structures and unions to rvalues. Let the
836 consumer signal an error. */
837 case TYPE_CODE_STRUCT
:
838 case TYPE_CODE_UNION
:
841 /* If the value is an enum or a bool, call it an integer. */
844 value
->type
= builtin_type (exp
->gdbarch
)->builtin_int
;
848 /* If the value is an lvalue, dereference it. */
849 require_rvalue (ax
, value
);
853 /* Return non-zero iff the type TYPE1 is considered "wider" than the
854 type TYPE2, according to the rules described in gen_usual_arithmetic. */
856 type_wider_than (struct type
*type1
, struct type
*type2
)
858 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)
859 || (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
860 && TYPE_UNSIGNED (type1
)
861 && !TYPE_UNSIGNED (type2
)));
865 /* Return the "wider" of the two types TYPE1 and TYPE2. */
867 max_type (struct type
*type1
, struct type
*type2
)
869 return type_wider_than (type1
, type2
) ? type1
: type2
;
873 /* Generate code to convert a scalar value of type FROM to type TO. */
875 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
877 /* Perhaps there is a more graceful way to state these rules. */
879 /* If we're converting to a narrower type, then we need to clear out
881 if (TYPE_LENGTH (to
) < TYPE_LENGTH (from
))
882 gen_extend (ax
, from
);
884 /* If the two values have equal width, but different signednesses,
885 then we need to extend. */
886 else if (TYPE_LENGTH (to
) == TYPE_LENGTH (from
))
888 if (TYPE_UNSIGNED (from
) != TYPE_UNSIGNED (to
))
892 /* If we're converting to a wider type, and becoming unsigned, then
893 we need to zero out any possible sign bits. */
894 else if (TYPE_LENGTH (to
) > TYPE_LENGTH (from
))
896 if (TYPE_UNSIGNED (to
))
902 /* Return non-zero iff the type FROM will require any bytecodes to be
903 emitted to be converted to the type TO. */
905 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
907 struct agent_expr
*ax
= new_agent_expr (NULL
, 0);
910 /* Actually generate the code, and see if anything came out. At the
911 moment, it would be trivial to replicate the code in
912 gen_conversion here, but in the future, when we're supporting
913 floating point and the like, it may not be. Doing things this
914 way allows this function to be independent of the logic in
916 gen_conversion (ax
, from
, to
);
917 nontrivial
= ax
->len
> 0;
918 free_agent_expr (ax
);
923 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
924 6.2.1.5) for the two operands of an arithmetic operator. This
925 effectively finds a "least upper bound" type for the two arguments,
926 and promotes each argument to that type. *VALUE1 and *VALUE2
927 describe the values as they are passed in, and as they are left. */
929 gen_usual_arithmetic (struct expression
*exp
, struct agent_expr
*ax
,
930 struct axs_value
*value1
, struct axs_value
*value2
)
932 /* Do the usual binary conversions. */
933 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
934 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
936 /* The ANSI integral promotions seem to work this way: Order the
937 integer types by size, and then by signedness: an n-bit
938 unsigned type is considered "wider" than an n-bit signed
939 type. Promote to the "wider" of the two types, and always
940 promote at least to int. */
941 struct type
*target
= max_type (builtin_type (exp
->gdbarch
)->builtin_int
,
942 max_type (value1
->type
, value2
->type
));
944 /* Deal with value2, on the top of the stack. */
945 gen_conversion (ax
, value2
->type
, target
);
947 /* Deal with value1, not on the top of the stack. Don't
948 generate the `swap' instructions if we're not actually going
950 if (is_nontrivial_conversion (value1
->type
, target
))
952 ax_simple (ax
, aop_swap
);
953 gen_conversion (ax
, value1
->type
, target
);
954 ax_simple (ax
, aop_swap
);
957 value1
->type
= value2
->type
= check_typedef (target
);
962 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
963 the value on the top of the stack, as described by VALUE. Assume
964 the value has integral type. */
966 gen_integral_promotions (struct expression
*exp
, struct agent_expr
*ax
,
967 struct axs_value
*value
)
969 const struct builtin_type
*builtin
= builtin_type (exp
->gdbarch
);
971 if (!type_wider_than (value
->type
, builtin
->builtin_int
))
973 gen_conversion (ax
, value
->type
, builtin
->builtin_int
);
974 value
->type
= builtin
->builtin_int
;
976 else if (!type_wider_than (value
->type
, builtin
->builtin_unsigned_int
))
978 gen_conversion (ax
, value
->type
, builtin
->builtin_unsigned_int
);
979 value
->type
= builtin
->builtin_unsigned_int
;
984 /* Generate code for a cast to TYPE. */
986 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
988 /* GCC does allow casts to yield lvalues, so this should be fixed
989 before merging these changes into the trunk. */
990 require_rvalue (ax
, value
);
991 /* Dereference typedefs. */
992 type
= check_typedef (type
);
994 switch (TYPE_CODE (type
))
998 /* It's implementation-defined, and I'll bet this is what GCC
1002 case TYPE_CODE_ARRAY
:
1003 case TYPE_CODE_STRUCT
:
1004 case TYPE_CODE_UNION
:
1005 case TYPE_CODE_FUNC
:
1006 error (_("Invalid type cast: intended type must be scalar."));
1008 case TYPE_CODE_ENUM
:
1009 case TYPE_CODE_BOOL
:
1010 /* We don't have to worry about the size of the value, because
1011 all our integral values are fully sign-extended, and when
1012 casting pointers we can do anything we like. Is there any
1013 way for us to know what GCC actually does with a cast like
1018 gen_conversion (ax
, value
->type
, type
);
1021 case TYPE_CODE_VOID
:
1022 /* We could pop the value, and rely on everyone else to check
1023 the type and notice that this value doesn't occupy a stack
1024 slot. But for now, leave the value on the stack, and
1025 preserve the "value == stack element" assumption. */
1029 error (_("Casts to requested type are not yet implemented."));
1037 /* Generating bytecode from GDB expressions: arithmetic */
1039 /* Scale the integer on the top of the stack by the size of the target
1040 of the pointer type TYPE. */
1042 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
1044 struct type
*element
= TYPE_TARGET_TYPE (type
);
1046 if (TYPE_LENGTH (element
) != 1)
1048 ax_const_l (ax
, TYPE_LENGTH (element
));
1054 /* Generate code for pointer arithmetic PTR + INT. */
1056 gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
1057 struct axs_value
*value1
, struct axs_value
*value2
)
1059 gdb_assert (pointer_type (value1
->type
));
1060 gdb_assert (TYPE_CODE (value2
->type
) == TYPE_CODE_INT
);
1062 gen_scale (ax
, aop_mul
, value1
->type
);
1063 ax_simple (ax
, aop_add
);
1064 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1065 value
->type
= value1
->type
;
1066 value
->kind
= axs_rvalue
;
1070 /* Generate code for pointer arithmetic PTR - INT. */
1072 gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
1073 struct axs_value
*value1
, struct axs_value
*value2
)
1075 gdb_assert (pointer_type (value1
->type
));
1076 gdb_assert (TYPE_CODE (value2
->type
) == TYPE_CODE_INT
);
1078 gen_scale (ax
, aop_mul
, value1
->type
);
1079 ax_simple (ax
, aop_sub
);
1080 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1081 value
->type
= value1
->type
;
1082 value
->kind
= axs_rvalue
;
1086 /* Generate code for pointer arithmetic PTR - PTR. */
1088 gen_ptrdiff (struct agent_expr
*ax
, struct axs_value
*value
,
1089 struct axs_value
*value1
, struct axs_value
*value2
,
1090 struct type
*result_type
)
1092 gdb_assert (pointer_type (value1
->type
));
1093 gdb_assert (pointer_type (value2
->type
));
1095 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1
->type
))
1096 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2
->type
)))
1098 First argument of `-' is a pointer, but second argument is neither\n\
1099 an integer nor a pointer of the same type."));
1101 ax_simple (ax
, aop_sub
);
1102 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1103 value
->type
= result_type
;
1104 value
->kind
= axs_rvalue
;
1108 gen_equal (struct agent_expr
*ax
, struct axs_value
*value
,
1109 struct axs_value
*value1
, struct axs_value
*value2
,
1110 struct type
*result_type
)
1112 if (pointer_type (value1
->type
) || pointer_type (value2
->type
))
1113 ax_simple (ax
, aop_equal
);
1115 gen_binop (ax
, value
, value1
, value2
,
1116 aop_equal
, aop_equal
, 0, "equal");
1117 value
->type
= result_type
;
1118 value
->kind
= axs_rvalue
;
1122 gen_less (struct agent_expr
*ax
, struct axs_value
*value
,
1123 struct axs_value
*value1
, struct axs_value
*value2
,
1124 struct type
*result_type
)
1126 if (pointer_type (value1
->type
) || pointer_type (value2
->type
))
1127 ax_simple (ax
, aop_less_unsigned
);
1129 gen_binop (ax
, value
, value1
, value2
,
1130 aop_less_signed
, aop_less_unsigned
, 0, "less than");
1131 value
->type
= result_type
;
1132 value
->kind
= axs_rvalue
;
1135 /* Generate code for a binary operator that doesn't do pointer magic.
1136 We set VALUE to describe the result value; we assume VALUE1 and
1137 VALUE2 describe the two operands, and that they've undergone the
1138 usual binary conversions. MAY_CARRY should be non-zero iff the
1139 result needs to be extended. NAME is the English name of the
1140 operator, used in error messages */
1142 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1143 struct axs_value
*value1
, struct axs_value
*value2
,
1144 enum agent_op op
, enum agent_op op_unsigned
,
1145 int may_carry
, char *name
)
1147 /* We only handle INT op INT. */
1148 if ((TYPE_CODE (value1
->type
) != TYPE_CODE_INT
)
1149 || (TYPE_CODE (value2
->type
) != TYPE_CODE_INT
))
1150 error (_("Invalid combination of types in %s."), name
);
1153 TYPE_UNSIGNED (value1
->type
) ? op_unsigned
: op
);
1155 gen_extend (ax
, value1
->type
); /* catch overflow */
1156 value
->type
= value1
->type
;
1157 value
->kind
= axs_rvalue
;
1162 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
1163 struct type
*result_type
)
1165 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
1166 && TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1167 error (_("Invalid type of operand to `!'."));
1169 ax_simple (ax
, aop_log_not
);
1170 value
->type
= result_type
;
1175 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1177 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
)
1178 error (_("Invalid type of operand to `~'."));
1180 ax_simple (ax
, aop_bit_not
);
1181 gen_extend (ax
, value
->type
);
1186 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1188 /* Dereference the value on the top of the stack. */
1190 gen_deref (struct agent_expr
*ax
, struct axs_value
*value
)
1192 /* The caller should check the type, because several operators use
1193 this, and we don't know what error message to generate. */
1194 if (!pointer_type (value
->type
))
1195 internal_error (__FILE__
, __LINE__
,
1196 _("gen_deref: expected a pointer"));
1198 /* We've got an rvalue now, which is a pointer. We want to yield an
1199 lvalue, whose address is exactly that pointer. So we don't
1200 actually emit any code; we just change the type from "Pointer to
1201 T" to "T", and mark the value as an lvalue in memory. Leave it
1202 to the consumer to actually dereference it. */
1203 value
->type
= check_typedef (TYPE_TARGET_TYPE (value
->type
));
1204 if (TYPE_CODE (value
->type
) == TYPE_CODE_VOID
)
1205 error (_("Attempt to dereference a generic pointer."));
1206 value
->kind
= ((TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1207 ? axs_rvalue
: axs_lvalue_memory
);
1211 /* Produce the address of the lvalue on the top of the stack. */
1213 gen_address_of (struct agent_expr
*ax
, struct axs_value
*value
)
1215 /* Special case for taking the address of a function. The ANSI
1216 standard describes this as a special case, too, so this
1217 arrangement is not without motivation. */
1218 if (TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1219 /* The value's already an rvalue on the stack, so we just need to
1221 value
->type
= lookup_pointer_type (value
->type
);
1223 switch (value
->kind
)
1226 error (_("Operand of `&' is an rvalue, which has no address."));
1228 case axs_lvalue_register
:
1229 error (_("Operand of `&' is in a register, and has no address."));
1231 case axs_lvalue_memory
:
1232 value
->kind
= axs_rvalue
;
1233 value
->type
= lookup_pointer_type (value
->type
);
1238 /* Generate code to push the value of a bitfield of a structure whose
1239 address is on the top of the stack. START and END give the
1240 starting and one-past-ending *bit* numbers of the field within the
1243 gen_bitfield_ref (struct expression
*exp
, struct agent_expr
*ax
,
1244 struct axs_value
*value
, struct type
*type
,
1247 /* Note that ops[i] fetches 8 << i bits. */
1248 static enum agent_op ops
[]
1249 = {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1250 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1252 /* We don't want to touch any byte that the bitfield doesn't
1253 actually occupy; we shouldn't make any accesses we're not
1254 explicitly permitted to. We rely here on the fact that the
1255 bytecode `ref' operators work on unaligned addresses.
1257 It takes some fancy footwork to get the stack to work the way
1258 we'd like. Say we're retrieving a bitfield that requires three
1259 fetches. Initially, the stack just contains the address:
1261 For the first fetch, we duplicate the address
1263 then add the byte offset, do the fetch, and shift and mask as
1264 needed, yielding a fragment of the value, properly aligned for
1265 the final bitwise or:
1267 then we swap, and repeat the process:
1268 frag1 addr --- address on top
1269 frag1 addr addr --- duplicate it
1270 frag1 addr frag2 --- get second fragment
1271 frag1 frag2 addr --- swap again
1272 frag1 frag2 frag3 --- get third fragment
1273 Notice that, since the third fragment is the last one, we don't
1274 bother duplicating the address this time. Now we have all the
1275 fragments on the stack, and we can simply `or' them together,
1276 yielding the final value of the bitfield. */
1278 /* The first and one-after-last bits in the field, but rounded down
1279 and up to byte boundaries. */
1280 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1281 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1285 /* current bit offset within the structure */
1288 /* The index in ops of the opcode we're considering. */
1291 /* The number of fragments we generated in the process. Probably
1292 equal to the number of `one' bits in bytesize, but who cares? */
1295 /* Dereference any typedefs. */
1296 type
= check_typedef (type
);
1298 /* Can we fetch the number of bits requested at all? */
1299 if ((end
- start
) > ((1 << num_ops
) * 8))
1300 internal_error (__FILE__
, __LINE__
,
1301 _("gen_bitfield_ref: bitfield too wide"));
1303 /* Note that we know here that we only need to try each opcode once.
1304 That may not be true on machines with weird byte sizes. */
1305 offset
= bound_start
;
1307 for (op
= num_ops
- 1; op
>= 0; op
--)
1309 /* number of bits that ops[op] would fetch */
1310 int op_size
= 8 << op
;
1312 /* The stack at this point, from bottom to top, contains zero or
1313 more fragments, then the address. */
1315 /* Does this fetch fit within the bitfield? */
1316 if (offset
+ op_size
<= bound_end
)
1318 /* Is this the last fragment? */
1319 int last_frag
= (offset
+ op_size
== bound_end
);
1322 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1324 /* Add the offset. */
1325 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1329 /* Record the area of memory we're about to fetch. */
1330 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1333 /* Perform the fetch. */
1334 ax_simple (ax
, ops
[op
]);
1336 /* Shift the bits we have to their proper position.
1337 gen_left_shift will generate right shifts when the operand
1340 A big-endian field diagram to ponder:
1341 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1342 +------++------++------++------++------++------++------++------+
1343 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1345 bit number 16 32 48 53
1346 These are bit numbers as supplied by GDB. Note that the
1347 bit numbers run from right to left once you've fetched the
1350 A little-endian field diagram to ponder:
1351 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1352 +------++------++------++------++------++------++------++------+
1353 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1355 bit number 48 32 16 4 0
1357 In both cases, the most significant end is on the left
1358 (i.e. normal numeric writing order), which means that you
1359 don't go crazy thinking about `left' and `right' shifts.
1361 We don't have to worry about masking yet:
1362 - If they contain garbage off the least significant end, then we
1363 must be looking at the low end of the field, and the right
1364 shift will wipe them out.
1365 - If they contain garbage off the most significant end, then we
1366 must be looking at the most significant end of the word, and
1367 the sign/zero extension will wipe them out.
1368 - If we're in the interior of the word, then there is no garbage
1369 on either end, because the ref operators zero-extend. */
1370 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
1371 gen_left_shift (ax
, end
- (offset
+ op_size
));
1373 gen_left_shift (ax
, offset
- start
);
1376 /* Bring the copy of the address up to the top. */
1377 ax_simple (ax
, aop_swap
);
1384 /* Generate enough bitwise `or' operations to combine all the
1385 fragments we left on the stack. */
1386 while (fragment_count
-- > 1)
1387 ax_simple (ax
, aop_bit_or
);
1389 /* Sign- or zero-extend the value as appropriate. */
1390 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1392 /* This is *not* an lvalue. Ugh. */
1393 value
->kind
= axs_rvalue
;
1397 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1398 is an accumulated offset (in bytes), will be nonzero for objects
1399 embedded in other objects, like C++ base classes. Behavior should
1400 generally follow value_primitive_field. */
1403 gen_primitive_field (struct expression
*exp
,
1404 struct agent_expr
*ax
, struct axs_value
*value
,
1405 int offset
, int fieldno
, struct type
*type
)
1407 /* Is this a bitfield? */
1408 if (TYPE_FIELD_PACKED (type
, fieldno
))
1409 gen_bitfield_ref (exp
, ax
, value
, TYPE_FIELD_TYPE (type
, fieldno
),
1410 (offset
* TARGET_CHAR_BIT
1411 + TYPE_FIELD_BITPOS (type
, fieldno
)),
1412 (offset
* TARGET_CHAR_BIT
1413 + TYPE_FIELD_BITPOS (type
, fieldno
)
1414 + TYPE_FIELD_BITSIZE (type
, fieldno
)));
1417 gen_offset (ax
, offset
1418 + TYPE_FIELD_BITPOS (type
, fieldno
) / TARGET_CHAR_BIT
);
1419 value
->kind
= axs_lvalue_memory
;
1420 value
->type
= TYPE_FIELD_TYPE (type
, fieldno
);
1424 /* Search for the given field in either the given type or one of its
1425 base classes. Return 1 if found, 0 if not. */
1428 gen_struct_ref_recursive (struct expression
*exp
, struct agent_expr
*ax
,
1429 struct axs_value
*value
,
1430 char *field
, int offset
, struct type
*type
)
1433 int nbases
= TYPE_N_BASECLASSES (type
);
1435 CHECK_TYPEDEF (type
);
1437 for (i
= TYPE_NFIELDS (type
) - 1; i
>= nbases
; i
--)
1439 char *this_name
= TYPE_FIELD_NAME (type
, i
);
1443 if (strcmp (field
, this_name
) == 0)
1445 /* Note that bytecodes for the struct's base (aka
1446 "this") will have been generated already, which will
1447 be unnecessary but not harmful if the static field is
1448 being handled as a global. */
1449 if (field_is_static (&TYPE_FIELD (type
, i
)))
1451 gen_static_field (exp
->gdbarch
, ax
, value
, type
, i
);
1452 if (value
->optimized_out
)
1453 error (_("static field `%s' has been "
1454 "optimized out, cannot use"),
1459 gen_primitive_field (exp
, ax
, value
, offset
, i
, type
);
1462 #if 0 /* is this right? */
1463 if (this_name
[0] == '\0')
1464 internal_error (__FILE__
, __LINE__
,
1465 _("find_field: anonymous unions not supported"));
1470 /* Now scan through base classes recursively. */
1471 for (i
= 0; i
< nbases
; i
++)
1473 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1475 rslt
= gen_struct_ref_recursive (exp
, ax
, value
, field
,
1476 offset
+ TYPE_BASECLASS_BITPOS (type
, i
)
1483 /* Not found anywhere, flag so caller can complain. */
1487 /* Generate code to reference the member named FIELD of a structure or
1488 union. The top of the stack, as described by VALUE, should have
1489 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1490 the operator being compiled, and OPERAND_NAME is the kind of thing
1491 it operates on; we use them in error messages. */
1493 gen_struct_ref (struct expression
*exp
, struct agent_expr
*ax
,
1494 struct axs_value
*value
, char *field
,
1495 char *operator_name
, char *operand_name
)
1500 /* Follow pointers until we reach a non-pointer. These aren't the C
1501 semantics, but they're what the normal GDB evaluator does, so we
1502 should at least be consistent. */
1503 while (pointer_type (value
->type
))
1505 require_rvalue (ax
, value
);
1506 gen_deref (ax
, value
);
1508 type
= check_typedef (value
->type
);
1510 /* This must yield a structure or a union. */
1511 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
1512 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
1513 error (_("The left operand of `%s' is not a %s."),
1514 operator_name
, operand_name
);
1516 /* And it must be in memory; we don't deal with structure rvalues,
1517 or structures living in registers. */
1518 if (value
->kind
!= axs_lvalue_memory
)
1519 error (_("Structure does not live in memory."));
1521 /* Search through fields and base classes recursively. */
1522 found
= gen_struct_ref_recursive (exp
, ax
, value
, field
, 0, type
);
1525 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1526 field
, TYPE_TAG_NAME (type
));
1530 gen_namespace_elt (struct expression
*exp
,
1531 struct agent_expr
*ax
, struct axs_value
*value
,
1532 const struct type
*curtype
, char *name
);
1534 gen_maybe_namespace_elt (struct expression
*exp
,
1535 struct agent_expr
*ax
, struct axs_value
*value
,
1536 const struct type
*curtype
, char *name
);
1539 gen_static_field (struct gdbarch
*gdbarch
,
1540 struct agent_expr
*ax
, struct axs_value
*value
,
1541 struct type
*type
, int fieldno
)
1543 if (TYPE_FIELD_LOC_KIND (type
, fieldno
) == FIELD_LOC_KIND_PHYSADDR
)
1545 ax_const_l (ax
, TYPE_FIELD_STATIC_PHYSADDR (type
, fieldno
));
1546 value
->kind
= axs_lvalue_memory
;
1547 value
->type
= TYPE_FIELD_TYPE (type
, fieldno
);
1548 value
->optimized_out
= 0;
1552 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, fieldno
);
1553 struct symbol
*sym
= lookup_symbol (phys_name
, 0, VAR_DOMAIN
, 0);
1557 gen_var_ref (gdbarch
, ax
, value
, sym
);
1559 /* Don't error if the value was optimized out, we may be
1560 scanning all static fields and just want to pass over this
1561 and continue with the rest. */
1565 /* Silently assume this was optimized out; class printing
1566 will let the user know why the data is missing. */
1567 value
->optimized_out
= 1;
1573 gen_struct_elt_for_reference (struct expression
*exp
,
1574 struct agent_expr
*ax
, struct axs_value
*value
,
1575 struct type
*type
, char *fieldname
)
1577 struct type
*t
= type
;
1580 if (TYPE_CODE (t
) != TYPE_CODE_STRUCT
1581 && TYPE_CODE (t
) != TYPE_CODE_UNION
)
1582 internal_error (__FILE__
, __LINE__
,
1583 _("non-aggregate type to gen_struct_elt_for_reference"));
1585 for (i
= TYPE_NFIELDS (t
) - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1587 char *t_field_name
= TYPE_FIELD_NAME (t
, i
);
1589 if (t_field_name
&& strcmp (t_field_name
, fieldname
) == 0)
1591 if (field_is_static (&TYPE_FIELD (t
, i
)))
1593 gen_static_field (exp
->gdbarch
, ax
, value
, t
, i
);
1594 if (value
->optimized_out
)
1595 error (_("static field `%s' has been "
1596 "optimized out, cannot use"),
1600 if (TYPE_FIELD_PACKED (t
, i
))
1601 error (_("pointers to bitfield members not allowed"));
1603 /* FIXME we need a way to do "want_address" equivalent */
1605 error (_("Cannot reference non-static field \"%s\""), fieldname
);
1609 /* FIXME add other scoped-reference cases here */
1611 /* Do a last-ditch lookup. */
1612 return gen_maybe_namespace_elt (exp
, ax
, value
, type
, fieldname
);
1615 /* C++: Return the member NAME of the namespace given by the type
1619 gen_namespace_elt (struct expression
*exp
,
1620 struct agent_expr
*ax
, struct axs_value
*value
,
1621 const struct type
*curtype
, char *name
)
1623 int found
= gen_maybe_namespace_elt (exp
, ax
, value
, curtype
, name
);
1626 error (_("No symbol \"%s\" in namespace \"%s\"."),
1627 name
, TYPE_TAG_NAME (curtype
));
1632 /* A helper function used by value_namespace_elt and
1633 value_struct_elt_for_reference. It looks up NAME inside the
1634 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1635 is a class and NAME refers to a type in CURTYPE itself (as opposed
1636 to, say, some base class of CURTYPE). */
1639 gen_maybe_namespace_elt (struct expression
*exp
,
1640 struct agent_expr
*ax
, struct axs_value
*value
,
1641 const struct type
*curtype
, char *name
)
1643 const char *namespace_name
= TYPE_TAG_NAME (curtype
);
1646 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
1647 block_for_pc (ax
->scope
),
1653 gen_var_ref (exp
->gdbarch
, ax
, value
, sym
);
1655 if (value
->optimized_out
)
1656 error (_("`%s' has been optimized out, cannot use"),
1657 SYMBOL_PRINT_NAME (sym
));
1664 gen_aggregate_elt_ref (struct expression
*exp
,
1665 struct agent_expr
*ax
, struct axs_value
*value
,
1666 struct type
*type
, char *field
,
1667 char *operator_name
, char *operand_name
)
1669 switch (TYPE_CODE (type
))
1671 case TYPE_CODE_STRUCT
:
1672 case TYPE_CODE_UNION
:
1673 return gen_struct_elt_for_reference (exp
, ax
, value
, type
, field
);
1675 case TYPE_CODE_NAMESPACE
:
1676 return gen_namespace_elt (exp
, ax
, value
, type
, field
);
1679 internal_error (__FILE__
, __LINE__
,
1680 _("non-aggregate type in gen_aggregate_elt_ref"));
1686 /* Generate code for GDB's magical `repeat' operator.
1687 LVALUE @ INT creates an array INT elements long, and whose elements
1688 have the same type as LVALUE, located in memory so that LVALUE is
1689 its first element. For example, argv[0]@argc gives you the array
1690 of command-line arguments.
1692 Unfortunately, because we have to know the types before we actually
1693 have a value for the expression, we can't implement this perfectly
1694 without changing the type system, having values that occupy two
1695 stack slots, doing weird things with sizeof, etc. So we require
1696 the right operand to be a constant expression. */
1698 gen_repeat (struct expression
*exp
, union exp_element
**pc
,
1699 struct agent_expr
*ax
, struct axs_value
*value
)
1701 struct axs_value value1
;
1703 /* We don't want to turn this into an rvalue, so no conversions
1705 gen_expr (exp
, pc
, ax
, &value1
);
1706 if (value1
.kind
!= axs_lvalue_memory
)
1707 error (_("Left operand of `@' must be an object in memory."));
1709 /* Evaluate the length; it had better be a constant. */
1711 struct value
*v
= const_expr (pc
);
1715 error (_("Right operand of `@' must be a "
1716 "constant, in agent expressions."));
1717 if (TYPE_CODE (value_type (v
)) != TYPE_CODE_INT
)
1718 error (_("Right operand of `@' must be an integer."));
1719 length
= value_as_long (v
);
1721 error (_("Right operand of `@' must be positive."));
1723 /* The top of the stack is already the address of the object, so
1724 all we need to do is frob the type of the lvalue. */
1726 /* FIXME-type-allocation: need a way to free this type when we are
1729 = lookup_array_range_type (value1
.type
, 0, length
- 1);
1731 value
->kind
= axs_lvalue_memory
;
1732 value
->type
= array
;
1738 /* Emit code for the `sizeof' operator.
1739 *PC should point at the start of the operand expression; we advance it
1740 to the first instruction after the operand. */
1742 gen_sizeof (struct expression
*exp
, union exp_element
**pc
,
1743 struct agent_expr
*ax
, struct axs_value
*value
,
1744 struct type
*size_type
)
1746 /* We don't care about the value of the operand expression; we only
1747 care about its type. However, in the current arrangement, the
1748 only way to find an expression's type is to generate code for it.
1749 So we generate code for the operand, and then throw it away,
1750 replacing it with code that simply pushes its size. */
1751 int start
= ax
->len
;
1753 gen_expr (exp
, pc
, ax
, value
);
1755 /* Throw away the code we just generated. */
1758 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
1759 value
->kind
= axs_rvalue
;
1760 value
->type
= size_type
;
1764 /* Generating bytecode from GDB expressions: general recursive thingy */
1767 /* A gen_expr function written by a Gen-X'er guy.
1768 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1770 gen_expr (struct expression
*exp
, union exp_element
**pc
,
1771 struct agent_expr
*ax
, struct axs_value
*value
)
1773 /* Used to hold the descriptions of operand expressions. */
1774 struct axs_value value1
, value2
, value3
;
1775 enum exp_opcode op
= (*pc
)[0].opcode
, op2
;
1776 int if1
, go1
, if2
, go2
, end
;
1777 struct type
*int_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1779 /* If we're looking at a constant expression, just push its value. */
1781 struct value
*v
= maybe_const_expr (pc
);
1785 ax_const_l (ax
, value_as_long (v
));
1786 value
->kind
= axs_rvalue
;
1787 value
->type
= check_typedef (value_type (v
));
1792 /* Otherwise, go ahead and generate code for it. */
1795 /* Binary arithmetic operators. */
1803 case BINOP_SUBSCRIPT
:
1804 case BINOP_BITWISE_AND
:
1805 case BINOP_BITWISE_IOR
:
1806 case BINOP_BITWISE_XOR
:
1808 case BINOP_NOTEQUAL
:
1814 gen_expr (exp
, pc
, ax
, &value1
);
1815 gen_usual_unary (exp
, ax
, &value1
);
1816 gen_expr_binop_rest (exp
, op
, pc
, ax
, value
, &value1
, &value2
);
1819 case BINOP_LOGICAL_AND
:
1821 /* Generate the obvious sequence of tests and jumps. */
1822 gen_expr (exp
, pc
, ax
, &value1
);
1823 gen_usual_unary (exp
, ax
, &value1
);
1824 if1
= ax_goto (ax
, aop_if_goto
);
1825 go1
= ax_goto (ax
, aop_goto
);
1826 ax_label (ax
, if1
, ax
->len
);
1827 gen_expr (exp
, pc
, ax
, &value2
);
1828 gen_usual_unary (exp
, ax
, &value2
);
1829 if2
= ax_goto (ax
, aop_if_goto
);
1830 go2
= ax_goto (ax
, aop_goto
);
1831 ax_label (ax
, if2
, ax
->len
);
1833 end
= ax_goto (ax
, aop_goto
);
1834 ax_label (ax
, go1
, ax
->len
);
1835 ax_label (ax
, go2
, ax
->len
);
1837 ax_label (ax
, end
, ax
->len
);
1838 value
->kind
= axs_rvalue
;
1839 value
->type
= int_type
;
1842 case BINOP_LOGICAL_OR
:
1844 /* Generate the obvious sequence of tests and jumps. */
1845 gen_expr (exp
, pc
, ax
, &value1
);
1846 gen_usual_unary (exp
, ax
, &value1
);
1847 if1
= ax_goto (ax
, aop_if_goto
);
1848 gen_expr (exp
, pc
, ax
, &value2
);
1849 gen_usual_unary (exp
, ax
, &value2
);
1850 if2
= ax_goto (ax
, aop_if_goto
);
1852 end
= ax_goto (ax
, aop_goto
);
1853 ax_label (ax
, if1
, ax
->len
);
1854 ax_label (ax
, if2
, ax
->len
);
1856 ax_label (ax
, end
, ax
->len
);
1857 value
->kind
= axs_rvalue
;
1858 value
->type
= int_type
;
1863 gen_expr (exp
, pc
, ax
, &value1
);
1864 gen_usual_unary (exp
, ax
, &value1
);
1865 /* For (A ? B : C), it's easiest to generate subexpression
1866 bytecodes in order, but if_goto jumps on true, so we invert
1867 the sense of A. Then we can do B by dropping through, and
1869 gen_logical_not (ax
, &value1
, int_type
);
1870 if1
= ax_goto (ax
, aop_if_goto
);
1871 gen_expr (exp
, pc
, ax
, &value2
);
1872 gen_usual_unary (exp
, ax
, &value2
);
1873 end
= ax_goto (ax
, aop_goto
);
1874 ax_label (ax
, if1
, ax
->len
);
1875 gen_expr (exp
, pc
, ax
, &value3
);
1876 gen_usual_unary (exp
, ax
, &value3
);
1877 ax_label (ax
, end
, ax
->len
);
1878 /* This is arbitary - what if B and C are incompatible types? */
1879 value
->type
= value2
.type
;
1880 value
->kind
= value2
.kind
;
1885 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1887 char *name
= internalvar_name ((*pc
)[1].internalvar
);
1888 struct trace_state_variable
*tsv
;
1891 gen_expr (exp
, pc
, ax
, value
);
1892 tsv
= find_trace_state_variable (name
);
1895 ax_tsv (ax
, aop_setv
, tsv
->number
);
1897 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1900 error (_("$%s is not a trace state variable, "
1901 "may not assign to it"), name
);
1904 error (_("May only assign to trace state variables"));
1907 case BINOP_ASSIGN_MODIFY
:
1909 op2
= (*pc
)[0].opcode
;
1912 if ((*pc
)[0].opcode
== OP_INTERNALVAR
)
1914 char *name
= internalvar_name ((*pc
)[1].internalvar
);
1915 struct trace_state_variable
*tsv
;
1918 tsv
= find_trace_state_variable (name
);
1921 /* The tsv will be the left half of the binary operation. */
1922 ax_tsv (ax
, aop_getv
, tsv
->number
);
1924 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1925 /* Trace state variables are always 64-bit integers. */
1926 value1
.kind
= axs_rvalue
;
1927 value1
.type
= builtin_type (exp
->gdbarch
)->builtin_long_long
;
1928 /* Now do right half of expression. */
1929 gen_expr_binop_rest (exp
, op2
, pc
, ax
, value
, &value1
, &value2
);
1930 /* We have a result of the binary op, set the tsv. */
1931 ax_tsv (ax
, aop_setv
, tsv
->number
);
1933 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1936 error (_("$%s is not a trace state variable, "
1937 "may not assign to it"), name
);
1940 error (_("May only assign to trace state variables"));
1943 /* Note that we need to be a little subtle about generating code
1944 for comma. In C, we can do some optimizations here because
1945 we know the left operand is only being evaluated for effect.
1946 However, if the tracing kludge is in effect, then we always
1947 need to evaluate the left hand side fully, so that all the
1948 variables it mentions get traced. */
1951 gen_expr (exp
, pc
, ax
, &value1
);
1952 /* Don't just dispose of the left operand. We might be tracing,
1953 in which case we want to emit code to trace it if it's an
1955 gen_traced_pop (exp
->gdbarch
, ax
, &value1
);
1956 gen_expr (exp
, pc
, ax
, value
);
1957 /* It's the consumer's responsibility to trace the right operand. */
1960 case OP_LONG
: /* some integer constant */
1962 struct type
*type
= (*pc
)[1].type
;
1963 LONGEST k
= (*pc
)[2].longconst
;
1966 gen_int_literal (ax
, value
, k
, type
);
1971 gen_var_ref (exp
->gdbarch
, ax
, value
, (*pc
)[2].symbol
);
1973 if (value
->optimized_out
)
1974 error (_("`%s' has been optimized out, cannot use"),
1975 SYMBOL_PRINT_NAME ((*pc
)[2].symbol
));
1982 const char *name
= &(*pc
)[2].string
;
1985 (*pc
) += 4 + BYTES_TO_EXP_ELEM ((*pc
)[1].longconst
+ 1);
1986 reg
= user_reg_map_name_to_regnum (exp
->gdbarch
, name
, strlen (name
));
1988 internal_error (__FILE__
, __LINE__
,
1989 _("Register $%s not available"), name
);
1990 /* No support for tracing user registers yet. */
1991 if (reg
>= gdbarch_num_regs (exp
->gdbarch
)
1992 + gdbarch_num_pseudo_regs (exp
->gdbarch
))
1993 error (_("'%s' is a user-register; "
1994 "GDB cannot yet trace user-register contents."),
1996 value
->kind
= axs_lvalue_register
;
1998 value
->type
= register_type (exp
->gdbarch
, reg
);
2002 case OP_INTERNALVAR
:
2004 const char *name
= internalvar_name ((*pc
)[1].internalvar
);
2005 struct trace_state_variable
*tsv
;
2008 tsv
= find_trace_state_variable (name
);
2011 ax_tsv (ax
, aop_getv
, tsv
->number
);
2013 ax_tsv (ax
, aop_tracev
, tsv
->number
);
2014 /* Trace state variables are always 64-bit integers. */
2015 value
->kind
= axs_rvalue
;
2016 value
->type
= builtin_type (exp
->gdbarch
)->builtin_long_long
;
2019 error (_("$%s is not a trace state variable; GDB agent "
2020 "expressions cannot use convenience variables."), name
);
2024 /* Weirdo operator: see comments for gen_repeat for details. */
2026 /* Note that gen_repeat handles its own argument evaluation. */
2028 gen_repeat (exp
, pc
, ax
, value
);
2033 struct type
*type
= (*pc
)[1].type
;
2036 gen_expr (exp
, pc
, ax
, value
);
2037 gen_cast (ax
, value
, type
);
2043 struct type
*type
= check_typedef ((*pc
)[1].type
);
2046 gen_expr (exp
, pc
, ax
, value
);
2048 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2049 already have the right value on the stack. For
2050 axs_lvalue_register, we must convert. */
2051 if (value
->kind
== axs_lvalue_register
)
2052 require_rvalue (ax
, value
);
2055 value
->kind
= axs_lvalue_memory
;
2061 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2062 gen_expr (exp
, pc
, ax
, value
);
2063 gen_usual_unary (exp
, ax
, value
);
2068 /* -FOO is equivalent to 0 - FOO. */
2069 gen_int_literal (ax
, &value1
, 0,
2070 builtin_type (exp
->gdbarch
)->builtin_int
);
2071 gen_usual_unary (exp
, ax
, &value1
); /* shouldn't do much */
2072 gen_expr (exp
, pc
, ax
, &value2
);
2073 gen_usual_unary (exp
, ax
, &value2
);
2074 gen_usual_arithmetic (exp
, ax
, &value1
, &value2
);
2075 gen_binop (ax
, value
, &value1
, &value2
, aop_sub
, aop_sub
, 1, "negation");
2078 case UNOP_LOGICAL_NOT
:
2080 gen_expr (exp
, pc
, ax
, value
);
2081 gen_usual_unary (exp
, ax
, value
);
2082 gen_logical_not (ax
, value
, int_type
);
2085 case UNOP_COMPLEMENT
:
2087 gen_expr (exp
, pc
, ax
, value
);
2088 gen_usual_unary (exp
, ax
, value
);
2089 gen_integral_promotions (exp
, ax
, value
);
2090 gen_complement (ax
, value
);
2095 gen_expr (exp
, pc
, ax
, value
);
2096 gen_usual_unary (exp
, ax
, value
);
2097 if (!pointer_type (value
->type
))
2098 error (_("Argument of unary `*' is not a pointer."));
2099 gen_deref (ax
, value
);
2104 gen_expr (exp
, pc
, ax
, value
);
2105 gen_address_of (ax
, value
);
2110 /* Notice that gen_sizeof handles its own operand, unlike most
2111 of the other unary operator functions. This is because we
2112 have to throw away the code we generate. */
2113 gen_sizeof (exp
, pc
, ax
, value
,
2114 builtin_type (exp
->gdbarch
)->builtin_int
);
2117 case STRUCTOP_STRUCT
:
2120 int length
= (*pc
)[1].longconst
;
2121 char *name
= &(*pc
)[2].string
;
2123 (*pc
) += 4 + BYTES_TO_EXP_ELEM (length
+ 1);
2124 gen_expr (exp
, pc
, ax
, value
);
2125 if (op
== STRUCTOP_STRUCT
)
2126 gen_struct_ref (exp
, ax
, value
, name
, ".", "structure or union");
2127 else if (op
== STRUCTOP_PTR
)
2128 gen_struct_ref (exp
, ax
, value
, name
, "->",
2129 "pointer to a structure or union");
2131 /* If this `if' chain doesn't handle it, then the case list
2132 shouldn't mention it, and we shouldn't be here. */
2133 internal_error (__FILE__
, __LINE__
,
2134 _("gen_expr: unhandled struct case"));
2141 struct symbol
*func
, *sym
;
2144 func
= block_linkage_function (block_for_pc (ax
->scope
));
2145 this_name
= language_def (SYMBOL_LANGUAGE (func
))->la_name_of_this
;
2146 b
= SYMBOL_BLOCK_VALUE (func
);
2148 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2149 symbol instead of the LOC_ARG one (if both exist). */
2150 sym
= lookup_block_symbol (b
, this_name
, VAR_DOMAIN
);
2152 error (_("no `%s' found"), this_name
);
2154 gen_var_ref (exp
->gdbarch
, ax
, value
, sym
);
2156 if (value
->optimized_out
)
2157 error (_("`%s' has been optimized out, cannot use"),
2158 SYMBOL_PRINT_NAME (sym
));
2166 struct type
*type
= (*pc
)[1].type
;
2167 int length
= longest_to_int ((*pc
)[2].longconst
);
2168 char *name
= &(*pc
)[3].string
;
2171 found
= gen_aggregate_elt_ref (exp
, ax
, value
, type
, name
,
2174 error (_("There is no field named %s"), name
);
2175 (*pc
) += 5 + BYTES_TO_EXP_ELEM (length
+ 1);
2180 error (_("Attempt to use a type name as an expression."));
2183 error (_("Unsupported operator %s (%d) in expression."),
2184 op_string (op
), op
);
2188 /* This handles the middle-to-right-side of code generation for binary
2189 expressions, which is shared between regular binary operations and
2190 assign-modify (+= and friends) expressions. */
2193 gen_expr_binop_rest (struct expression
*exp
,
2194 enum exp_opcode op
, union exp_element
**pc
,
2195 struct agent_expr
*ax
, struct axs_value
*value
,
2196 struct axs_value
*value1
, struct axs_value
*value2
)
2198 struct type
*int_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2200 gen_expr (exp
, pc
, ax
, value2
);
2201 gen_usual_unary (exp
, ax
, value2
);
2202 gen_usual_arithmetic (exp
, ax
, value1
, value2
);
2206 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
2207 && pointer_type (value2
->type
))
2209 /* Swap the values and proceed normally. */
2210 ax_simple (ax
, aop_swap
);
2211 gen_ptradd (ax
, value
, value2
, value1
);
2213 else if (pointer_type (value1
->type
)
2214 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
2215 gen_ptradd (ax
, value
, value1
, value2
);
2217 gen_binop (ax
, value
, value1
, value2
,
2218 aop_add
, aop_add
, 1, "addition");
2221 if (pointer_type (value1
->type
)
2222 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
2223 gen_ptrsub (ax
,value
, value1
, value2
);
2224 else if (pointer_type (value1
->type
)
2225 && pointer_type (value2
->type
))
2226 /* FIXME --- result type should be ptrdiff_t */
2227 gen_ptrdiff (ax
, value
, value1
, value2
,
2228 builtin_type (exp
->gdbarch
)->builtin_long
);
2230 gen_binop (ax
, value
, value1
, value2
,
2231 aop_sub
, aop_sub
, 1, "subtraction");
2234 gen_binop (ax
, value
, value1
, value2
,
2235 aop_mul
, aop_mul
, 1, "multiplication");
2238 gen_binop (ax
, value
, value1
, value2
,
2239 aop_div_signed
, aop_div_unsigned
, 1, "division");
2242 gen_binop (ax
, value
, value1
, value2
,
2243 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
2246 gen_binop (ax
, value
, value1
, value2
,
2247 aop_lsh
, aop_lsh
, 1, "left shift");
2250 gen_binop (ax
, value
, value1
, value2
,
2251 aop_rsh_signed
, aop_rsh_unsigned
, 1, "right shift");
2253 case BINOP_SUBSCRIPT
:
2257 if (binop_types_user_defined_p (op
, value1
->type
, value2
->type
))
2259 error (_("cannot subscript requested type: "
2260 "cannot call user defined functions"));
2264 /* If the user attempts to subscript something that is not
2265 an array or pointer type (like a plain int variable for
2266 example), then report this as an error. */
2267 type
= check_typedef (value1
->type
);
2268 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
2269 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
2271 if (TYPE_NAME (type
))
2272 error (_("cannot subscript something of type `%s'"),
2275 error (_("cannot subscript requested type"));
2279 if (!is_integral_type (value2
->type
))
2280 error (_("Argument to arithmetic operation "
2281 "not a number or boolean."));
2283 gen_ptradd (ax
, value
, value1
, value2
);
2284 gen_deref (ax
, value
);
2287 case BINOP_BITWISE_AND
:
2288 gen_binop (ax
, value
, value1
, value2
,
2289 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
2292 case BINOP_BITWISE_IOR
:
2293 gen_binop (ax
, value
, value1
, value2
,
2294 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
2297 case BINOP_BITWISE_XOR
:
2298 gen_binop (ax
, value
, value1
, value2
,
2299 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
2303 gen_equal (ax
, value
, value1
, value2
, int_type
);
2306 case BINOP_NOTEQUAL
:
2307 gen_equal (ax
, value
, value1
, value2
, int_type
);
2308 gen_logical_not (ax
, value
, int_type
);
2312 gen_less (ax
, value
, value1
, value2
, int_type
);
2316 ax_simple (ax
, aop_swap
);
2317 gen_less (ax
, value
, value1
, value2
, int_type
);
2321 ax_simple (ax
, aop_swap
);
2322 gen_less (ax
, value
, value1
, value2
, int_type
);
2323 gen_logical_not (ax
, value
, int_type
);
2327 gen_less (ax
, value
, value1
, value2
, int_type
);
2328 gen_logical_not (ax
, value
, int_type
);
2332 /* We should only list operators in the outer case statement
2333 that we actually handle in the inner case statement. */
2334 internal_error (__FILE__
, __LINE__
,
2335 _("gen_expr: op case sets don't match"));
2340 /* Given a single variable and a scope, generate bytecodes to trace
2341 its value. This is for use in situations where we have only a
2342 variable's name, and no parsed expression; for instance, when the
2343 name comes from a list of local variables of a function. */
2346 gen_trace_for_var (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2349 struct cleanup
*old_chain
= 0;
2350 struct agent_expr
*ax
= new_agent_expr (gdbarch
, scope
);
2351 struct axs_value value
;
2353 old_chain
= make_cleanup_free_agent_expr (ax
);
2356 gen_var_ref (gdbarch
, ax
, &value
, var
);
2358 /* If there is no actual variable to trace, flag it by returning
2359 an empty agent expression. */
2360 if (value
.optimized_out
)
2362 do_cleanups (old_chain
);
2366 /* Make sure we record the final object, and get rid of it. */
2367 gen_traced_pop (gdbarch
, ax
, &value
);
2369 /* Oh, and terminate. */
2370 ax_simple (ax
, aop_end
);
2372 /* We have successfully built the agent expr, so cancel the cleanup
2373 request. If we add more cleanups that we always want done, this
2374 will have to get more complicated. */
2375 discard_cleanups (old_chain
);
2379 /* Generating bytecode from GDB expressions: driver */
2381 /* Given a GDB expression EXPR, return bytecode to trace its value.
2382 The result will use the `trace' and `trace_quick' bytecodes to
2383 record the value of all memory touched by the expression. The
2384 caller can then use the ax_reqs function to discover which
2385 registers it relies upon. */
2387 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2389 struct cleanup
*old_chain
= 0;
2390 struct agent_expr
*ax
= new_agent_expr (expr
->gdbarch
, scope
);
2391 union exp_element
*pc
;
2392 struct axs_value value
;
2394 old_chain
= make_cleanup_free_agent_expr (ax
);
2398 value
.optimized_out
= 0;
2399 gen_expr (expr
, &pc
, ax
, &value
);
2401 /* Make sure we record the final object, and get rid of it. */
2402 gen_traced_pop (expr
->gdbarch
, ax
, &value
);
2404 /* Oh, and terminate. */
2405 ax_simple (ax
, aop_end
);
2407 /* We have successfully built the agent expr, so cancel the cleanup
2408 request. If we add more cleanups that we always want done, this
2409 will have to get more complicated. */
2410 discard_cleanups (old_chain
);
2414 /* Given a GDB expression EXPR, return a bytecode sequence that will
2415 evaluate and return a result. The bytecodes will do a direct
2416 evaluation, using the current data on the target, rather than
2417 recording blocks of memory and registers for later use, as
2418 gen_trace_for_expr does. The generated bytecode sequence leaves
2419 the result of expression evaluation on the top of the stack. */
2422 gen_eval_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2424 struct cleanup
*old_chain
= 0;
2425 struct agent_expr
*ax
= new_agent_expr (expr
->gdbarch
, scope
);
2426 union exp_element
*pc
;
2427 struct axs_value value
;
2429 old_chain
= make_cleanup_free_agent_expr (ax
);
2433 value
.optimized_out
= 0;
2434 gen_expr (expr
, &pc
, ax
, &value
);
2436 require_rvalue (ax
, &value
);
2438 /* Oh, and terminate. */
2439 ax_simple (ax
, aop_end
);
2441 /* We have successfully built the agent expr, so cancel the cleanup
2442 request. If we add more cleanups that we always want done, this
2443 will have to get more complicated. */
2444 discard_cleanups (old_chain
);
2449 agent_command (char *exp
, int from_tty
)
2451 struct cleanup
*old_chain
= 0;
2452 struct expression
*expr
;
2453 struct agent_expr
*agent
;
2454 struct frame_info
*fi
= get_current_frame (); /* need current scope */
2456 /* We don't deal with overlay debugging at the moment. We need to
2457 think more carefully about this. If you copy this code into
2458 another command, change the error message; the user shouldn't
2459 have to know anything about agent expressions. */
2460 if (overlay_debugging
)
2461 error (_("GDB can't do agent expression translation with overlays."));
2464 error_no_arg (_("expression to translate"));
2466 expr
= parse_expression (exp
);
2467 old_chain
= make_cleanup (free_current_contents
, &expr
);
2468 agent
= gen_trace_for_expr (get_frame_pc (fi
), expr
);
2469 make_cleanup_free_agent_expr (agent
);
2471 ax_print (gdb_stdout
, agent
);
2473 /* It would be nice to call ax_reqs here to gather some general info
2474 about the expression, and then print out the result. */
2476 do_cleanups (old_chain
);
2480 /* Parse the given expression, compile it into an agent expression
2481 that does direct evaluation, and display the resulting
2485 agent_eval_command (char *exp
, int from_tty
)
2487 struct cleanup
*old_chain
= 0;
2488 struct expression
*expr
;
2489 struct agent_expr
*agent
;
2490 struct frame_info
*fi
= get_current_frame (); /* need current scope */
2492 /* We don't deal with overlay debugging at the moment. We need to
2493 think more carefully about this. If you copy this code into
2494 another command, change the error message; the user shouldn't
2495 have to know anything about agent expressions. */
2496 if (overlay_debugging
)
2497 error (_("GDB can't do agent expression translation with overlays."));
2500 error_no_arg (_("expression to translate"));
2502 expr
= parse_expression (exp
);
2503 old_chain
= make_cleanup (free_current_contents
, &expr
);
2504 agent
= gen_eval_for_expr (get_frame_pc (fi
), expr
);
2505 make_cleanup_free_agent_expr (agent
);
2507 ax_print (gdb_stdout
, agent
);
2509 /* It would be nice to call ax_reqs here to gather some general info
2510 about the expression, and then print out the result. */
2512 do_cleanups (old_chain
);
2517 /* Initialization code. */
2519 void _initialize_ax_gdb (void);
2521 _initialize_ax_gdb (void)
2523 add_cmd ("agent", class_maintenance
, agent_command
,
2524 _("Translate an expression into "
2525 "remote agent bytecode for tracing."),
2528 add_cmd ("agent-eval", class_maintenance
, agent_eval_command
,
2529 _("Translate an expression into remote "
2530 "agent bytecode for evaluation."),
This page took 0.080532 seconds and 4 git commands to generate.