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 /* No support for tracing user registers yet. */
1982 if (reg >= gdbarch_num_regs (exp->gdbarch)
1983 + gdbarch_num_pseudo_regs (exp->gdbarch))
1984 error (_("'%s' is a pseudo-register; "
1985 "GDB cannot yet trace pseudoregister contents."),
1987 value->kind = axs_lvalue_register;
1989 value->type = register_type (exp->gdbarch, reg);
1993 case OP_INTERNALVAR:
1995 const char *name = internalvar_name ((*pc)[1].internalvar);
1996 struct trace_state_variable *tsv;
1999 tsv = find_trace_state_variable (name);
2002 ax_tsv (ax, aop_getv, tsv->number);
2004 ax_tsv (ax, aop_tracev, tsv->number);
2005 /* Trace state variables are always 64-bit integers. */
2006 value->kind = axs_rvalue;
2007 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2010 error (_("$%s is not a trace state variable; GDB agent expressions cannot use convenience variables."), name);
2014 /* Weirdo operator: see comments for gen_repeat for details. */
2016 /* Note that gen_repeat handles its own argument evaluation. */
2018 gen_repeat (exp, pc, ax, value);
2023 struct type *type = (*pc)[1].type;
2026 gen_expr (exp, pc, ax, value);
2027 gen_cast (ax, value, type);
2033 struct type *type = check_typedef ((*pc)[1].type);
2036 gen_expr (exp, pc, ax, value);
2037 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
2038 it's just a hack for dealing with minsyms; you take some
2039 integer constant, pretend it's the address of an lvalue of
2040 the given type, and dereference it. */
2041 if (value->kind != axs_rvalue)
2042 /* This would be weird. */
2043 internal_error (__FILE__, __LINE__,
2044 _("gen_expr: OP_MEMVAL operand isn't an rvalue???"));
2046 value->kind = axs_lvalue_memory;
2052 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2053 gen_expr (exp, pc, ax, value);
2054 gen_usual_unary (exp, ax, value);
2059 /* -FOO is equivalent to 0 - FOO. */
2060 gen_int_literal (ax, &value1, 0,
2061 builtin_type (exp->gdbarch)->builtin_int);
2062 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2063 gen_expr (exp, pc, ax, &value2);
2064 gen_usual_unary (exp, ax, &value2);
2065 gen_usual_arithmetic (exp, ax, &value1, &value2);
2066 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2069 case UNOP_LOGICAL_NOT:
2071 gen_expr (exp, pc, ax, value);
2072 gen_usual_unary (exp, ax, value);
2073 gen_logical_not (ax, value, int_type);
2076 case UNOP_COMPLEMENT:
2078 gen_expr (exp, pc, ax, value);
2079 gen_usual_unary (exp, ax, value);
2080 gen_integral_promotions (exp, ax, value);
2081 gen_complement (ax, value);
2086 gen_expr (exp, pc, ax, value);
2087 gen_usual_unary (exp, ax, value);
2088 if (!pointer_type (value->type))
2089 error (_("Argument of unary `*' is not a pointer."));
2090 gen_deref (ax, value);
2095 gen_expr (exp, pc, ax, value);
2096 gen_address_of (ax, value);
2101 /* Notice that gen_sizeof handles its own operand, unlike most
2102 of the other unary operator functions. This is because we
2103 have to throw away the code we generate. */
2104 gen_sizeof (exp, pc, ax, value,
2105 builtin_type (exp->gdbarch)->builtin_int);
2108 case STRUCTOP_STRUCT:
2111 int length = (*pc)[1].longconst;
2112 char *name = &(*pc)[2].string;
2114 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2115 gen_expr (exp, pc, ax, value);
2116 if (op == STRUCTOP_STRUCT)
2117 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2118 else if (op == STRUCTOP_PTR)
2119 gen_struct_ref (exp, ax, value, name, "->",
2120 "pointer to a structure or union");
2122 /* If this `if' chain doesn't handle it, then the case list
2123 shouldn't mention it, and we shouldn't be here. */
2124 internal_error (__FILE__, __LINE__,
2125 _("gen_expr: unhandled struct case"));
2132 struct symbol *func, *sym;
2135 func = block_linkage_function (block_for_pc (ax->scope));
2136 this_name = language_def (SYMBOL_LANGUAGE (func))->la_name_of_this;
2137 b = SYMBOL_BLOCK_VALUE (func);
2139 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2140 symbol instead of the LOC_ARG one (if both exist). */
2141 sym = lookup_block_symbol (b, this_name, VAR_DOMAIN);
2143 error (_("no `%s' found"), this_name);
2145 gen_var_ref (exp->gdbarch, ax, value, sym);
2147 if (value->optimized_out)
2148 error (_("`%s' has been optimized out, cannot use"),
2149 SYMBOL_PRINT_NAME (sym));
2157 struct type *type = (*pc)[1].type;
2158 int length = longest_to_int ((*pc)[2].longconst);
2159 char *name = &(*pc)[3].string;
2162 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2165 error (_("There is no field named %s"), name);
2166 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2171 error (_("Attempt to use a type name as an expression."));
2174 error (_("Unsupported operator %s (%d) in expression."),
2175 op_string (op), op);
2179 /* This handles the middle-to-right-side of code generation for binary
2180 expressions, which is shared between regular binary operations and
2181 assign-modify (+= and friends) expressions. */
2184 gen_expr_binop_rest (struct expression *exp,
2185 enum exp_opcode op, union exp_element **pc,
2186 struct agent_expr *ax, struct axs_value *value,
2187 struct axs_value *value1, struct axs_value *value2)
2189 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2191 gen_expr (exp, pc, ax, value2);
2192 gen_usual_unary (exp, ax, value2);
2193 gen_usual_arithmetic (exp, ax, value1, value2);
2197 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2198 && pointer_type (value2->type))
2200 /* Swap the values and proceed normally. */
2201 ax_simple (ax, aop_swap);
2202 gen_ptradd (ax, value, value2, value1);
2204 else if (pointer_type (value1->type)
2205 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2206 gen_ptradd (ax, value, value1, value2);
2208 gen_binop (ax, value, value1, value2,
2209 aop_add, aop_add, 1, "addition");
2212 if (pointer_type (value1->type)
2213 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2214 gen_ptrsub (ax,value, value1, value2);
2215 else if (pointer_type (value1->type)
2216 && pointer_type (value2->type))
2217 /* FIXME --- result type should be ptrdiff_t */
2218 gen_ptrdiff (ax, value, value1, value2,
2219 builtin_type (exp->gdbarch)->builtin_long);
2221 gen_binop (ax, value, value1, value2,
2222 aop_sub, aop_sub, 1, "subtraction");
2225 gen_binop (ax, value, value1, value2,
2226 aop_mul, aop_mul, 1, "multiplication");
2229 gen_binop (ax, value, value1, value2,
2230 aop_div_signed, aop_div_unsigned, 1, "division");
2233 gen_binop (ax, value, value1, value2,
2234 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2237 gen_binop (ax, value, value1, value2,
2238 aop_lsh, aop_lsh, 1, "left shift");
2241 gen_binop (ax, value, value1, value2,
2242 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2244 case BINOP_SUBSCRIPT:
2248 if (binop_types_user_defined_p (op, value1->type, value2->type))
2251 cannot subscript requested type: cannot call user defined functions"));
2255 /* If the user attempts to subscript something that is not
2256 an array or pointer type (like a plain int variable for
2257 example), then report this as an error. */
2258 type = check_typedef (value1->type);
2259 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2260 && TYPE_CODE (type) != TYPE_CODE_PTR)
2262 if (TYPE_NAME (type))
2263 error (_("cannot subscript something of type `%s'"),
2266 error (_("cannot subscript requested type"));
2270 if (!is_integral_type (value2->type))
2271 error (_("Argument to arithmetic operation not a number or boolean."));
2273 gen_ptradd (ax, value, value1, value2);
2274 gen_deref (ax, value);
2277 case BINOP_BITWISE_AND:
2278 gen_binop (ax, value, value1, value2,
2279 aop_bit_and, aop_bit_and, 0, "bitwise and");
2282 case BINOP_BITWISE_IOR:
2283 gen_binop (ax, value, value1, value2,
2284 aop_bit_or, aop_bit_or, 0, "bitwise or");
2287 case BINOP_BITWISE_XOR:
2288 gen_binop (ax, value, value1, value2,
2289 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2293 gen_equal (ax, value, value1, value2, int_type);
2296 case BINOP_NOTEQUAL:
2297 gen_equal (ax, value, value1, value2, int_type);
2298 gen_logical_not (ax, value, int_type);
2302 gen_less (ax, value, value1, value2, int_type);
2306 ax_simple (ax, aop_swap);
2307 gen_less (ax, value, value1, value2, int_type);
2311 ax_simple (ax, aop_swap);
2312 gen_less (ax, value, value1, value2, int_type);
2313 gen_logical_not (ax, value, int_type);
2317 gen_less (ax, value, value1, value2, int_type);
2318 gen_logical_not (ax, value, int_type);
2322 /* We should only list operators in the outer case statement
2323 that we actually handle in the inner case statement. */
2324 internal_error (__FILE__, __LINE__,
2325 _("gen_expr: op case sets don't match"));
2330 /* Given a single variable and a scope, generate bytecodes to trace
2331 its value. This is for use in situations where we have only a
2332 variable's name, and no parsed expression; for instance, when the
2333 name comes from a list of local variables of a function. */
2336 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2339 struct cleanup *old_chain = 0;
2340 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2341 struct axs_value value;
2343 old_chain = make_cleanup_free_agent_expr (ax);
2346 gen_var_ref (gdbarch, ax, &value, var);
2348 /* If there is no actual variable to trace, flag it by returning
2349 an empty agent expression. */
2350 if (value.optimized_out)
2352 do_cleanups (old_chain);
2356 /* Make sure we record the final object, and get rid of it. */
2357 gen_traced_pop (gdbarch, ax, &value);
2359 /* Oh, and terminate. */
2360 ax_simple (ax, aop_end);
2362 /* We have successfully built the agent expr, so cancel the cleanup
2363 request. If we add more cleanups that we always want done, this
2364 will have to get more complicated. */
2365 discard_cleanups (old_chain);
2369 /* Generating bytecode from GDB expressions: driver */
2371 /* Given a GDB expression EXPR, return bytecode to trace its value.
2372 The result will use the `trace' and `trace_quick' bytecodes to
2373 record the value of all memory touched by the expression. The
2374 caller can then use the ax_reqs function to discover which
2375 registers it relies upon. */
2377 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
2379 struct cleanup *old_chain = 0;
2380 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2381 union exp_element *pc;
2382 struct axs_value value;
2384 old_chain = make_cleanup_free_agent_expr (ax);
2388 value.optimized_out = 0;
2389 gen_expr (expr, &pc, ax, &value);
2391 /* Make sure we record the final object, and get rid of it. */
2392 gen_traced_pop (expr->gdbarch, ax, &value);
2394 /* Oh, and terminate. */
2395 ax_simple (ax, aop_end);
2397 /* We have successfully built the agent expr, so cancel the cleanup
2398 request. If we add more cleanups that we always want done, this
2399 will have to get more complicated. */
2400 discard_cleanups (old_chain);
2404 /* Given a GDB expression EXPR, return a bytecode sequence that will
2405 evaluate and return a result. The bytecodes will do a direct
2406 evaluation, using the current data on the target, rather than
2407 recording blocks of memory and registers for later use, as
2408 gen_trace_for_expr does. The generated bytecode sequence leaves
2409 the result of expression evaluation on the top of the stack. */
2412 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2414 struct cleanup *old_chain = 0;
2415 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2416 union exp_element *pc;
2417 struct axs_value value;
2419 old_chain = make_cleanup_free_agent_expr (ax);
2423 value.optimized_out = 0;
2424 gen_expr (expr, &pc, ax, &value);
2426 require_rvalue (ax, &value);
2428 /* Oh, and terminate. */
2429 ax_simple (ax, aop_end);
2431 /* We have successfully built the agent expr, so cancel the cleanup
2432 request. If we add more cleanups that we always want done, this
2433 will have to get more complicated. */
2434 discard_cleanups (old_chain);
2439 agent_command (char *exp, int from_tty)
2441 struct cleanup *old_chain = 0;
2442 struct expression *expr;
2443 struct agent_expr *agent;
2444 struct frame_info *fi = get_current_frame (); /* need current scope */
2446 /* We don't deal with overlay debugging at the moment. We need to
2447 think more carefully about this. If you copy this code into
2448 another command, change the error message; the user shouldn't
2449 have to know anything about agent expressions. */
2450 if (overlay_debugging)
2451 error (_("GDB can't do agent expression translation with overlays."));
2454 error_no_arg (_("expression to translate"));
2456 expr = parse_expression (exp);
2457 old_chain = make_cleanup (free_current_contents, &expr);
2458 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
2459 make_cleanup_free_agent_expr (agent);
2461 ax_print (gdb_stdout, agent);
2463 /* It would be nice to call ax_reqs here to gather some general info
2464 about the expression, and then print out the result. */
2466 do_cleanups (old_chain);
2470 /* Parse the given expression, compile it into an agent expression
2471 that does direct evaluation, and display the resulting
2475 agent_eval_command (char *exp, int from_tty)
2477 struct cleanup *old_chain = 0;
2478 struct expression *expr;
2479 struct agent_expr *agent;
2480 struct frame_info *fi = get_current_frame (); /* need current scope */
2482 /* We don't deal with overlay debugging at the moment. We need to
2483 think more carefully about this. If you copy this code into
2484 another command, change the error message; the user shouldn't
2485 have to know anything about agent expressions. */
2486 if (overlay_debugging)
2487 error (_("GDB can't do agent expression translation with overlays."));
2490 error_no_arg (_("expression to translate"));
2492 expr = parse_expression (exp);
2493 old_chain = make_cleanup (free_current_contents, &expr);
2494 agent = gen_eval_for_expr (get_frame_pc (fi), expr);
2495 make_cleanup_free_agent_expr (agent);
2497 ax_print (gdb_stdout, agent);
2499 /* It would be nice to call ax_reqs here to gather some general info
2500 about the expression, and then print out the result. */
2502 do_cleanups (old_chain);
2507 /* Initialization code. */
2509 void _initialize_ax_gdb (void);
2511 _initialize_ax_gdb (void)
2513 add_cmd ("agent", class_maintenance, agent_command,
2514 _("Translate an expression into remote agent bytecode for tracing."),
2517 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2518 _("Translate an expression into remote agent bytecode for evaluation."),