1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998-2001, 2003, 2007-2012 Free Software Foundation,
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"
43 #include "arch-utils.h"
48 /* To make sense of this file, you should read doc/agentexpr.texi.
49 Then look at the types and enums in ax-gdb.h. For the code itself,
50 look at gen_expr, towards the bottom; that's the main function that
51 looks at the GDB expressions and calls everything else to generate
54 I'm beginning to wonder whether it wouldn't be nicer to internally
55 generate trees, with types, and then spit out the bytecode in
56 linear form afterwards; we could generate fewer `swap', `ext', and
57 `zero_ext' bytecodes that way; it would make good constant folding
58 easier, too. But at the moment, I think we should be willing to
59 pay for the simplicity of this code with less-than-optimal bytecode
62 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
66 /* Prototypes for local functions. */
68 /* There's a standard order to the arguments of these functions:
69 union exp_element ** --- pointer into expression
70 struct agent_expr * --- agent expression buffer to generate code into
71 struct axs_value * --- describes value left on top of stack */
73 static struct value *const_var_ref (struct symbol *var);
74 static struct value *const_expr (union exp_element **pc);
75 static struct value *maybe_const_expr (union exp_element **pc);
77 static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
80 static void gen_sign_extend (struct agent_expr *, struct type *);
81 static void gen_extend (struct agent_expr *, struct type *);
82 static void gen_fetch (struct agent_expr *, struct type *);
83 static void gen_left_shift (struct agent_expr *, int);
86 static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
87 static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
88 static void gen_offset (struct agent_expr *ax, int offset);
89 static void gen_sym_offset (struct agent_expr *, struct symbol *);
90 static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
91 struct axs_value *value, struct symbol *var);
94 static void gen_int_literal (struct agent_expr *ax,
95 struct axs_value *value,
96 LONGEST k, struct type *type);
99 static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
100 static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
101 struct axs_value *value);
102 static int type_wider_than (struct type *type1, struct type *type2);
103 static struct type *max_type (struct type *type1, struct type *type2);
104 static void gen_conversion (struct agent_expr *ax,
105 struct type *from, struct type *to);
106 static int is_nontrivial_conversion (struct type *from, struct type *to);
107 static void gen_usual_arithmetic (struct expression *exp,
108 struct agent_expr *ax,
109 struct axs_value *value1,
110 struct axs_value *value2);
111 static void gen_integral_promotions (struct expression *exp,
112 struct agent_expr *ax,
113 struct axs_value *value);
114 static void gen_cast (struct agent_expr *ax,
115 struct axs_value *value, struct type *type);
116 static void gen_scale (struct agent_expr *ax,
117 enum agent_op op, struct type *type);
118 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
119 struct axs_value *value1, struct axs_value *value2);
120 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
121 struct axs_value *value1, struct axs_value *value2);
122 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
123 struct axs_value *value1, struct axs_value *value2,
124 struct type *result_type);
125 static void gen_binop (struct agent_expr *ax,
126 struct axs_value *value,
127 struct axs_value *value1,
128 struct axs_value *value2,
130 enum agent_op op_unsigned, int may_carry, char *name);
131 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
132 struct type *result_type);
133 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
134 static void gen_deref (struct agent_expr *, struct axs_value *);
135 static void gen_address_of (struct agent_expr *, struct axs_value *);
136 static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
137 struct axs_value *value,
138 struct type *type, int start, int end);
139 static void gen_primitive_field (struct expression *exp,
140 struct agent_expr *ax,
141 struct axs_value *value,
142 int offset, int fieldno, struct type *type);
143 static int gen_struct_ref_recursive (struct expression *exp,
144 struct agent_expr *ax,
145 struct axs_value *value,
146 char *field, int offset,
148 static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
149 struct axs_value *value,
151 char *operator_name, char *operand_name);
152 static void gen_static_field (struct gdbarch *gdbarch,
153 struct agent_expr *ax, struct axs_value *value,
154 struct type *type, int fieldno);
155 static void gen_repeat (struct expression *exp, union exp_element **pc,
156 struct agent_expr *ax, struct axs_value *value);
157 static void gen_sizeof (struct expression *exp, union exp_element **pc,
158 struct agent_expr *ax, struct axs_value *value,
159 struct type *size_type);
160 static void gen_expr (struct expression *exp, union exp_element **pc,
161 struct agent_expr *ax, struct axs_value *value);
162 static void gen_expr_binop_rest (struct expression *exp,
163 enum exp_opcode op, union exp_element **pc,
164 struct agent_expr *ax,
165 struct axs_value *value,
166 struct axs_value *value1,
167 struct axs_value *value2);
169 static void agent_command (char *exp, int from_tty);
172 /* Detecting constant expressions. */
174 /* If the variable reference at *PC is a constant, return its value.
175 Otherwise, return zero.
177 Hey, Wally! How can a variable reference be a constant?
179 Well, Beav, this function really handles the OP_VAR_VALUE operator,
180 not specifically variable references. GDB uses OP_VAR_VALUE to
181 refer to any kind of symbolic reference: function names, enum
182 elements, and goto labels are all handled through the OP_VAR_VALUE
183 operator, even though they're constants. It makes sense given the
186 Gee, Wally, don'cha wonder sometimes if data representations that
187 subvert commonly accepted definitions of terms in favor of heavily
188 context-specific interpretations are really just a tool of the
189 programming hegemony to preserve their power and exclude the
192 static struct value *
193 const_var_ref (struct symbol *var)
195 struct type *type = SYMBOL_TYPE (var);
197 switch (SYMBOL_CLASS (var))
200 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
203 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
211 /* If the expression starting at *PC has a constant value, return it.
212 Otherwise, return zero. If we return a value, then *PC will be
213 advanced to the end of it. If we return zero, *PC could be
215 static struct value *
216 const_expr (union exp_element **pc)
218 enum exp_opcode op = (*pc)->opcode;
225 struct type *type = (*pc)[1].type;
226 LONGEST k = (*pc)[2].longconst;
229 return value_from_longest (type, k);
234 struct value *v = const_var_ref ((*pc)[2].symbol);
240 /* We could add more operators in here. */
244 v1 = const_expr (pc);
246 return value_neg (v1);
256 /* Like const_expr, but guarantee also that *PC is undisturbed if the
257 expression is not constant. */
258 static struct value *
259 maybe_const_expr (union exp_element **pc)
261 union exp_element *tentative_pc = *pc;
262 struct value *v = const_expr (&tentative_pc);
264 /* If we got a value, then update the real PC. */
272 /* Generating bytecode from GDB expressions: general assumptions */
274 /* Here are a few general assumptions made throughout the code; if you
275 want to make a change that contradicts one of these, then you'd
276 better scan things pretty thoroughly.
278 - We assume that all values occupy one stack element. For example,
279 sometimes we'll swap to get at the left argument to a binary
280 operator. If we decide that void values should occupy no stack
281 elements, or that synthetic arrays (whose size is determined at
282 run time, created by the `@' operator) should occupy two stack
283 elements (address and length), then this will cause trouble.
285 - We assume the stack elements are infinitely wide, and that we
286 don't have to worry what happens if the user requests an
287 operation that is wider than the actual interpreter's stack.
288 That is, it's up to the interpreter to handle directly all the
289 integer widths the user has access to. (Woe betide the language
292 - We don't support side effects. Thus, we don't have to worry about
293 GCC's generalized lvalues, function calls, etc.
295 - We don't support floating point. Many places where we switch on
296 some type don't bother to include cases for floating point; there
297 may be even more subtle ways this assumption exists. For
298 example, the arguments to % must be integers.
300 - We assume all subexpressions have a static, unchanging type. If
301 we tried to support convenience variables, this would be a
304 - All values on the stack should always be fully zero- or
307 (I wasn't sure whether to choose this or its opposite --- that
308 only addresses are assumed extended --- but it turns out that
309 neither convention completely eliminates spurious extend
310 operations (if everything is always extended, then you have to
311 extend after add, because it could overflow; if nothing is
312 extended, then you end up producing extends whenever you change
313 sizes), and this is simpler.) */
316 /* Generating bytecode from GDB expressions: the `trace' kludge */
318 /* The compiler in this file is a general-purpose mechanism for
319 translating GDB expressions into bytecode. One ought to be able to
320 find a million and one uses for it.
322 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
323 of expediency. Let he who is without sin cast the first stone.
325 For the data tracing facility, we need to insert `trace' bytecodes
326 before each data fetch; this records all the memory that the
327 expression touches in the course of evaluation, so that memory will
328 be available when the user later tries to evaluate the expression
331 This should be done (I think) in a post-processing pass, that walks
332 an arbitrary agent expression and inserts `trace' operations at the
333 appropriate points. But it's much faster to just hack them
334 directly into the code. And since we're in a crunch, that's what
337 Setting the flag trace_kludge to non-zero enables the code that
338 emits the trace bytecodes at the appropriate points. */
341 /* Inspired by trace_kludge, this indicates that pointers to chars
342 should get an added tracenz bytecode to record nonzero bytes, up to
343 a length that is the value of trace_string_kludge. */
344 int trace_string_kludge;
346 /* Scan for all static fields in the given class, including any base
347 classes, and generate tracing bytecodes for each. */
350 gen_trace_static_fields (struct gdbarch *gdbarch,
351 struct agent_expr *ax,
354 int i, nbases = TYPE_N_BASECLASSES (type);
355 struct axs_value value;
357 CHECK_TYPEDEF (type);
359 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
361 if (field_is_static (&TYPE_FIELD (type, i)))
363 gen_static_field (gdbarch, ax, &value, type, i);
364 if (value.optimized_out)
368 case axs_lvalue_memory:
370 int length = TYPE_LENGTH (check_typedef (value.type));
372 ax_const_l (ax, length);
373 ax_simple (ax, aop_trace);
377 case axs_lvalue_register:
378 /* We don't actually need the register's value to be pushed,
379 just note that we need it to be collected. */
380 ax_reg_mask (ax, value.u.reg);
388 /* Now scan through base classes recursively. */
389 for (i = 0; i < nbases; i++)
391 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
393 gen_trace_static_fields (gdbarch, ax, basetype);
397 /* Trace the lvalue on the stack, if it needs it. In either case, pop
398 the value. Useful on the left side of a comma, and at the end of
399 an expression being used for tracing. */
401 gen_traced_pop (struct gdbarch *gdbarch,
402 struct agent_expr *ax, struct axs_value *value)
404 int string_trace = 0;
405 if (trace_string_kludge
406 && TYPE_CODE (value->type) == TYPE_CODE_PTR
407 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
417 ax_const_l (ax, trace_string_kludge);
418 ax_simple (ax, aop_tracenz);
421 /* We don't trace rvalues, just the lvalues necessary to
422 produce them. So just dispose of this value. */
423 ax_simple (ax, aop_pop);
426 case axs_lvalue_memory:
428 int length = TYPE_LENGTH (check_typedef (value->type));
431 ax_simple (ax, aop_dup);
433 /* There's no point in trying to use a trace_quick bytecode
434 here, since "trace_quick SIZE pop" is three bytes, whereas
435 "const8 SIZE trace" is also three bytes, does the same
436 thing, and the simplest code which generates that will also
437 work correctly for objects with large sizes. */
438 ax_const_l (ax, length);
439 ax_simple (ax, aop_trace);
443 ax_simple (ax, aop_ref32);
444 ax_const_l (ax, trace_string_kludge);
445 ax_simple (ax, aop_tracenz);
450 case axs_lvalue_register:
451 /* We don't actually need the register's value to be on the
452 stack, and the target will get heartburn if the register is
453 larger than will fit in a stack, so just mark it for
454 collection and be done with it. */
455 ax_reg_mask (ax, value->u.reg);
457 /* But if the register points to a string, assume the value
458 will fit on the stack and push it anyway. */
461 ax_reg (ax, value->u.reg);
462 ax_const_l (ax, trace_string_kludge);
463 ax_simple (ax, aop_tracenz);
468 /* If we're not tracing, just pop the value. */
469 ax_simple (ax, aop_pop);
471 /* To trace C++ classes with static fields stored elsewhere. */
473 && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
474 || TYPE_CODE (value->type) == TYPE_CODE_UNION))
475 gen_trace_static_fields (gdbarch, ax, value->type);
480 /* Generating bytecode from GDB expressions: helper functions */
482 /* Assume that the lower bits of the top of the stack is a value of
483 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
485 gen_sign_extend (struct agent_expr *ax, struct type *type)
487 /* Do we need to sign-extend this? */
488 if (!TYPE_UNSIGNED (type))
489 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
493 /* Assume the lower bits of the top of the stack hold a value of type
494 TYPE, and the upper bits are garbage. Sign-extend or truncate as
497 gen_extend (struct agent_expr *ax, struct type *type)
499 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
502 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
506 /* Assume that the top of the stack contains a value of type "pointer
507 to TYPE"; generate code to fetch its value. Note that TYPE is the
508 target type, not the pointer type. */
510 gen_fetch (struct agent_expr *ax, struct type *type)
514 /* Record the area of memory we're about to fetch. */
515 ax_trace_quick (ax, TYPE_LENGTH (type));
518 switch (TYPE_CODE (type))
526 /* It's a scalar value, so we know how to dereference it. How
527 many bytes long is it? */
528 switch (TYPE_LENGTH (type))
530 case 8 / TARGET_CHAR_BIT:
531 ax_simple (ax, aop_ref8);
533 case 16 / TARGET_CHAR_BIT:
534 ax_simple (ax, aop_ref16);
536 case 32 / TARGET_CHAR_BIT:
537 ax_simple (ax, aop_ref32);
539 case 64 / TARGET_CHAR_BIT:
540 ax_simple (ax, aop_ref64);
543 /* Either our caller shouldn't have asked us to dereference
544 that pointer (other code's fault), or we're not
545 implementing something we should be (this code's fault).
546 In any case, it's a bug the user shouldn't see. */
548 internal_error (__FILE__, __LINE__,
549 _("gen_fetch: strange size"));
552 gen_sign_extend (ax, type);
556 /* Either our caller shouldn't have asked us to dereference that
557 pointer (other code's fault), or we're not implementing
558 something we should be (this code's fault). In any case,
559 it's a bug the user shouldn't see. */
560 internal_error (__FILE__, __LINE__,
561 _("gen_fetch: bad type code"));
566 /* Generate code to left shift the top of the stack by DISTANCE bits, or
567 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
568 unsigned (logical) right shifts. */
570 gen_left_shift (struct agent_expr *ax, int distance)
574 ax_const_l (ax, distance);
575 ax_simple (ax, aop_lsh);
577 else if (distance < 0)
579 ax_const_l (ax, -distance);
580 ax_simple (ax, aop_rsh_unsigned);
586 /* Generating bytecode from GDB expressions: symbol references */
588 /* Generate code to push the base address of the argument portion of
589 the top stack frame. */
591 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
594 LONGEST frame_offset;
596 gdbarch_virtual_frame_pointer (gdbarch,
597 ax->scope, &frame_reg, &frame_offset);
598 ax_reg (ax, frame_reg);
599 gen_offset (ax, frame_offset);
603 /* Generate code to push the base address of the locals portion of the
606 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
609 LONGEST frame_offset;
611 gdbarch_virtual_frame_pointer (gdbarch,
612 ax->scope, &frame_reg, &frame_offset);
613 ax_reg (ax, frame_reg);
614 gen_offset (ax, frame_offset);
618 /* Generate code to add OFFSET to the top of the stack. Try to
619 generate short and readable code. We use this for getting to
620 variables on the stack, and structure members. If we were
621 programming in ML, it would be clearer why these are the same
624 gen_offset (struct agent_expr *ax, int offset)
626 /* It would suffice to simply push the offset and add it, but this
627 makes it easier to read positive and negative offsets in the
631 ax_const_l (ax, offset);
632 ax_simple (ax, aop_add);
636 ax_const_l (ax, -offset);
637 ax_simple (ax, aop_sub);
642 /* In many cases, a symbol's value is the offset from some other
643 address (stack frame, base register, etc.) Generate code to add
644 VAR's value to the top of the stack. */
646 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
648 gen_offset (ax, SYMBOL_VALUE (var));
652 /* Generate code for a variable reference to AX. The variable is the
653 symbol VAR. Set VALUE to describe the result. */
656 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
657 struct axs_value *value, struct symbol *var)
659 /* Dereference any typedefs. */
660 value->type = check_typedef (SYMBOL_TYPE (var));
661 value->optimized_out = 0;
663 /* I'm imitating the code in read_var_value. */
664 switch (SYMBOL_CLASS (var))
666 case LOC_CONST: /* A constant, like an enum value. */
667 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
668 value->kind = axs_rvalue;
671 case LOC_LABEL: /* A goto label, being used as a value. */
672 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
673 value->kind = axs_rvalue;
676 case LOC_CONST_BYTES:
677 internal_error (__FILE__, __LINE__,
678 _("gen_var_ref: LOC_CONST_BYTES "
679 "symbols are not supported"));
681 /* Variable at a fixed location in memory. Easy. */
683 /* Push the address of the variable. */
684 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
685 value->kind = axs_lvalue_memory;
688 case LOC_ARG: /* var lives in argument area of frame */
689 gen_frame_args_address (gdbarch, ax);
690 gen_sym_offset (ax, var);
691 value->kind = axs_lvalue_memory;
694 case LOC_REF_ARG: /* As above, but the frame slot really
695 holds the address of the variable. */
696 gen_frame_args_address (gdbarch, ax);
697 gen_sym_offset (ax, var);
698 /* Don't assume any particular pointer size. */
699 gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
700 value->kind = axs_lvalue_memory;
703 case LOC_LOCAL: /* var lives in locals area of frame */
704 gen_frame_locals_address (gdbarch, ax);
705 gen_sym_offset (ax, var);
706 value->kind = axs_lvalue_memory;
710 error (_("Cannot compute value of typedef `%s'."),
711 SYMBOL_PRINT_NAME (var));
715 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
716 value->kind = axs_rvalue;
720 /* Don't generate any code at all; in the process of treating
721 this as an lvalue or rvalue, the caller will generate the
723 value->kind = axs_lvalue_register;
724 value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
727 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
728 register, not on the stack. Simpler than LOC_REGISTER
729 because it's just like any other case where the thing
730 has a real address. */
731 case LOC_REGPARM_ADDR:
732 ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
733 value->kind = axs_lvalue_memory;
738 struct minimal_symbol *msym
739 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
742 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
744 /* Push the address of the variable. */
745 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
746 value->kind = axs_lvalue_memory;
751 /* FIXME: cagney/2004-01-26: It should be possible to
752 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
753 Unfortunately DWARF 2 stores the frame-base (instead of the
754 function) location in a function's symbol. Oops! For the
755 moment enable this when/where applicable. */
756 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
759 case LOC_OPTIMIZED_OUT:
760 /* Flag this, but don't say anything; leave it up to callers to
762 value->optimized_out = 1;
766 error (_("Cannot find value of botched symbol `%s'."),
767 SYMBOL_PRINT_NAME (var));
774 /* Generating bytecode from GDB expressions: literals */
777 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
781 value->kind = axs_rvalue;
782 value->type = check_typedef (type);
787 /* Generating bytecode from GDB expressions: unary conversions, casts */
789 /* Take what's on the top of the stack (as described by VALUE), and
790 try to make an rvalue out of it. Signal an error if we can't do
793 require_rvalue (struct agent_expr *ax, struct axs_value *value)
795 /* Only deal with scalars, structs and such may be too large
796 to fit in a stack entry. */
797 value->type = check_typedef (value->type);
798 if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
799 || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
800 || TYPE_CODE (value->type) == TYPE_CODE_UNION
801 || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
802 error (_("Value not scalar: cannot be an rvalue."));
807 /* It's already an rvalue. */
810 case axs_lvalue_memory:
811 /* The top of stack is the address of the object. Dereference. */
812 gen_fetch (ax, value->type);
815 case axs_lvalue_register:
816 /* There's nothing on the stack, but value->u.reg is the
817 register number containing the value.
819 When we add floating-point support, this is going to have to
820 change. What about SPARC register pairs, for example? */
821 ax_reg (ax, value->u.reg);
822 gen_extend (ax, value->type);
826 value->kind = axs_rvalue;
830 /* Assume the top of the stack is described by VALUE, and perform the
831 usual unary conversions. This is motivated by ANSI 6.2.2, but of
832 course GDB expressions are not ANSI; they're the mishmash union of
833 a bunch of languages. Rah.
835 NOTE! This function promises to produce an rvalue only when the
836 incoming value is of an appropriate type. In other words, the
837 consumer of the value this function produces may assume the value
838 is an rvalue only after checking its type.
840 The immediate issue is that if the user tries to use a structure or
841 union as an operand of, say, the `+' operator, we don't want to try
842 to convert that structure to an rvalue; require_rvalue will bomb on
843 structs and unions. Rather, we want to simply pass the struct
844 lvalue through unchanged, and let `+' raise an error. */
847 gen_usual_unary (struct expression *exp, struct agent_expr *ax,
848 struct axs_value *value)
850 /* We don't have to generate any code for the usual integral
851 conversions, since values are always represented as full-width on
852 the stack. Should we tweak the type? */
854 /* Some types require special handling. */
855 switch (TYPE_CODE (value->type))
857 /* Functions get converted to a pointer to the function. */
859 value->type = lookup_pointer_type (value->type);
860 value->kind = axs_rvalue; /* Should always be true, but just in case. */
863 /* Arrays get converted to a pointer to their first element, and
864 are no longer an lvalue. */
865 case TYPE_CODE_ARRAY:
867 struct type *elements = TYPE_TARGET_TYPE (value->type);
869 value->type = lookup_pointer_type (elements);
870 value->kind = axs_rvalue;
871 /* We don't need to generate any code; the address of the array
872 is also the address of its first element. */
876 /* Don't try to convert structures and unions to rvalues. Let the
877 consumer signal an error. */
878 case TYPE_CODE_STRUCT:
879 case TYPE_CODE_UNION:
883 /* If the value is an lvalue, dereference it. */
884 require_rvalue (ax, value);
888 /* Return non-zero iff the type TYPE1 is considered "wider" than the
889 type TYPE2, according to the rules described in gen_usual_arithmetic. */
891 type_wider_than (struct type *type1, struct type *type2)
893 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
894 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
895 && TYPE_UNSIGNED (type1)
896 && !TYPE_UNSIGNED (type2)));
900 /* Return the "wider" of the two types TYPE1 and TYPE2. */
902 max_type (struct type *type1, struct type *type2)
904 return type_wider_than (type1, type2) ? type1 : type2;
908 /* Generate code to convert a scalar value of type FROM to type TO. */
910 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
912 /* Perhaps there is a more graceful way to state these rules. */
914 /* If we're converting to a narrower type, then we need to clear out
916 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
917 gen_extend (ax, from);
919 /* If the two values have equal width, but different signednesses,
920 then we need to extend. */
921 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
923 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
927 /* If we're converting to a wider type, and becoming unsigned, then
928 we need to zero out any possible sign bits. */
929 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
931 if (TYPE_UNSIGNED (to))
937 /* Return non-zero iff the type FROM will require any bytecodes to be
938 emitted to be converted to the type TO. */
940 is_nontrivial_conversion (struct type *from, struct type *to)
942 struct agent_expr *ax = new_agent_expr (NULL, 0);
945 /* Actually generate the code, and see if anything came out. At the
946 moment, it would be trivial to replicate the code in
947 gen_conversion here, but in the future, when we're supporting
948 floating point and the like, it may not be. Doing things this
949 way allows this function to be independent of the logic in
951 gen_conversion (ax, from, to);
952 nontrivial = ax->len > 0;
953 free_agent_expr (ax);
958 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
959 6.2.1.5) for the two operands of an arithmetic operator. This
960 effectively finds a "least upper bound" type for the two arguments,
961 and promotes each argument to that type. *VALUE1 and *VALUE2
962 describe the values as they are passed in, and as they are left. */
964 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
965 struct axs_value *value1, struct axs_value *value2)
967 /* Do the usual binary conversions. */
968 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
969 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
971 /* The ANSI integral promotions seem to work this way: Order the
972 integer types by size, and then by signedness: an n-bit
973 unsigned type is considered "wider" than an n-bit signed
974 type. Promote to the "wider" of the two types, and always
975 promote at least to int. */
976 struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
977 max_type (value1->type, value2->type));
979 /* Deal with value2, on the top of the stack. */
980 gen_conversion (ax, value2->type, target);
982 /* Deal with value1, not on the top of the stack. Don't
983 generate the `swap' instructions if we're not actually going
985 if (is_nontrivial_conversion (value1->type, target))
987 ax_simple (ax, aop_swap);
988 gen_conversion (ax, value1->type, target);
989 ax_simple (ax, aop_swap);
992 value1->type = value2->type = check_typedef (target);
997 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
998 the value on the top of the stack, as described by VALUE. Assume
999 the value has integral type. */
1001 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
1002 struct axs_value *value)
1004 const struct builtin_type *builtin = builtin_type (exp->gdbarch);
1006 if (!type_wider_than (value->type, builtin->builtin_int))
1008 gen_conversion (ax, value->type, builtin->builtin_int);
1009 value->type = builtin->builtin_int;
1011 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
1013 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
1014 value->type = builtin->builtin_unsigned_int;
1019 /* Generate code for a cast to TYPE. */
1021 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
1023 /* GCC does allow casts to yield lvalues, so this should be fixed
1024 before merging these changes into the trunk. */
1025 require_rvalue (ax, value);
1026 /* Dereference typedefs. */
1027 type = check_typedef (type);
1029 switch (TYPE_CODE (type))
1033 /* It's implementation-defined, and I'll bet this is what GCC
1037 case TYPE_CODE_ARRAY:
1038 case TYPE_CODE_STRUCT:
1039 case TYPE_CODE_UNION:
1040 case TYPE_CODE_FUNC:
1041 error (_("Invalid type cast: intended type must be scalar."));
1043 case TYPE_CODE_ENUM:
1044 case TYPE_CODE_BOOL:
1045 /* We don't have to worry about the size of the value, because
1046 all our integral values are fully sign-extended, and when
1047 casting pointers we can do anything we like. Is there any
1048 way for us to know what GCC actually does with a cast like
1053 gen_conversion (ax, value->type, type);
1056 case TYPE_CODE_VOID:
1057 /* We could pop the value, and rely on everyone else to check
1058 the type and notice that this value doesn't occupy a stack
1059 slot. But for now, leave the value on the stack, and
1060 preserve the "value == stack element" assumption. */
1064 error (_("Casts to requested type are not yet implemented."));
1072 /* Generating bytecode from GDB expressions: arithmetic */
1074 /* Scale the integer on the top of the stack by the size of the target
1075 of the pointer type TYPE. */
1077 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1079 struct type *element = TYPE_TARGET_TYPE (type);
1081 if (TYPE_LENGTH (element) != 1)
1083 ax_const_l (ax, TYPE_LENGTH (element));
1089 /* Generate code for pointer arithmetic PTR + INT. */
1091 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1092 struct axs_value *value1, struct axs_value *value2)
1094 gdb_assert (pointer_type (value1->type));
1095 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1097 gen_scale (ax, aop_mul, value1->type);
1098 ax_simple (ax, aop_add);
1099 gen_extend (ax, value1->type); /* Catch overflow. */
1100 value->type = value1->type;
1101 value->kind = axs_rvalue;
1105 /* Generate code for pointer arithmetic PTR - INT. */
1107 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1108 struct axs_value *value1, struct axs_value *value2)
1110 gdb_assert (pointer_type (value1->type));
1111 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1113 gen_scale (ax, aop_mul, value1->type);
1114 ax_simple (ax, aop_sub);
1115 gen_extend (ax, value1->type); /* Catch overflow. */
1116 value->type = value1->type;
1117 value->kind = axs_rvalue;
1121 /* Generate code for pointer arithmetic PTR - PTR. */
1123 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1124 struct axs_value *value1, struct axs_value *value2,
1125 struct type *result_type)
1127 gdb_assert (pointer_type (value1->type));
1128 gdb_assert (pointer_type (value2->type));
1130 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1131 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1133 First argument of `-' is a pointer, but second argument is neither\n\
1134 an integer nor a pointer of the same type."));
1136 ax_simple (ax, aop_sub);
1137 gen_scale (ax, aop_div_unsigned, value1->type);
1138 value->type = result_type;
1139 value->kind = axs_rvalue;
1143 gen_equal (struct agent_expr *ax, struct axs_value *value,
1144 struct axs_value *value1, struct axs_value *value2,
1145 struct type *result_type)
1147 if (pointer_type (value1->type) || pointer_type (value2->type))
1148 ax_simple (ax, aop_equal);
1150 gen_binop (ax, value, value1, value2,
1151 aop_equal, aop_equal, 0, "equal");
1152 value->type = result_type;
1153 value->kind = axs_rvalue;
1157 gen_less (struct agent_expr *ax, struct axs_value *value,
1158 struct axs_value *value1, struct axs_value *value2,
1159 struct type *result_type)
1161 if (pointer_type (value1->type) || pointer_type (value2->type))
1162 ax_simple (ax, aop_less_unsigned);
1164 gen_binop (ax, value, value1, value2,
1165 aop_less_signed, aop_less_unsigned, 0, "less than");
1166 value->type = result_type;
1167 value->kind = axs_rvalue;
1170 /* Generate code for a binary operator that doesn't do pointer magic.
1171 We set VALUE to describe the result value; we assume VALUE1 and
1172 VALUE2 describe the two operands, and that they've undergone the
1173 usual binary conversions. MAY_CARRY should be non-zero iff the
1174 result needs to be extended. NAME is the English name of the
1175 operator, used in error messages */
1177 gen_binop (struct agent_expr *ax, struct axs_value *value,
1178 struct axs_value *value1, struct axs_value *value2,
1179 enum agent_op op, enum agent_op op_unsigned,
1180 int may_carry, char *name)
1182 /* We only handle INT op INT. */
1183 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1184 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1185 error (_("Invalid combination of types in %s."), name);
1188 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1190 gen_extend (ax, value1->type); /* catch overflow */
1191 value->type = value1->type;
1192 value->kind = axs_rvalue;
1197 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1198 struct type *result_type)
1200 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1201 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1202 error (_("Invalid type of operand to `!'."));
1204 ax_simple (ax, aop_log_not);
1205 value->type = result_type;
1210 gen_complement (struct agent_expr *ax, struct axs_value *value)
1212 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1213 error (_("Invalid type of operand to `~'."));
1215 ax_simple (ax, aop_bit_not);
1216 gen_extend (ax, value->type);
1221 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1223 /* Dereference the value on the top of the stack. */
1225 gen_deref (struct agent_expr *ax, struct axs_value *value)
1227 /* The caller should check the type, because several operators use
1228 this, and we don't know what error message to generate. */
1229 if (!pointer_type (value->type))
1230 internal_error (__FILE__, __LINE__,
1231 _("gen_deref: expected a pointer"));
1233 /* We've got an rvalue now, which is a pointer. We want to yield an
1234 lvalue, whose address is exactly that pointer. So we don't
1235 actually emit any code; we just change the type from "Pointer to
1236 T" to "T", and mark the value as an lvalue in memory. Leave it
1237 to the consumer to actually dereference it. */
1238 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1239 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1240 error (_("Attempt to dereference a generic pointer."));
1241 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1242 ? axs_rvalue : axs_lvalue_memory);
1246 /* Produce the address of the lvalue on the top of the stack. */
1248 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1250 /* Special case for taking the address of a function. The ANSI
1251 standard describes this as a special case, too, so this
1252 arrangement is not without motivation. */
1253 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1254 /* The value's already an rvalue on the stack, so we just need to
1256 value->type = lookup_pointer_type (value->type);
1258 switch (value->kind)
1261 error (_("Operand of `&' is an rvalue, which has no address."));
1263 case axs_lvalue_register:
1264 error (_("Operand of `&' is in a register, and has no address."));
1266 case axs_lvalue_memory:
1267 value->kind = axs_rvalue;
1268 value->type = lookup_pointer_type (value->type);
1273 /* Generate code to push the value of a bitfield of a structure whose
1274 address is on the top of the stack. START and END give the
1275 starting and one-past-ending *bit* numbers of the field within the
1278 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1279 struct axs_value *value, struct type *type,
1282 /* Note that ops[i] fetches 8 << i bits. */
1283 static enum agent_op ops[]
1284 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1285 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1287 /* We don't want to touch any byte that the bitfield doesn't
1288 actually occupy; we shouldn't make any accesses we're not
1289 explicitly permitted to. We rely here on the fact that the
1290 bytecode `ref' operators work on unaligned addresses.
1292 It takes some fancy footwork to get the stack to work the way
1293 we'd like. Say we're retrieving a bitfield that requires three
1294 fetches. Initially, the stack just contains the address:
1296 For the first fetch, we duplicate the address
1298 then add the byte offset, do the fetch, and shift and mask as
1299 needed, yielding a fragment of the value, properly aligned for
1300 the final bitwise or:
1302 then we swap, and repeat the process:
1303 frag1 addr --- address on top
1304 frag1 addr addr --- duplicate it
1305 frag1 addr frag2 --- get second fragment
1306 frag1 frag2 addr --- swap again
1307 frag1 frag2 frag3 --- get third fragment
1308 Notice that, since the third fragment is the last one, we don't
1309 bother duplicating the address this time. Now we have all the
1310 fragments on the stack, and we can simply `or' them together,
1311 yielding the final value of the bitfield. */
1313 /* The first and one-after-last bits in the field, but rounded down
1314 and up to byte boundaries. */
1315 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1316 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1320 /* current bit offset within the structure */
1323 /* The index in ops of the opcode we're considering. */
1326 /* The number of fragments we generated in the process. Probably
1327 equal to the number of `one' bits in bytesize, but who cares? */
1330 /* Dereference any typedefs. */
1331 type = check_typedef (type);
1333 /* Can we fetch the number of bits requested at all? */
1334 if ((end - start) > ((1 << num_ops) * 8))
1335 internal_error (__FILE__, __LINE__,
1336 _("gen_bitfield_ref: bitfield too wide"));
1338 /* Note that we know here that we only need to try each opcode once.
1339 That may not be true on machines with weird byte sizes. */
1340 offset = bound_start;
1342 for (op = num_ops - 1; op >= 0; op--)
1344 /* number of bits that ops[op] would fetch */
1345 int op_size = 8 << op;
1347 /* The stack at this point, from bottom to top, contains zero or
1348 more fragments, then the address. */
1350 /* Does this fetch fit within the bitfield? */
1351 if (offset + op_size <= bound_end)
1353 /* Is this the last fragment? */
1354 int last_frag = (offset + op_size == bound_end);
1357 ax_simple (ax, aop_dup); /* keep a copy of the address */
1359 /* Add the offset. */
1360 gen_offset (ax, offset / TARGET_CHAR_BIT);
1364 /* Record the area of memory we're about to fetch. */
1365 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1368 /* Perform the fetch. */
1369 ax_simple (ax, ops[op]);
1371 /* Shift the bits we have to their proper position.
1372 gen_left_shift will generate right shifts when the operand
1375 A big-endian field diagram to ponder:
1376 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1377 +------++------++------++------++------++------++------++------+
1378 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1380 bit number 16 32 48 53
1381 These are bit numbers as supplied by GDB. Note that the
1382 bit numbers run from right to left once you've fetched the
1385 A little-endian field diagram to ponder:
1386 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1387 +------++------++------++------++------++------++------++------+
1388 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1390 bit number 48 32 16 4 0
1392 In both cases, the most significant end is on the left
1393 (i.e. normal numeric writing order), which means that you
1394 don't go crazy thinking about `left' and `right' shifts.
1396 We don't have to worry about masking yet:
1397 - If they contain garbage off the least significant end, then we
1398 must be looking at the low end of the field, and the right
1399 shift will wipe them out.
1400 - If they contain garbage off the most significant end, then we
1401 must be looking at the most significant end of the word, and
1402 the sign/zero extension will wipe them out.
1403 - If we're in the interior of the word, then there is no garbage
1404 on either end, because the ref operators zero-extend. */
1405 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1406 gen_left_shift (ax, end - (offset + op_size));
1408 gen_left_shift (ax, offset - start);
1411 /* Bring the copy of the address up to the top. */
1412 ax_simple (ax, aop_swap);
1419 /* Generate enough bitwise `or' operations to combine all the
1420 fragments we left on the stack. */
1421 while (fragment_count-- > 1)
1422 ax_simple (ax, aop_bit_or);
1424 /* Sign- or zero-extend the value as appropriate. */
1425 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1427 /* This is *not* an lvalue. Ugh. */
1428 value->kind = axs_rvalue;
1432 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1433 is an accumulated offset (in bytes), will be nonzero for objects
1434 embedded in other objects, like C++ base classes. Behavior should
1435 generally follow value_primitive_field. */
1438 gen_primitive_field (struct expression *exp,
1439 struct agent_expr *ax, struct axs_value *value,
1440 int offset, int fieldno, struct type *type)
1442 /* Is this a bitfield? */
1443 if (TYPE_FIELD_PACKED (type, fieldno))
1444 gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1445 (offset * TARGET_CHAR_BIT
1446 + TYPE_FIELD_BITPOS (type, fieldno)),
1447 (offset * TARGET_CHAR_BIT
1448 + TYPE_FIELD_BITPOS (type, fieldno)
1449 + TYPE_FIELD_BITSIZE (type, fieldno)));
1452 gen_offset (ax, offset
1453 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1454 value->kind = axs_lvalue_memory;
1455 value->type = TYPE_FIELD_TYPE (type, fieldno);
1459 /* Search for the given field in either the given type or one of its
1460 base classes. Return 1 if found, 0 if not. */
1463 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1464 struct axs_value *value,
1465 char *field, int offset, struct type *type)
1468 int nbases = TYPE_N_BASECLASSES (type);
1470 CHECK_TYPEDEF (type);
1472 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1474 const char *this_name = TYPE_FIELD_NAME (type, i);
1478 if (strcmp (field, this_name) == 0)
1480 /* Note that bytecodes for the struct's base (aka
1481 "this") will have been generated already, which will
1482 be unnecessary but not harmful if the static field is
1483 being handled as a global. */
1484 if (field_is_static (&TYPE_FIELD (type, i)))
1486 gen_static_field (exp->gdbarch, ax, value, type, i);
1487 if (value->optimized_out)
1488 error (_("static field `%s' has been "
1489 "optimized out, cannot use"),
1494 gen_primitive_field (exp, ax, value, offset, i, type);
1497 #if 0 /* is this right? */
1498 if (this_name[0] == '\0')
1499 internal_error (__FILE__, __LINE__,
1500 _("find_field: anonymous unions not supported"));
1505 /* Now scan through base classes recursively. */
1506 for (i = 0; i < nbases; i++)
1508 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1510 rslt = gen_struct_ref_recursive (exp, ax, value, field,
1511 offset + TYPE_BASECLASS_BITPOS (type, i)
1518 /* Not found anywhere, flag so caller can complain. */
1522 /* Generate code to reference the member named FIELD of a structure or
1523 union. The top of the stack, as described by VALUE, should have
1524 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1525 the operator being compiled, and OPERAND_NAME is the kind of thing
1526 it operates on; we use them in error messages. */
1528 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1529 struct axs_value *value, char *field,
1530 char *operator_name, char *operand_name)
1535 /* Follow pointers until we reach a non-pointer. These aren't the C
1536 semantics, but they're what the normal GDB evaluator does, so we
1537 should at least be consistent. */
1538 while (pointer_type (value->type))
1540 require_rvalue (ax, value);
1541 gen_deref (ax, value);
1543 type = check_typedef (value->type);
1545 /* This must yield a structure or a union. */
1546 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1547 && TYPE_CODE (type) != TYPE_CODE_UNION)
1548 error (_("The left operand of `%s' is not a %s."),
1549 operator_name, operand_name);
1551 /* And it must be in memory; we don't deal with structure rvalues,
1552 or structures living in registers. */
1553 if (value->kind != axs_lvalue_memory)
1554 error (_("Structure does not live in memory."));
1556 /* Search through fields and base classes recursively. */
1557 found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1560 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1561 field, TYPE_TAG_NAME (type));
1565 gen_namespace_elt (struct expression *exp,
1566 struct agent_expr *ax, struct axs_value *value,
1567 const struct type *curtype, char *name);
1569 gen_maybe_namespace_elt (struct expression *exp,
1570 struct agent_expr *ax, struct axs_value *value,
1571 const struct type *curtype, char *name);
1574 gen_static_field (struct gdbarch *gdbarch,
1575 struct agent_expr *ax, struct axs_value *value,
1576 struct type *type, int fieldno)
1578 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1580 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1581 value->kind = axs_lvalue_memory;
1582 value->type = TYPE_FIELD_TYPE (type, fieldno);
1583 value->optimized_out = 0;
1587 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1588 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
1592 gen_var_ref (gdbarch, ax, value, sym);
1594 /* Don't error if the value was optimized out, we may be
1595 scanning all static fields and just want to pass over this
1596 and continue with the rest. */
1600 /* Silently assume this was optimized out; class printing
1601 will let the user know why the data is missing. */
1602 value->optimized_out = 1;
1608 gen_struct_elt_for_reference (struct expression *exp,
1609 struct agent_expr *ax, struct axs_value *value,
1610 struct type *type, char *fieldname)
1612 struct type *t = type;
1615 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1616 && TYPE_CODE (t) != TYPE_CODE_UNION)
1617 internal_error (__FILE__, __LINE__,
1618 _("non-aggregate type to gen_struct_elt_for_reference"));
1620 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1622 const char *t_field_name = TYPE_FIELD_NAME (t, i);
1624 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1626 if (field_is_static (&TYPE_FIELD (t, i)))
1628 gen_static_field (exp->gdbarch, ax, value, t, i);
1629 if (value->optimized_out)
1630 error (_("static field `%s' has been "
1631 "optimized out, cannot use"),
1635 if (TYPE_FIELD_PACKED (t, i))
1636 error (_("pointers to bitfield members not allowed"));
1638 /* FIXME we need a way to do "want_address" equivalent */
1640 error (_("Cannot reference non-static field \"%s\""), fieldname);
1644 /* FIXME add other scoped-reference cases here */
1646 /* Do a last-ditch lookup. */
1647 return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1650 /* C++: Return the member NAME of the namespace given by the type
1654 gen_namespace_elt (struct expression *exp,
1655 struct agent_expr *ax, struct axs_value *value,
1656 const struct type *curtype, char *name)
1658 int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1661 error (_("No symbol \"%s\" in namespace \"%s\"."),
1662 name, TYPE_TAG_NAME (curtype));
1667 /* A helper function used by value_namespace_elt and
1668 value_struct_elt_for_reference. It looks up NAME inside the
1669 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1670 is a class and NAME refers to a type in CURTYPE itself (as opposed
1671 to, say, some base class of CURTYPE). */
1674 gen_maybe_namespace_elt (struct expression *exp,
1675 struct agent_expr *ax, struct axs_value *value,
1676 const struct type *curtype, char *name)
1678 const char *namespace_name = TYPE_TAG_NAME (curtype);
1681 sym = cp_lookup_symbol_namespace (namespace_name, name,
1682 block_for_pc (ax->scope),
1688 gen_var_ref (exp->gdbarch, ax, value, sym);
1690 if (value->optimized_out)
1691 error (_("`%s' has been optimized out, cannot use"),
1692 SYMBOL_PRINT_NAME (sym));
1699 gen_aggregate_elt_ref (struct expression *exp,
1700 struct agent_expr *ax, struct axs_value *value,
1701 struct type *type, char *field,
1702 char *operator_name, char *operand_name)
1704 switch (TYPE_CODE (type))
1706 case TYPE_CODE_STRUCT:
1707 case TYPE_CODE_UNION:
1708 return gen_struct_elt_for_reference (exp, ax, value, type, field);
1710 case TYPE_CODE_NAMESPACE:
1711 return gen_namespace_elt (exp, ax, value, type, field);
1714 internal_error (__FILE__, __LINE__,
1715 _("non-aggregate type in gen_aggregate_elt_ref"));
1721 /* Generate code for GDB's magical `repeat' operator.
1722 LVALUE @ INT creates an array INT elements long, and whose elements
1723 have the same type as LVALUE, located in memory so that LVALUE is
1724 its first element. For example, argv[0]@argc gives you the array
1725 of command-line arguments.
1727 Unfortunately, because we have to know the types before we actually
1728 have a value for the expression, we can't implement this perfectly
1729 without changing the type system, having values that occupy two
1730 stack slots, doing weird things with sizeof, etc. So we require
1731 the right operand to be a constant expression. */
1733 gen_repeat (struct expression *exp, union exp_element **pc,
1734 struct agent_expr *ax, struct axs_value *value)
1736 struct axs_value value1;
1738 /* We don't want to turn this into an rvalue, so no conversions
1740 gen_expr (exp, pc, ax, &value1);
1741 if (value1.kind != axs_lvalue_memory)
1742 error (_("Left operand of `@' must be an object in memory."));
1744 /* Evaluate the length; it had better be a constant. */
1746 struct value *v = const_expr (pc);
1750 error (_("Right operand of `@' must be a "
1751 "constant, in agent expressions."));
1752 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1753 error (_("Right operand of `@' must be an integer."));
1754 length = value_as_long (v);
1756 error (_("Right operand of `@' must be positive."));
1758 /* The top of the stack is already the address of the object, so
1759 all we need to do is frob the type of the lvalue. */
1761 /* FIXME-type-allocation: need a way to free this type when we are
1764 = lookup_array_range_type (value1.type, 0, length - 1);
1766 value->kind = axs_lvalue_memory;
1767 value->type = array;
1773 /* Emit code for the `sizeof' operator.
1774 *PC should point at the start of the operand expression; we advance it
1775 to the first instruction after the operand. */
1777 gen_sizeof (struct expression *exp, union exp_element **pc,
1778 struct agent_expr *ax, struct axs_value *value,
1779 struct type *size_type)
1781 /* We don't care about the value of the operand expression; we only
1782 care about its type. However, in the current arrangement, the
1783 only way to find an expression's type is to generate code for it.
1784 So we generate code for the operand, and then throw it away,
1785 replacing it with code that simply pushes its size. */
1786 int start = ax->len;
1788 gen_expr (exp, pc, ax, value);
1790 /* Throw away the code we just generated. */
1793 ax_const_l (ax, TYPE_LENGTH (value->type));
1794 value->kind = axs_rvalue;
1795 value->type = size_type;
1799 /* Generating bytecode from GDB expressions: general recursive thingy */
1802 /* A gen_expr function written by a Gen-X'er guy.
1803 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1805 gen_expr (struct expression *exp, union exp_element **pc,
1806 struct agent_expr *ax, struct axs_value *value)
1808 /* Used to hold the descriptions of operand expressions. */
1809 struct axs_value value1, value2, value3;
1810 enum exp_opcode op = (*pc)[0].opcode, op2;
1811 int if1, go1, if2, go2, end;
1812 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
1814 /* If we're looking at a constant expression, just push its value. */
1816 struct value *v = maybe_const_expr (pc);
1820 ax_const_l (ax, value_as_long (v));
1821 value->kind = axs_rvalue;
1822 value->type = check_typedef (value_type (v));
1827 /* Otherwise, go ahead and generate code for it. */
1830 /* Binary arithmetic operators. */
1838 case BINOP_SUBSCRIPT:
1839 case BINOP_BITWISE_AND:
1840 case BINOP_BITWISE_IOR:
1841 case BINOP_BITWISE_XOR:
1843 case BINOP_NOTEQUAL:
1849 gen_expr (exp, pc, ax, &value1);
1850 gen_usual_unary (exp, ax, &value1);
1851 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1854 case BINOP_LOGICAL_AND:
1856 /* Generate the obvious sequence of tests and jumps. */
1857 gen_expr (exp, pc, ax, &value1);
1858 gen_usual_unary (exp, ax, &value1);
1859 if1 = ax_goto (ax, aop_if_goto);
1860 go1 = ax_goto (ax, aop_goto);
1861 ax_label (ax, if1, ax->len);
1862 gen_expr (exp, pc, ax, &value2);
1863 gen_usual_unary (exp, ax, &value2);
1864 if2 = ax_goto (ax, aop_if_goto);
1865 go2 = ax_goto (ax, aop_goto);
1866 ax_label (ax, if2, ax->len);
1868 end = ax_goto (ax, aop_goto);
1869 ax_label (ax, go1, ax->len);
1870 ax_label (ax, go2, ax->len);
1872 ax_label (ax, end, ax->len);
1873 value->kind = axs_rvalue;
1874 value->type = int_type;
1877 case BINOP_LOGICAL_OR:
1879 /* Generate the obvious sequence of tests and jumps. */
1880 gen_expr (exp, pc, ax, &value1);
1881 gen_usual_unary (exp, ax, &value1);
1882 if1 = ax_goto (ax, aop_if_goto);
1883 gen_expr (exp, pc, ax, &value2);
1884 gen_usual_unary (exp, ax, &value2);
1885 if2 = ax_goto (ax, aop_if_goto);
1887 end = ax_goto (ax, aop_goto);
1888 ax_label (ax, if1, ax->len);
1889 ax_label (ax, if2, ax->len);
1891 ax_label (ax, end, ax->len);
1892 value->kind = axs_rvalue;
1893 value->type = int_type;
1898 gen_expr (exp, pc, ax, &value1);
1899 gen_usual_unary (exp, ax, &value1);
1900 /* For (A ? B : C), it's easiest to generate subexpression
1901 bytecodes in order, but if_goto jumps on true, so we invert
1902 the sense of A. Then we can do B by dropping through, and
1904 gen_logical_not (ax, &value1, int_type);
1905 if1 = ax_goto (ax, aop_if_goto);
1906 gen_expr (exp, pc, ax, &value2);
1907 gen_usual_unary (exp, ax, &value2);
1908 end = ax_goto (ax, aop_goto);
1909 ax_label (ax, if1, ax->len);
1910 gen_expr (exp, pc, ax, &value3);
1911 gen_usual_unary (exp, ax, &value3);
1912 ax_label (ax, end, ax->len);
1913 /* This is arbitary - what if B and C are incompatible types? */
1914 value->type = value2.type;
1915 value->kind = value2.kind;
1920 if ((*pc)[0].opcode == OP_INTERNALVAR)
1922 char *name = internalvar_name ((*pc)[1].internalvar);
1923 struct trace_state_variable *tsv;
1926 gen_expr (exp, pc, ax, value);
1927 tsv = find_trace_state_variable (name);
1930 ax_tsv (ax, aop_setv, tsv->number);
1932 ax_tsv (ax, aop_tracev, tsv->number);
1935 error (_("$%s is not a trace state variable, "
1936 "may not assign to it"), name);
1939 error (_("May only assign to trace state variables"));
1942 case BINOP_ASSIGN_MODIFY:
1944 op2 = (*pc)[0].opcode;
1947 if ((*pc)[0].opcode == OP_INTERNALVAR)
1949 char *name = internalvar_name ((*pc)[1].internalvar);
1950 struct trace_state_variable *tsv;
1953 tsv = find_trace_state_variable (name);
1956 /* The tsv will be the left half of the binary operation. */
1957 ax_tsv (ax, aop_getv, tsv->number);
1959 ax_tsv (ax, aop_tracev, tsv->number);
1960 /* Trace state variables are always 64-bit integers. */
1961 value1.kind = axs_rvalue;
1962 value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1963 /* Now do right half of expression. */
1964 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1965 /* We have a result of the binary op, set the tsv. */
1966 ax_tsv (ax, aop_setv, tsv->number);
1968 ax_tsv (ax, aop_tracev, tsv->number);
1971 error (_("$%s is not a trace state variable, "
1972 "may not assign to it"), name);
1975 error (_("May only assign to trace state variables"));
1978 /* Note that we need to be a little subtle about generating code
1979 for comma. In C, we can do some optimizations here because
1980 we know the left operand is only being evaluated for effect.
1981 However, if the tracing kludge is in effect, then we always
1982 need to evaluate the left hand side fully, so that all the
1983 variables it mentions get traced. */
1986 gen_expr (exp, pc, ax, &value1);
1987 /* Don't just dispose of the left operand. We might be tracing,
1988 in which case we want to emit code to trace it if it's an
1990 gen_traced_pop (exp->gdbarch, ax, &value1);
1991 gen_expr (exp, pc, ax, value);
1992 /* It's the consumer's responsibility to trace the right operand. */
1995 case OP_LONG: /* some integer constant */
1997 struct type *type = (*pc)[1].type;
1998 LONGEST k = (*pc)[2].longconst;
2001 gen_int_literal (ax, value, k, type);
2006 gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
2008 if (value->optimized_out)
2009 error (_("`%s' has been optimized out, cannot use"),
2010 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
2017 const char *name = &(*pc)[2].string;
2020 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
2021 reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
2023 internal_error (__FILE__, __LINE__,
2024 _("Register $%s not available"), name);
2025 /* No support for tracing user registers yet. */
2026 if (reg >= gdbarch_num_regs (exp->gdbarch)
2027 + gdbarch_num_pseudo_regs (exp->gdbarch))
2028 error (_("'%s' is a user-register; "
2029 "GDB cannot yet trace user-register contents."),
2031 value->kind = axs_lvalue_register;
2033 value->type = register_type (exp->gdbarch, reg);
2037 case OP_INTERNALVAR:
2039 const char *name = internalvar_name ((*pc)[1].internalvar);
2040 struct trace_state_variable *tsv;
2043 tsv = find_trace_state_variable (name);
2046 ax_tsv (ax, aop_getv, tsv->number);
2048 ax_tsv (ax, aop_tracev, tsv->number);
2049 /* Trace state variables are always 64-bit integers. */
2050 value->kind = axs_rvalue;
2051 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2054 error (_("$%s is not a trace state variable; GDB agent "
2055 "expressions cannot use convenience variables."), name);
2059 /* Weirdo operator: see comments for gen_repeat for details. */
2061 /* Note that gen_repeat handles its own argument evaluation. */
2063 gen_repeat (exp, pc, ax, value);
2068 struct type *type = (*pc)[1].type;
2071 gen_expr (exp, pc, ax, value);
2072 gen_cast (ax, value, type);
2078 struct type *type = check_typedef ((*pc)[1].type);
2081 gen_expr (exp, pc, ax, value);
2083 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2084 already have the right value on the stack. For
2085 axs_lvalue_register, we must convert. */
2086 if (value->kind == axs_lvalue_register)
2087 require_rvalue (ax, value);
2090 value->kind = axs_lvalue_memory;
2096 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2097 gen_expr (exp, pc, ax, value);
2098 gen_usual_unary (exp, ax, value);
2103 /* -FOO is equivalent to 0 - FOO. */
2104 gen_int_literal (ax, &value1, 0,
2105 builtin_type (exp->gdbarch)->builtin_int);
2106 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2107 gen_expr (exp, pc, ax, &value2);
2108 gen_usual_unary (exp, ax, &value2);
2109 gen_usual_arithmetic (exp, ax, &value1, &value2);
2110 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2113 case UNOP_LOGICAL_NOT:
2115 gen_expr (exp, pc, ax, value);
2116 gen_usual_unary (exp, ax, value);
2117 gen_logical_not (ax, value, int_type);
2120 case UNOP_COMPLEMENT:
2122 gen_expr (exp, pc, ax, value);
2123 gen_usual_unary (exp, ax, value);
2124 gen_integral_promotions (exp, ax, value);
2125 gen_complement (ax, value);
2130 gen_expr (exp, pc, ax, value);
2131 gen_usual_unary (exp, ax, value);
2132 if (!pointer_type (value->type))
2133 error (_("Argument of unary `*' is not a pointer."));
2134 gen_deref (ax, value);
2139 gen_expr (exp, pc, ax, value);
2140 gen_address_of (ax, value);
2145 /* Notice that gen_sizeof handles its own operand, unlike most
2146 of the other unary operator functions. This is because we
2147 have to throw away the code we generate. */
2148 gen_sizeof (exp, pc, ax, value,
2149 builtin_type (exp->gdbarch)->builtin_int);
2152 case STRUCTOP_STRUCT:
2155 int length = (*pc)[1].longconst;
2156 char *name = &(*pc)[2].string;
2158 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2159 gen_expr (exp, pc, ax, value);
2160 if (op == STRUCTOP_STRUCT)
2161 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2162 else if (op == STRUCTOP_PTR)
2163 gen_struct_ref (exp, ax, value, name, "->",
2164 "pointer to a structure or union");
2166 /* If this `if' chain doesn't handle it, then the case list
2167 shouldn't mention it, and we shouldn't be here. */
2168 internal_error (__FILE__, __LINE__,
2169 _("gen_expr: unhandled struct case"));
2176 struct symbol *sym, *func;
2178 const struct language_defn *lang;
2180 b = block_for_pc (ax->scope);
2181 func = block_linkage_function (b);
2182 lang = language_def (SYMBOL_LANGUAGE (func));
2184 sym = lookup_language_this (lang, b);
2186 error (_("no `%s' found"), lang->la_name_of_this);
2188 gen_var_ref (exp->gdbarch, ax, value, sym);
2190 if (value->optimized_out)
2191 error (_("`%s' has been optimized out, cannot use"),
2192 SYMBOL_PRINT_NAME (sym));
2200 struct type *type = (*pc)[1].type;
2201 int length = longest_to_int ((*pc)[2].longconst);
2202 char *name = &(*pc)[3].string;
2205 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2208 error (_("There is no field named %s"), name);
2209 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2214 error (_("Attempt to use a type name as an expression."));
2217 error (_("Unsupported operator %s (%d) in expression."),
2218 op_string (op), op);
2222 /* This handles the middle-to-right-side of code generation for binary
2223 expressions, which is shared between regular binary operations and
2224 assign-modify (+= and friends) expressions. */
2227 gen_expr_binop_rest (struct expression *exp,
2228 enum exp_opcode op, union exp_element **pc,
2229 struct agent_expr *ax, struct axs_value *value,
2230 struct axs_value *value1, struct axs_value *value2)
2232 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2234 gen_expr (exp, pc, ax, value2);
2235 gen_usual_unary (exp, ax, value2);
2236 gen_usual_arithmetic (exp, ax, value1, value2);
2240 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2241 && pointer_type (value2->type))
2243 /* Swap the values and proceed normally. */
2244 ax_simple (ax, aop_swap);
2245 gen_ptradd (ax, value, value2, value1);
2247 else if (pointer_type (value1->type)
2248 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2249 gen_ptradd (ax, value, value1, value2);
2251 gen_binop (ax, value, value1, value2,
2252 aop_add, aop_add, 1, "addition");
2255 if (pointer_type (value1->type)
2256 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2257 gen_ptrsub (ax,value, value1, value2);
2258 else if (pointer_type (value1->type)
2259 && pointer_type (value2->type))
2260 /* FIXME --- result type should be ptrdiff_t */
2261 gen_ptrdiff (ax, value, value1, value2,
2262 builtin_type (exp->gdbarch)->builtin_long);
2264 gen_binop (ax, value, value1, value2,
2265 aop_sub, aop_sub, 1, "subtraction");
2268 gen_binop (ax, value, value1, value2,
2269 aop_mul, aop_mul, 1, "multiplication");
2272 gen_binop (ax, value, value1, value2,
2273 aop_div_signed, aop_div_unsigned, 1, "division");
2276 gen_binop (ax, value, value1, value2,
2277 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2280 gen_binop (ax, value, value1, value2,
2281 aop_lsh, aop_lsh, 1, "left shift");
2284 gen_binop (ax, value, value1, value2,
2285 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2287 case BINOP_SUBSCRIPT:
2291 if (binop_types_user_defined_p (op, value1->type, value2->type))
2293 error (_("cannot subscript requested type: "
2294 "cannot call user defined functions"));
2298 /* If the user attempts to subscript something that is not
2299 an array or pointer type (like a plain int variable for
2300 example), then report this as an error. */
2301 type = check_typedef (value1->type);
2302 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2303 && TYPE_CODE (type) != TYPE_CODE_PTR)
2305 if (TYPE_NAME (type))
2306 error (_("cannot subscript something of type `%s'"),
2309 error (_("cannot subscript requested type"));
2313 if (!is_integral_type (value2->type))
2314 error (_("Argument to arithmetic operation "
2315 "not a number or boolean."));
2317 gen_ptradd (ax, value, value1, value2);
2318 gen_deref (ax, value);
2321 case BINOP_BITWISE_AND:
2322 gen_binop (ax, value, value1, value2,
2323 aop_bit_and, aop_bit_and, 0, "bitwise and");
2326 case BINOP_BITWISE_IOR:
2327 gen_binop (ax, value, value1, value2,
2328 aop_bit_or, aop_bit_or, 0, "bitwise or");
2331 case BINOP_BITWISE_XOR:
2332 gen_binop (ax, value, value1, value2,
2333 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2337 gen_equal (ax, value, value1, value2, int_type);
2340 case BINOP_NOTEQUAL:
2341 gen_equal (ax, value, value1, value2, int_type);
2342 gen_logical_not (ax, value, int_type);
2346 gen_less (ax, value, value1, value2, int_type);
2350 ax_simple (ax, aop_swap);
2351 gen_less (ax, value, value1, value2, int_type);
2355 ax_simple (ax, aop_swap);
2356 gen_less (ax, value, value1, value2, int_type);
2357 gen_logical_not (ax, value, int_type);
2361 gen_less (ax, value, value1, value2, int_type);
2362 gen_logical_not (ax, value, int_type);
2366 /* We should only list operators in the outer case statement
2367 that we actually handle in the inner case statement. */
2368 internal_error (__FILE__, __LINE__,
2369 _("gen_expr: op case sets don't match"));
2374 /* Given a single variable and a scope, generate bytecodes to trace
2375 its value. This is for use in situations where we have only a
2376 variable's name, and no parsed expression; for instance, when the
2377 name comes from a list of local variables of a function. */
2380 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2383 struct cleanup *old_chain = 0;
2384 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2385 struct axs_value value;
2387 old_chain = make_cleanup_free_agent_expr (ax);
2390 gen_var_ref (gdbarch, ax, &value, var);
2392 /* If there is no actual variable to trace, flag it by returning
2393 an empty agent expression. */
2394 if (value.optimized_out)
2396 do_cleanups (old_chain);
2400 /* Make sure we record the final object, and get rid of it. */
2401 gen_traced_pop (gdbarch, ax, &value);
2403 /* Oh, and terminate. */
2404 ax_simple (ax, aop_end);
2406 /* We have successfully built the agent expr, so cancel the cleanup
2407 request. If we add more cleanups that we always want done, this
2408 will have to get more complicated. */
2409 discard_cleanups (old_chain);
2413 /* Generating bytecode from GDB expressions: driver */
2415 /* Given a GDB expression EXPR, return bytecode to trace its value.
2416 The result will use the `trace' and `trace_quick' bytecodes to
2417 record the value of all memory touched by the expression. The
2418 caller can then use the ax_reqs function to discover which
2419 registers it relies upon. */
2421 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
2423 struct cleanup *old_chain = 0;
2424 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2425 union exp_element *pc;
2426 struct axs_value value;
2428 old_chain = make_cleanup_free_agent_expr (ax);
2432 value.optimized_out = 0;
2433 gen_expr (expr, &pc, ax, &value);
2435 /* Make sure we record the final object, and get rid of it. */
2436 gen_traced_pop (expr->gdbarch, 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);
2448 /* Given a GDB expression EXPR, return a bytecode sequence that will
2449 evaluate and return a result. The bytecodes will do a direct
2450 evaluation, using the current data on the target, rather than
2451 recording blocks of memory and registers for later use, as
2452 gen_trace_for_expr does. The generated bytecode sequence leaves
2453 the result of expression evaluation on the top of the stack. */
2456 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2458 struct cleanup *old_chain = 0;
2459 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2460 union exp_element *pc;
2461 struct axs_value value;
2463 old_chain = make_cleanup_free_agent_expr (ax);
2467 value.optimized_out = 0;
2468 gen_expr (expr, &pc, ax, &value);
2470 require_rvalue (ax, &value);
2472 /* Oh, and terminate. */
2473 ax_simple (ax, aop_end);
2475 /* We have successfully built the agent expr, so cancel the cleanup
2476 request. If we add more cleanups that we always want done, this
2477 will have to get more complicated. */
2478 discard_cleanups (old_chain);
2483 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch)
2485 struct cleanup *old_chain = 0;
2486 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2487 struct axs_value value;
2489 old_chain = make_cleanup_free_agent_expr (ax);
2493 gdbarch_gen_return_address (gdbarch, ax, &value, scope);
2495 /* Make sure we record the final object, and get rid of it. */
2496 gen_traced_pop (gdbarch, ax, &value);
2498 /* Oh, and terminate. */
2499 ax_simple (ax, aop_end);
2501 /* We have successfully built the agent expr, so cancel the cleanup
2502 request. If we add more cleanups that we always want done, this
2503 will have to get more complicated. */
2504 discard_cleanups (old_chain);
2509 agent_command (char *exp, int from_tty)
2511 struct cleanup *old_chain = 0;
2512 struct expression *expr;
2513 struct agent_expr *agent;
2514 struct frame_info *fi = get_current_frame (); /* need current scope */
2516 /* We don't deal with overlay debugging at the moment. We need to
2517 think more carefully about this. If you copy this code into
2518 another command, change the error message; the user shouldn't
2519 have to know anything about agent expressions. */
2520 if (overlay_debugging)
2521 error (_("GDB can't do agent expression translation with overlays."));
2524 error_no_arg (_("expression to translate"));
2526 trace_string_kludge = 0;
2528 exp = decode_agent_options (exp);
2530 /* Recognize the return address collection directive specially. Note
2531 that it is not really an expression of any sort. */
2532 if (strcmp (exp, "$_ret") == 0)
2534 agent = gen_trace_for_return_address (get_frame_pc (fi),
2535 get_current_arch ());
2536 old_chain = make_cleanup_free_agent_expr (agent);
2540 expr = parse_expression (exp);
2541 old_chain = make_cleanup (free_current_contents, &expr);
2542 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
2543 make_cleanup_free_agent_expr (agent);
2547 ax_print (gdb_stdout, agent);
2549 /* It would be nice to call ax_reqs here to gather some general info
2550 about the expression, and then print out the result. */
2552 do_cleanups (old_chain);
2556 /* Parse the given expression, compile it into an agent expression
2557 that does direct evaluation, and display the resulting
2561 agent_eval_command (char *exp, int from_tty)
2563 struct cleanup *old_chain = 0;
2564 struct expression *expr;
2565 struct agent_expr *agent;
2566 struct frame_info *fi = get_current_frame (); /* need current scope */
2568 /* We don't deal with overlay debugging at the moment. We need to
2569 think more carefully about this. If you copy this code into
2570 another command, change the error message; the user shouldn't
2571 have to know anything about agent expressions. */
2572 if (overlay_debugging)
2573 error (_("GDB can't do agent expression translation with overlays."));
2576 error_no_arg (_("expression to translate"));
2578 expr = parse_expression (exp);
2579 old_chain = make_cleanup (free_current_contents, &expr);
2580 agent = gen_eval_for_expr (get_frame_pc (fi), expr);
2581 make_cleanup_free_agent_expr (agent);
2583 ax_print (gdb_stdout, agent);
2585 /* It would be nice to call ax_reqs here to gather some general info
2586 about the expression, and then print out the result. */
2588 do_cleanups (old_chain);
2593 /* Initialization code. */
2595 void _initialize_ax_gdb (void);
2597 _initialize_ax_gdb (void)
2599 add_cmd ("agent", class_maintenance, agent_command,
2600 _("Translate an expression into "
2601 "remote agent bytecode for tracing."),
2604 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2605 _("Translate an expression into remote "
2606 "agent bytecode for evaluation."),