1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "expression.h"
33 #include "gdb_string.h"
36 #include "user-regs.h"
38 #include "dictionary.h"
39 #include "breakpoint.h"
40 #include "tracepoint.h"
41 #include "cp-support.h"
42 #include "arch-utils.h"
43 #include "cli/cli-utils.h"
51 /* To make sense of this file, you should read doc/agentexpr.texi.
52 Then look at the types and enums in ax-gdb.h. For the code itself,
53 look at gen_expr, towards the bottom; that's the main function that
54 looks at the GDB expressions and calls everything else to generate
57 I'm beginning to wonder whether it wouldn't be nicer to internally
58 generate trees, with types, and then spit out the bytecode in
59 linear form afterwards; we could generate fewer `swap', `ext', and
60 `zero_ext' bytecodes that way; it would make good constant folding
61 easier, too. But at the moment, I think we should be willing to
62 pay for the simplicity of this code with less-than-optimal bytecode
65 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
69 /* Prototypes for local functions. */
71 /* There's a standard order to the arguments of these functions:
72 union exp_element ** --- pointer into expression
73 struct agent_expr * --- agent expression buffer to generate code into
74 struct axs_value * --- describes value left on top of stack */
76 static struct value *const_var_ref (struct symbol *var);
77 static struct value *const_expr (union exp_element **pc);
78 static struct value *maybe_const_expr (union exp_element **pc);
80 static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
83 static void gen_sign_extend (struct agent_expr *, struct type *);
84 static void gen_extend (struct agent_expr *, struct type *);
85 static void gen_fetch (struct agent_expr *, struct type *);
86 static void gen_left_shift (struct agent_expr *, int);
89 static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
90 static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
91 static void gen_offset (struct agent_expr *ax, int offset);
92 static void gen_sym_offset (struct agent_expr *, struct symbol *);
93 static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
94 struct axs_value *value, struct symbol *var);
97 static void gen_int_literal (struct agent_expr *ax,
98 struct axs_value *value,
99 LONGEST k, struct type *type);
101 static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
102 struct axs_value *value);
103 static int type_wider_than (struct type *type1, struct type *type2);
104 static struct type *max_type (struct type *type1, struct type *type2);
105 static void gen_conversion (struct agent_expr *ax,
106 struct type *from, struct type *to);
107 static int is_nontrivial_conversion (struct type *from, struct type *to);
108 static void gen_usual_arithmetic (struct expression *exp,
109 struct agent_expr *ax,
110 struct axs_value *value1,
111 struct axs_value *value2);
112 static void gen_integral_promotions (struct expression *exp,
113 struct agent_expr *ax,
114 struct axs_value *value);
115 static void gen_cast (struct agent_expr *ax,
116 struct axs_value *value, struct type *type);
117 static void gen_scale (struct agent_expr *ax,
118 enum agent_op op, struct type *type);
119 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
120 struct axs_value *value1, struct axs_value *value2);
121 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
122 struct axs_value *value1, struct axs_value *value2);
123 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
124 struct axs_value *value1, struct axs_value *value2,
125 struct type *result_type);
126 static void gen_binop (struct agent_expr *ax,
127 struct axs_value *value,
128 struct axs_value *value1,
129 struct axs_value *value2,
131 enum agent_op op_unsigned, int may_carry, char *name);
132 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
133 struct type *result_type);
134 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
135 static void gen_deref (struct agent_expr *, struct axs_value *);
136 static void gen_address_of (struct agent_expr *, struct axs_value *);
137 static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
138 struct axs_value *value,
139 struct type *type, int start, int end);
140 static void gen_primitive_field (struct expression *exp,
141 struct agent_expr *ax,
142 struct axs_value *value,
143 int offset, int fieldno, struct type *type);
144 static int gen_struct_ref_recursive (struct expression *exp,
145 struct agent_expr *ax,
146 struct axs_value *value,
147 char *field, int offset,
149 static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
150 struct axs_value *value,
152 char *operator_name, char *operand_name);
153 static void gen_static_field (struct gdbarch *gdbarch,
154 struct agent_expr *ax, struct axs_value *value,
155 struct type *type, int fieldno);
156 static void gen_repeat (struct expression *exp, union exp_element **pc,
157 struct agent_expr *ax, struct axs_value *value);
158 static void gen_sizeof (struct expression *exp, union exp_element **pc,
159 struct agent_expr *ax, struct axs_value *value,
160 struct type *size_type);
161 static void gen_expr_binop_rest (struct expression *exp,
162 enum exp_opcode op, union exp_element **pc,
163 struct agent_expr *ax,
164 struct axs_value *value,
165 struct axs_value *value1,
166 struct axs_value *value2);
168 static void agent_command (char *exp, int from_tty);
171 /* Detecting constant expressions. */
173 /* If the variable reference at *PC is a constant, return its value.
174 Otherwise, return zero.
176 Hey, Wally! How can a variable reference be a constant?
178 Well, Beav, this function really handles the OP_VAR_VALUE operator,
179 not specifically variable references. GDB uses OP_VAR_VALUE to
180 refer to any kind of symbolic reference: function names, enum
181 elements, and goto labels are all handled through the OP_VAR_VALUE
182 operator, even though they're constants. It makes sense given the
185 Gee, Wally, don'cha wonder sometimes if data representations that
186 subvert commonly accepted definitions of terms in favor of heavily
187 context-specific interpretations are really just a tool of the
188 programming hegemony to preserve their power and exclude the
191 static struct value *
192 const_var_ref (struct symbol *var)
194 struct type *type = SYMBOL_TYPE (var);
196 switch (SYMBOL_CLASS (var))
199 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
202 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
210 /* If the expression starting at *PC has a constant value, return it.
211 Otherwise, return zero. If we return a value, then *PC will be
212 advanced to the end of it. If we return zero, *PC could be
214 static struct value *
215 const_expr (union exp_element **pc)
217 enum exp_opcode op = (*pc)->opcode;
224 struct type *type = (*pc)[1].type;
225 LONGEST k = (*pc)[2].longconst;
228 return value_from_longest (type, k);
233 struct value *v = const_var_ref ((*pc)[2].symbol);
239 /* We could add more operators in here. */
243 v1 = const_expr (pc);
245 return value_neg (v1);
255 /* Like const_expr, but guarantee also that *PC is undisturbed if the
256 expression is not constant. */
257 static struct value *
258 maybe_const_expr (union exp_element **pc)
260 union exp_element *tentative_pc = *pc;
261 struct value *v = const_expr (&tentative_pc);
263 /* If we got a value, then update the real PC. */
271 /* Generating bytecode from GDB expressions: general assumptions */
273 /* Here are a few general assumptions made throughout the code; if you
274 want to make a change that contradicts one of these, then you'd
275 better scan things pretty thoroughly.
277 - We assume that all values occupy one stack element. For example,
278 sometimes we'll swap to get at the left argument to a binary
279 operator. If we decide that void values should occupy no stack
280 elements, or that synthetic arrays (whose size is determined at
281 run time, created by the `@' operator) should occupy two stack
282 elements (address and length), then this will cause trouble.
284 - We assume the stack elements are infinitely wide, and that we
285 don't have to worry what happens if the user requests an
286 operation that is wider than the actual interpreter's stack.
287 That is, it's up to the interpreter to handle directly all the
288 integer widths the user has access to. (Woe betide the language
291 - We don't support side effects. Thus, we don't have to worry about
292 GCC's generalized lvalues, function calls, etc.
294 - We don't support floating point. Many places where we switch on
295 some type don't bother to include cases for floating point; there
296 may be even more subtle ways this assumption exists. For
297 example, the arguments to % must be integers.
299 - We assume all subexpressions have a static, unchanging type. If
300 we tried to support convenience variables, this would be a
303 - All values on the stack should always be fully zero- or
306 (I wasn't sure whether to choose this or its opposite --- that
307 only addresses are assumed extended --- but it turns out that
308 neither convention completely eliminates spurious extend
309 operations (if everything is always extended, then you have to
310 extend after add, because it could overflow; if nothing is
311 extended, then you end up producing extends whenever you change
312 sizes), and this is simpler.) */
315 /* Scan for all static fields in the given class, including any base
316 classes, and generate tracing bytecodes for each. */
319 gen_trace_static_fields (struct gdbarch *gdbarch,
320 struct agent_expr *ax,
323 int i, nbases = TYPE_N_BASECLASSES (type);
324 struct axs_value value;
326 CHECK_TYPEDEF (type);
328 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
330 if (field_is_static (&TYPE_FIELD (type, i)))
332 gen_static_field (gdbarch, ax, &value, type, i);
333 if (value.optimized_out)
337 case axs_lvalue_memory:
339 /* Initialize the TYPE_LENGTH if it is a typedef. */
340 check_typedef (value.type);
341 ax_const_l (ax, TYPE_LENGTH (value.type));
342 ax_simple (ax, aop_trace);
346 case axs_lvalue_register:
347 /* We don't actually need the register's value to be pushed,
348 just note that we need it to be collected. */
349 ax_reg_mask (ax, value.u.reg);
357 /* Now scan through base classes recursively. */
358 for (i = 0; i < nbases; i++)
360 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
362 gen_trace_static_fields (gdbarch, ax, basetype);
366 /* Trace the lvalue on the stack, if it needs it. In either case, pop
367 the value. Useful on the left side of a comma, and at the end of
368 an expression being used for tracing. */
370 gen_traced_pop (struct gdbarch *gdbarch,
371 struct agent_expr *ax, struct axs_value *value)
373 int string_trace = 0;
375 && TYPE_CODE (value->type) == TYPE_CODE_PTR
376 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
386 ax_const_l (ax, ax->trace_string);
387 ax_simple (ax, aop_tracenz);
390 /* We don't trace rvalues, just the lvalues necessary to
391 produce them. So just dispose of this value. */
392 ax_simple (ax, aop_pop);
395 case axs_lvalue_memory:
398 ax_simple (ax, aop_dup);
400 /* Initialize the TYPE_LENGTH if it is a typedef. */
401 check_typedef (value->type);
403 /* There's no point in trying to use a trace_quick bytecode
404 here, since "trace_quick SIZE pop" is three bytes, whereas
405 "const8 SIZE trace" is also three bytes, does the same
406 thing, and the simplest code which generates that will also
407 work correctly for objects with large sizes. */
408 ax_const_l (ax, TYPE_LENGTH (value->type));
409 ax_simple (ax, aop_trace);
413 ax_simple (ax, aop_ref32);
414 ax_const_l (ax, ax->trace_string);
415 ax_simple (ax, aop_tracenz);
420 case axs_lvalue_register:
421 /* We don't actually need the register's value to be on the
422 stack, and the target will get heartburn if the register is
423 larger than will fit in a stack, so just mark it for
424 collection and be done with it. */
425 ax_reg_mask (ax, value->u.reg);
427 /* But if the register points to a string, assume the value
428 will fit on the stack and push it anyway. */
431 ax_reg (ax, value->u.reg);
432 ax_const_l (ax, ax->trace_string);
433 ax_simple (ax, aop_tracenz);
438 /* If we're not tracing, just pop the value. */
439 ax_simple (ax, aop_pop);
441 /* To trace C++ classes with static fields stored elsewhere. */
443 && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
444 || TYPE_CODE (value->type) == TYPE_CODE_UNION))
445 gen_trace_static_fields (gdbarch, ax, value->type);
450 /* Generating bytecode from GDB expressions: helper functions */
452 /* Assume that the lower bits of the top of the stack is a value of
453 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
455 gen_sign_extend (struct agent_expr *ax, struct type *type)
457 /* Do we need to sign-extend this? */
458 if (!TYPE_UNSIGNED (type))
459 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
463 /* Assume the lower bits of the top of the stack hold a value of type
464 TYPE, and the upper bits are garbage. Sign-extend or truncate as
467 gen_extend (struct agent_expr *ax, struct type *type)
469 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
472 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
476 /* Assume that the top of the stack contains a value of type "pointer
477 to TYPE"; generate code to fetch its value. Note that TYPE is the
478 target type, not the pointer type. */
480 gen_fetch (struct agent_expr *ax, struct type *type)
484 /* Record the area of memory we're about to fetch. */
485 ax_trace_quick (ax, TYPE_LENGTH (type));
488 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
489 type = TYPE_TARGET_TYPE (type);
491 switch (TYPE_CODE (type))
499 /* It's a scalar value, so we know how to dereference it. How
500 many bytes long is it? */
501 switch (TYPE_LENGTH (type))
503 case 8 / TARGET_CHAR_BIT:
504 ax_simple (ax, aop_ref8);
506 case 16 / TARGET_CHAR_BIT:
507 ax_simple (ax, aop_ref16);
509 case 32 / TARGET_CHAR_BIT:
510 ax_simple (ax, aop_ref32);
512 case 64 / TARGET_CHAR_BIT:
513 ax_simple (ax, aop_ref64);
516 /* Either our caller shouldn't have asked us to dereference
517 that pointer (other code's fault), or we're not
518 implementing something we should be (this code's fault).
519 In any case, it's a bug the user shouldn't see. */
521 internal_error (__FILE__, __LINE__,
522 _("gen_fetch: strange size"));
525 gen_sign_extend (ax, type);
529 /* Our caller requested us to dereference a pointer from an unsupported
530 type. Error out and give callers a chance to handle the failure
532 error (_("gen_fetch: Unsupported type code `%s'."),
538 /* Generate code to left shift the top of the stack by DISTANCE bits, or
539 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
540 unsigned (logical) right shifts. */
542 gen_left_shift (struct agent_expr *ax, int distance)
546 ax_const_l (ax, distance);
547 ax_simple (ax, aop_lsh);
549 else if (distance < 0)
551 ax_const_l (ax, -distance);
552 ax_simple (ax, aop_rsh_unsigned);
558 /* Generating bytecode from GDB expressions: symbol references */
560 /* Generate code to push the base address of the argument portion of
561 the top stack frame. */
563 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
566 LONGEST frame_offset;
568 gdbarch_virtual_frame_pointer (gdbarch,
569 ax->scope, &frame_reg, &frame_offset);
570 ax_reg (ax, frame_reg);
571 gen_offset (ax, frame_offset);
575 /* Generate code to push the base address of the locals portion of the
578 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
581 LONGEST frame_offset;
583 gdbarch_virtual_frame_pointer (gdbarch,
584 ax->scope, &frame_reg, &frame_offset);
585 ax_reg (ax, frame_reg);
586 gen_offset (ax, frame_offset);
590 /* Generate code to add OFFSET to the top of the stack. Try to
591 generate short and readable code. We use this for getting to
592 variables on the stack, and structure members. If we were
593 programming in ML, it would be clearer why these are the same
596 gen_offset (struct agent_expr *ax, int offset)
598 /* It would suffice to simply push the offset and add it, but this
599 makes it easier to read positive and negative offsets in the
603 ax_const_l (ax, offset);
604 ax_simple (ax, aop_add);
608 ax_const_l (ax, -offset);
609 ax_simple (ax, aop_sub);
614 /* In many cases, a symbol's value is the offset from some other
615 address (stack frame, base register, etc.) Generate code to add
616 VAR's value to the top of the stack. */
618 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
620 gen_offset (ax, SYMBOL_VALUE (var));
624 /* Generate code for a variable reference to AX. The variable is the
625 symbol VAR. Set VALUE to describe the result. */
628 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
629 struct axs_value *value, struct symbol *var)
631 /* Dereference any typedefs. */
632 value->type = check_typedef (SYMBOL_TYPE (var));
633 value->optimized_out = 0;
635 if (SYMBOL_COMPUTED_OPS (var) != NULL)
637 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
641 /* I'm imitating the code in read_var_value. */
642 switch (SYMBOL_CLASS (var))
644 case LOC_CONST: /* A constant, like an enum value. */
645 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
646 value->kind = axs_rvalue;
649 case LOC_LABEL: /* A goto label, being used as a value. */
650 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
651 value->kind = axs_rvalue;
654 case LOC_CONST_BYTES:
655 internal_error (__FILE__, __LINE__,
656 _("gen_var_ref: LOC_CONST_BYTES "
657 "symbols are not supported"));
659 /* Variable at a fixed location in memory. Easy. */
661 /* Push the address of the variable. */
662 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
663 value->kind = axs_lvalue_memory;
666 case LOC_ARG: /* var lives in argument area of frame */
667 gen_frame_args_address (gdbarch, ax);
668 gen_sym_offset (ax, var);
669 value->kind = axs_lvalue_memory;
672 case LOC_REF_ARG: /* As above, but the frame slot really
673 holds the address of the variable. */
674 gen_frame_args_address (gdbarch, ax);
675 gen_sym_offset (ax, var);
676 /* Don't assume any particular pointer size. */
677 gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
678 value->kind = axs_lvalue_memory;
681 case LOC_LOCAL: /* var lives in locals area of frame */
682 gen_frame_locals_address (gdbarch, ax);
683 gen_sym_offset (ax, var);
684 value->kind = axs_lvalue_memory;
688 error (_("Cannot compute value of typedef `%s'."),
689 SYMBOL_PRINT_NAME (var));
693 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
694 value->kind = axs_rvalue;
698 /* Don't generate any code at all; in the process of treating
699 this as an lvalue or rvalue, the caller will generate the
701 value->kind = axs_lvalue_register;
702 value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
705 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
706 register, not on the stack. Simpler than LOC_REGISTER
707 because it's just like any other case where the thing
708 has a real address. */
709 case LOC_REGPARM_ADDR:
710 ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
711 value->kind = axs_lvalue_memory;
716 struct minimal_symbol *msym
717 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
720 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
722 /* Push the address of the variable. */
723 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
724 value->kind = axs_lvalue_memory;
729 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
731 case LOC_OPTIMIZED_OUT:
732 /* Flag this, but don't say anything; leave it up to callers to
734 value->optimized_out = 1;
738 error (_("Cannot find value of botched symbol `%s'."),
739 SYMBOL_PRINT_NAME (var));
746 /* Generating bytecode from GDB expressions: literals */
749 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
753 value->kind = axs_rvalue;
754 value->type = check_typedef (type);
759 /* Generating bytecode from GDB expressions: unary conversions, casts */
761 /* Take what's on the top of the stack (as described by VALUE), and
762 try to make an rvalue out of it. Signal an error if we can't do
765 require_rvalue (struct agent_expr *ax, struct axs_value *value)
767 /* Only deal with scalars, structs and such may be too large
768 to fit in a stack entry. */
769 value->type = check_typedef (value->type);
770 if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
771 || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
772 || TYPE_CODE (value->type) == TYPE_CODE_UNION
773 || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
774 error (_("Value not scalar: cannot be an rvalue."));
779 /* It's already an rvalue. */
782 case axs_lvalue_memory:
783 /* The top of stack is the address of the object. Dereference. */
784 gen_fetch (ax, value->type);
787 case axs_lvalue_register:
788 /* There's nothing on the stack, but value->u.reg is the
789 register number containing the value.
791 When we add floating-point support, this is going to have to
792 change. What about SPARC register pairs, for example? */
793 ax_reg (ax, value->u.reg);
794 gen_extend (ax, value->type);
798 value->kind = axs_rvalue;
802 /* Assume the top of the stack is described by VALUE, and perform the
803 usual unary conversions. This is motivated by ANSI 6.2.2, but of
804 course GDB expressions are not ANSI; they're the mishmash union of
805 a bunch of languages. Rah.
807 NOTE! This function promises to produce an rvalue only when the
808 incoming value is of an appropriate type. In other words, the
809 consumer of the value this function produces may assume the value
810 is an rvalue only after checking its type.
812 The immediate issue is that if the user tries to use a structure or
813 union as an operand of, say, the `+' operator, we don't want to try
814 to convert that structure to an rvalue; require_rvalue will bomb on
815 structs and unions. Rather, we want to simply pass the struct
816 lvalue through unchanged, and let `+' raise an error. */
819 gen_usual_unary (struct expression *exp, struct agent_expr *ax,
820 struct axs_value *value)
822 /* We don't have to generate any code for the usual integral
823 conversions, since values are always represented as full-width on
824 the stack. Should we tweak the type? */
826 /* Some types require special handling. */
827 switch (TYPE_CODE (value->type))
829 /* Functions get converted to a pointer to the function. */
831 value->type = lookup_pointer_type (value->type);
832 value->kind = axs_rvalue; /* Should always be true, but just in case. */
835 /* Arrays get converted to a pointer to their first element, and
836 are no longer an lvalue. */
837 case TYPE_CODE_ARRAY:
839 struct type *elements = TYPE_TARGET_TYPE (value->type);
841 value->type = lookup_pointer_type (elements);
842 value->kind = axs_rvalue;
843 /* We don't need to generate any code; the address of the array
844 is also the address of its first element. */
848 /* Don't try to convert structures and unions to rvalues. Let the
849 consumer signal an error. */
850 case TYPE_CODE_STRUCT:
851 case TYPE_CODE_UNION:
855 /* If the value is an lvalue, dereference it. */
856 require_rvalue (ax, value);
860 /* Return non-zero iff the type TYPE1 is considered "wider" than the
861 type TYPE2, according to the rules described in gen_usual_arithmetic. */
863 type_wider_than (struct type *type1, struct type *type2)
865 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
866 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
867 && TYPE_UNSIGNED (type1)
868 && !TYPE_UNSIGNED (type2)));
872 /* Return the "wider" of the two types TYPE1 and TYPE2. */
874 max_type (struct type *type1, struct type *type2)
876 return type_wider_than (type1, type2) ? type1 : type2;
880 /* Generate code to convert a scalar value of type FROM to type TO. */
882 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
884 /* Perhaps there is a more graceful way to state these rules. */
886 /* If we're converting to a narrower type, then we need to clear out
888 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
889 gen_extend (ax, from);
891 /* If the two values have equal width, but different signednesses,
892 then we need to extend. */
893 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
895 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
899 /* If we're converting to a wider type, and becoming unsigned, then
900 we need to zero out any possible sign bits. */
901 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
903 if (TYPE_UNSIGNED (to))
909 /* Return non-zero iff the type FROM will require any bytecodes to be
910 emitted to be converted to the type TO. */
912 is_nontrivial_conversion (struct type *from, struct type *to)
914 struct agent_expr *ax = new_agent_expr (NULL, 0);
917 /* Actually generate the code, and see if anything came out. At the
918 moment, it would be trivial to replicate the code in
919 gen_conversion here, but in the future, when we're supporting
920 floating point and the like, it may not be. Doing things this
921 way allows this function to be independent of the logic in
923 gen_conversion (ax, from, to);
924 nontrivial = ax->len > 0;
925 free_agent_expr (ax);
930 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
931 6.2.1.5) for the two operands of an arithmetic operator. This
932 effectively finds a "least upper bound" type for the two arguments,
933 and promotes each argument to that type. *VALUE1 and *VALUE2
934 describe the values as they are passed in, and as they are left. */
936 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
937 struct axs_value *value1, struct axs_value *value2)
939 /* Do the usual binary conversions. */
940 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
941 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
943 /* The ANSI integral promotions seem to work this way: Order the
944 integer types by size, and then by signedness: an n-bit
945 unsigned type is considered "wider" than an n-bit signed
946 type. Promote to the "wider" of the two types, and always
947 promote at least to int. */
948 struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
949 max_type (value1->type, value2->type));
951 /* Deal with value2, on the top of the stack. */
952 gen_conversion (ax, value2->type, target);
954 /* Deal with value1, not on the top of the stack. Don't
955 generate the `swap' instructions if we're not actually going
957 if (is_nontrivial_conversion (value1->type, target))
959 ax_simple (ax, aop_swap);
960 gen_conversion (ax, value1->type, target);
961 ax_simple (ax, aop_swap);
964 value1->type = value2->type = check_typedef (target);
969 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
970 the value on the top of the stack, as described by VALUE. Assume
971 the value has integral type. */
973 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
974 struct axs_value *value)
976 const struct builtin_type *builtin = builtin_type (exp->gdbarch);
978 if (!type_wider_than (value->type, builtin->builtin_int))
980 gen_conversion (ax, value->type, builtin->builtin_int);
981 value->type = builtin->builtin_int;
983 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
985 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
986 value->type = builtin->builtin_unsigned_int;
991 /* Generate code for a cast to TYPE. */
993 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
995 /* GCC does allow casts to yield lvalues, so this should be fixed
996 before merging these changes into the trunk. */
997 require_rvalue (ax, value);
998 /* Dereference typedefs. */
999 type = check_typedef (type);
1001 switch (TYPE_CODE (type))
1005 /* It's implementation-defined, and I'll bet this is what GCC
1009 case TYPE_CODE_ARRAY:
1010 case TYPE_CODE_STRUCT:
1011 case TYPE_CODE_UNION:
1012 case TYPE_CODE_FUNC:
1013 error (_("Invalid type cast: intended type must be scalar."));
1015 case TYPE_CODE_ENUM:
1016 case TYPE_CODE_BOOL:
1017 /* We don't have to worry about the size of the value, because
1018 all our integral values are fully sign-extended, and when
1019 casting pointers we can do anything we like. Is there any
1020 way for us to know what GCC actually does with a cast like
1025 gen_conversion (ax, value->type, type);
1028 case TYPE_CODE_VOID:
1029 /* We could pop the value, and rely on everyone else to check
1030 the type and notice that this value doesn't occupy a stack
1031 slot. But for now, leave the value on the stack, and
1032 preserve the "value == stack element" assumption. */
1036 error (_("Casts to requested type are not yet implemented."));
1044 /* Generating bytecode from GDB expressions: arithmetic */
1046 /* Scale the integer on the top of the stack by the size of the target
1047 of the pointer type TYPE. */
1049 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1051 struct type *element = TYPE_TARGET_TYPE (type);
1053 if (TYPE_LENGTH (element) != 1)
1055 ax_const_l (ax, TYPE_LENGTH (element));
1061 /* Generate code for pointer arithmetic PTR + INT. */
1063 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1064 struct axs_value *value1, struct axs_value *value2)
1066 gdb_assert (pointer_type (value1->type));
1067 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1069 gen_scale (ax, aop_mul, value1->type);
1070 ax_simple (ax, aop_add);
1071 gen_extend (ax, value1->type); /* Catch overflow. */
1072 value->type = value1->type;
1073 value->kind = axs_rvalue;
1077 /* Generate code for pointer arithmetic PTR - INT. */
1079 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1080 struct axs_value *value1, struct axs_value *value2)
1082 gdb_assert (pointer_type (value1->type));
1083 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1085 gen_scale (ax, aop_mul, value1->type);
1086 ax_simple (ax, aop_sub);
1087 gen_extend (ax, value1->type); /* Catch overflow. */
1088 value->type = value1->type;
1089 value->kind = axs_rvalue;
1093 /* Generate code for pointer arithmetic PTR - PTR. */
1095 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1096 struct axs_value *value1, struct axs_value *value2,
1097 struct type *result_type)
1099 gdb_assert (pointer_type (value1->type));
1100 gdb_assert (pointer_type (value2->type));
1102 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1103 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1105 First argument of `-' is a pointer, but second argument is neither\n\
1106 an integer nor a pointer of the same type."));
1108 ax_simple (ax, aop_sub);
1109 gen_scale (ax, aop_div_unsigned, value1->type);
1110 value->type = result_type;
1111 value->kind = axs_rvalue;
1115 gen_equal (struct agent_expr *ax, struct axs_value *value,
1116 struct axs_value *value1, struct axs_value *value2,
1117 struct type *result_type)
1119 if (pointer_type (value1->type) || pointer_type (value2->type))
1120 ax_simple (ax, aop_equal);
1122 gen_binop (ax, value, value1, value2,
1123 aop_equal, aop_equal, 0, "equal");
1124 value->type = result_type;
1125 value->kind = axs_rvalue;
1129 gen_less (struct agent_expr *ax, struct axs_value *value,
1130 struct axs_value *value1, struct axs_value *value2,
1131 struct type *result_type)
1133 if (pointer_type (value1->type) || pointer_type (value2->type))
1134 ax_simple (ax, aop_less_unsigned);
1136 gen_binop (ax, value, value1, value2,
1137 aop_less_signed, aop_less_unsigned, 0, "less than");
1138 value->type = result_type;
1139 value->kind = axs_rvalue;
1142 /* Generate code for a binary operator that doesn't do pointer magic.
1143 We set VALUE to describe the result value; we assume VALUE1 and
1144 VALUE2 describe the two operands, and that they've undergone the
1145 usual binary conversions. MAY_CARRY should be non-zero iff the
1146 result needs to be extended. NAME is the English name of the
1147 operator, used in error messages */
1149 gen_binop (struct agent_expr *ax, struct axs_value *value,
1150 struct axs_value *value1, struct axs_value *value2,
1151 enum agent_op op, enum agent_op op_unsigned,
1152 int may_carry, char *name)
1154 /* We only handle INT op INT. */
1155 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1156 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1157 error (_("Invalid combination of types in %s."), name);
1160 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1162 gen_extend (ax, value1->type); /* catch overflow */
1163 value->type = value1->type;
1164 value->kind = axs_rvalue;
1169 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1170 struct type *result_type)
1172 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1173 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1174 error (_("Invalid type of operand to `!'."));
1176 ax_simple (ax, aop_log_not);
1177 value->type = result_type;
1182 gen_complement (struct agent_expr *ax, struct axs_value *value)
1184 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1185 error (_("Invalid type of operand to `~'."));
1187 ax_simple (ax, aop_bit_not);
1188 gen_extend (ax, value->type);
1193 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1195 /* Dereference the value on the top of the stack. */
1197 gen_deref (struct agent_expr *ax, struct axs_value *value)
1199 /* The caller should check the type, because several operators use
1200 this, and we don't know what error message to generate. */
1201 if (!pointer_type (value->type))
1202 internal_error (__FILE__, __LINE__,
1203 _("gen_deref: expected a pointer"));
1205 /* We've got an rvalue now, which is a pointer. We want to yield an
1206 lvalue, whose address is exactly that pointer. So we don't
1207 actually emit any code; we just change the type from "Pointer to
1208 T" to "T", and mark the value as an lvalue in memory. Leave it
1209 to the consumer to actually dereference it. */
1210 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1211 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1212 error (_("Attempt to dereference a generic pointer."));
1213 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1214 ? axs_rvalue : axs_lvalue_memory);
1218 /* Produce the address of the lvalue on the top of the stack. */
1220 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1222 /* Special case for taking the address of a function. The ANSI
1223 standard describes this as a special case, too, so this
1224 arrangement is not without motivation. */
1225 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1226 /* The value's already an rvalue on the stack, so we just need to
1228 value->type = lookup_pointer_type (value->type);
1230 switch (value->kind)
1233 error (_("Operand of `&' is an rvalue, which has no address."));
1235 case axs_lvalue_register:
1236 error (_("Operand of `&' is in a register, and has no address."));
1238 case axs_lvalue_memory:
1239 value->kind = axs_rvalue;
1240 value->type = lookup_pointer_type (value->type);
1245 /* Generate code to push the value of a bitfield of a structure whose
1246 address is on the top of the stack. START and END give the
1247 starting and one-past-ending *bit* numbers of the field within the
1250 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1251 struct axs_value *value, struct type *type,
1254 /* Note that ops[i] fetches 8 << i bits. */
1255 static enum agent_op ops[]
1256 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1257 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1259 /* We don't want to touch any byte that the bitfield doesn't
1260 actually occupy; we shouldn't make any accesses we're not
1261 explicitly permitted to. We rely here on the fact that the
1262 bytecode `ref' operators work on unaligned addresses.
1264 It takes some fancy footwork to get the stack to work the way
1265 we'd like. Say we're retrieving a bitfield that requires three
1266 fetches. Initially, the stack just contains the address:
1268 For the first fetch, we duplicate the address
1270 then add the byte offset, do the fetch, and shift and mask as
1271 needed, yielding a fragment of the value, properly aligned for
1272 the final bitwise or:
1274 then we swap, and repeat the process:
1275 frag1 addr --- address on top
1276 frag1 addr addr --- duplicate it
1277 frag1 addr frag2 --- get second fragment
1278 frag1 frag2 addr --- swap again
1279 frag1 frag2 frag3 --- get third fragment
1280 Notice that, since the third fragment is the last one, we don't
1281 bother duplicating the address this time. Now we have all the
1282 fragments on the stack, and we can simply `or' them together,
1283 yielding the final value of the bitfield. */
1285 /* The first and one-after-last bits in the field, but rounded down
1286 and up to byte boundaries. */
1287 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1288 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1292 /* current bit offset within the structure */
1295 /* The index in ops of the opcode we're considering. */
1298 /* The number of fragments we generated in the process. Probably
1299 equal to the number of `one' bits in bytesize, but who cares? */
1302 /* Dereference any typedefs. */
1303 type = check_typedef (type);
1305 /* Can we fetch the number of bits requested at all? */
1306 if ((end - start) > ((1 << num_ops) * 8))
1307 internal_error (__FILE__, __LINE__,
1308 _("gen_bitfield_ref: bitfield too wide"));
1310 /* Note that we know here that we only need to try each opcode once.
1311 That may not be true on machines with weird byte sizes. */
1312 offset = bound_start;
1314 for (op = num_ops - 1; op >= 0; op--)
1316 /* number of bits that ops[op] would fetch */
1317 int op_size = 8 << op;
1319 /* The stack at this point, from bottom to top, contains zero or
1320 more fragments, then the address. */
1322 /* Does this fetch fit within the bitfield? */
1323 if (offset + op_size <= bound_end)
1325 /* Is this the last fragment? */
1326 int last_frag = (offset + op_size == bound_end);
1329 ax_simple (ax, aop_dup); /* keep a copy of the address */
1331 /* Add the offset. */
1332 gen_offset (ax, offset / TARGET_CHAR_BIT);
1336 /* Record the area of memory we're about to fetch. */
1337 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1340 /* Perform the fetch. */
1341 ax_simple (ax, ops[op]);
1343 /* Shift the bits we have to their proper position.
1344 gen_left_shift will generate right shifts when the operand
1347 A big-endian field diagram to ponder:
1348 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1349 +------++------++------++------++------++------++------++------+
1350 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1352 bit number 16 32 48 53
1353 These are bit numbers as supplied by GDB. Note that the
1354 bit numbers run from right to left once you've fetched the
1357 A little-endian field diagram to ponder:
1358 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1359 +------++------++------++------++------++------++------++------+
1360 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1362 bit number 48 32 16 4 0
1364 In both cases, the most significant end is on the left
1365 (i.e. normal numeric writing order), which means that you
1366 don't go crazy thinking about `left' and `right' shifts.
1368 We don't have to worry about masking yet:
1369 - If they contain garbage off the least significant end, then we
1370 must be looking at the low end of the field, and the right
1371 shift will wipe them out.
1372 - If they contain garbage off the most significant end, then we
1373 must be looking at the most significant end of the word, and
1374 the sign/zero extension will wipe them out.
1375 - If we're in the interior of the word, then there is no garbage
1376 on either end, because the ref operators zero-extend. */
1377 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1378 gen_left_shift (ax, end - (offset + op_size));
1380 gen_left_shift (ax, offset - start);
1383 /* Bring the copy of the address up to the top. */
1384 ax_simple (ax, aop_swap);
1391 /* Generate enough bitwise `or' operations to combine all the
1392 fragments we left on the stack. */
1393 while (fragment_count-- > 1)
1394 ax_simple (ax, aop_bit_or);
1396 /* Sign- or zero-extend the value as appropriate. */
1397 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1399 /* This is *not* an lvalue. Ugh. */
1400 value->kind = axs_rvalue;
1404 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1405 is an accumulated offset (in bytes), will be nonzero for objects
1406 embedded in other objects, like C++ base classes. Behavior should
1407 generally follow value_primitive_field. */
1410 gen_primitive_field (struct expression *exp,
1411 struct agent_expr *ax, struct axs_value *value,
1412 int offset, int fieldno, struct type *type)
1414 /* Is this a bitfield? */
1415 if (TYPE_FIELD_PACKED (type, fieldno))
1416 gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1417 (offset * TARGET_CHAR_BIT
1418 + TYPE_FIELD_BITPOS (type, fieldno)),
1419 (offset * TARGET_CHAR_BIT
1420 + TYPE_FIELD_BITPOS (type, fieldno)
1421 + TYPE_FIELD_BITSIZE (type, fieldno)));
1424 gen_offset (ax, offset
1425 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1426 value->kind = axs_lvalue_memory;
1427 value->type = TYPE_FIELD_TYPE (type, fieldno);
1431 /* Search for the given field in either the given type or one of its
1432 base classes. Return 1 if found, 0 if not. */
1435 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1436 struct axs_value *value,
1437 char *field, int offset, struct type *type)
1440 int nbases = TYPE_N_BASECLASSES (type);
1442 CHECK_TYPEDEF (type);
1444 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1446 const char *this_name = TYPE_FIELD_NAME (type, i);
1450 if (strcmp (field, this_name) == 0)
1452 /* Note that bytecodes for the struct's base (aka
1453 "this") will have been generated already, which will
1454 be unnecessary but not harmful if the static field is
1455 being handled as a global. */
1456 if (field_is_static (&TYPE_FIELD (type, i)))
1458 gen_static_field (exp->gdbarch, ax, value, type, i);
1459 if (value->optimized_out)
1460 error (_("static field `%s' has been "
1461 "optimized out, cannot use"),
1466 gen_primitive_field (exp, ax, value, offset, i, type);
1469 #if 0 /* is this right? */
1470 if (this_name[0] == '\0')
1471 internal_error (__FILE__, __LINE__,
1472 _("find_field: anonymous unions not supported"));
1477 /* Now scan through base classes recursively. */
1478 for (i = 0; i < nbases; i++)
1480 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1482 rslt = gen_struct_ref_recursive (exp, ax, value, field,
1483 offset + TYPE_BASECLASS_BITPOS (type, i)
1490 /* Not found anywhere, flag so caller can complain. */
1494 /* Generate code to reference the member named FIELD of a structure or
1495 union. The top of the stack, as described by VALUE, should have
1496 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1497 the operator being compiled, and OPERAND_NAME is the kind of thing
1498 it operates on; we use them in error messages. */
1500 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1501 struct axs_value *value, char *field,
1502 char *operator_name, char *operand_name)
1507 /* Follow pointers until we reach a non-pointer. These aren't the C
1508 semantics, but they're what the normal GDB evaluator does, so we
1509 should at least be consistent. */
1510 while (pointer_type (value->type))
1512 require_rvalue (ax, value);
1513 gen_deref (ax, value);
1515 type = check_typedef (value->type);
1517 /* This must yield a structure or a union. */
1518 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1519 && TYPE_CODE (type) != TYPE_CODE_UNION)
1520 error (_("The left operand of `%s' is not a %s."),
1521 operator_name, operand_name);
1523 /* And it must be in memory; we don't deal with structure rvalues,
1524 or structures living in registers. */
1525 if (value->kind != axs_lvalue_memory)
1526 error (_("Structure does not live in memory."));
1528 /* Search through fields and base classes recursively. */
1529 found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1532 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1533 field, TYPE_TAG_NAME (type));
1537 gen_namespace_elt (struct expression *exp,
1538 struct agent_expr *ax, struct axs_value *value,
1539 const struct type *curtype, char *name);
1541 gen_maybe_namespace_elt (struct expression *exp,
1542 struct agent_expr *ax, struct axs_value *value,
1543 const struct type *curtype, char *name);
1546 gen_static_field (struct gdbarch *gdbarch,
1547 struct agent_expr *ax, struct axs_value *value,
1548 struct type *type, int fieldno)
1550 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1552 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1553 value->kind = axs_lvalue_memory;
1554 value->type = TYPE_FIELD_TYPE (type, fieldno);
1555 value->optimized_out = 0;
1559 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1560 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
1564 gen_var_ref (gdbarch, ax, value, sym);
1566 /* Don't error if the value was optimized out, we may be
1567 scanning all static fields and just want to pass over this
1568 and continue with the rest. */
1572 /* Silently assume this was optimized out; class printing
1573 will let the user know why the data is missing. */
1574 value->optimized_out = 1;
1580 gen_struct_elt_for_reference (struct expression *exp,
1581 struct agent_expr *ax, struct axs_value *value,
1582 struct type *type, char *fieldname)
1584 struct type *t = type;
1587 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1588 && TYPE_CODE (t) != TYPE_CODE_UNION)
1589 internal_error (__FILE__, __LINE__,
1590 _("non-aggregate type to gen_struct_elt_for_reference"));
1592 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1594 const char *t_field_name = TYPE_FIELD_NAME (t, i);
1596 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1598 if (field_is_static (&TYPE_FIELD (t, i)))
1600 gen_static_field (exp->gdbarch, ax, value, t, i);
1601 if (value->optimized_out)
1602 error (_("static field `%s' has been "
1603 "optimized out, cannot use"),
1607 if (TYPE_FIELD_PACKED (t, i))
1608 error (_("pointers to bitfield members not allowed"));
1610 /* FIXME we need a way to do "want_address" equivalent */
1612 error (_("Cannot reference non-static field \"%s\""), fieldname);
1616 /* FIXME add other scoped-reference cases here */
1618 /* Do a last-ditch lookup. */
1619 return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1622 /* C++: Return the member NAME of the namespace given by the type
1626 gen_namespace_elt (struct expression *exp,
1627 struct agent_expr *ax, struct axs_value *value,
1628 const struct type *curtype, char *name)
1630 int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1633 error (_("No symbol \"%s\" in namespace \"%s\"."),
1634 name, TYPE_TAG_NAME (curtype));
1639 /* A helper function used by value_namespace_elt and
1640 value_struct_elt_for_reference. It looks up NAME inside the
1641 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1642 is a class and NAME refers to a type in CURTYPE itself (as opposed
1643 to, say, some base class of CURTYPE). */
1646 gen_maybe_namespace_elt (struct expression *exp,
1647 struct agent_expr *ax, struct axs_value *value,
1648 const struct type *curtype, char *name)
1650 const char *namespace_name = TYPE_TAG_NAME (curtype);
1653 sym = cp_lookup_symbol_namespace (namespace_name, name,
1654 block_for_pc (ax->scope),
1660 gen_var_ref (exp->gdbarch, ax, value, sym);
1662 if (value->optimized_out)
1663 error (_("`%s' has been optimized out, cannot use"),
1664 SYMBOL_PRINT_NAME (sym));
1671 gen_aggregate_elt_ref (struct expression *exp,
1672 struct agent_expr *ax, struct axs_value *value,
1673 struct type *type, char *field,
1674 char *operator_name, char *operand_name)
1676 switch (TYPE_CODE (type))
1678 case TYPE_CODE_STRUCT:
1679 case TYPE_CODE_UNION:
1680 return gen_struct_elt_for_reference (exp, ax, value, type, field);
1682 case TYPE_CODE_NAMESPACE:
1683 return gen_namespace_elt (exp, ax, value, type, field);
1686 internal_error (__FILE__, __LINE__,
1687 _("non-aggregate type in gen_aggregate_elt_ref"));
1693 /* Generate code for GDB's magical `repeat' operator.
1694 LVALUE @ INT creates an array INT elements long, and whose elements
1695 have the same type as LVALUE, located in memory so that LVALUE is
1696 its first element. For example, argv[0]@argc gives you the array
1697 of command-line arguments.
1699 Unfortunately, because we have to know the types before we actually
1700 have a value for the expression, we can't implement this perfectly
1701 without changing the type system, having values that occupy two
1702 stack slots, doing weird things with sizeof, etc. So we require
1703 the right operand to be a constant expression. */
1705 gen_repeat (struct expression *exp, union exp_element **pc,
1706 struct agent_expr *ax, struct axs_value *value)
1708 struct axs_value value1;
1710 /* We don't want to turn this into an rvalue, so no conversions
1712 gen_expr (exp, pc, ax, &value1);
1713 if (value1.kind != axs_lvalue_memory)
1714 error (_("Left operand of `@' must be an object in memory."));
1716 /* Evaluate the length; it had better be a constant. */
1718 struct value *v = const_expr (pc);
1722 error (_("Right operand of `@' must be a "
1723 "constant, in agent expressions."));
1724 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1725 error (_("Right operand of `@' must be an integer."));
1726 length = value_as_long (v);
1728 error (_("Right operand of `@' must be positive."));
1730 /* The top of the stack is already the address of the object, so
1731 all we need to do is frob the type of the lvalue. */
1733 /* FIXME-type-allocation: need a way to free this type when we are
1736 = lookup_array_range_type (value1.type, 0, length - 1);
1738 value->kind = axs_lvalue_memory;
1739 value->type = array;
1745 /* Emit code for the `sizeof' operator.
1746 *PC should point at the start of the operand expression; we advance it
1747 to the first instruction after the operand. */
1749 gen_sizeof (struct expression *exp, union exp_element **pc,
1750 struct agent_expr *ax, struct axs_value *value,
1751 struct type *size_type)
1753 /* We don't care about the value of the operand expression; we only
1754 care about its type. However, in the current arrangement, the
1755 only way to find an expression's type is to generate code for it.
1756 So we generate code for the operand, and then throw it away,
1757 replacing it with code that simply pushes its size. */
1758 int start = ax->len;
1760 gen_expr (exp, pc, ax, value);
1762 /* Throw away the code we just generated. */
1765 ax_const_l (ax, TYPE_LENGTH (value->type));
1766 value->kind = axs_rvalue;
1767 value->type = size_type;
1771 /* Generating bytecode from GDB expressions: general recursive thingy */
1774 /* A gen_expr function written by a Gen-X'er guy.
1775 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1777 gen_expr (struct expression *exp, union exp_element **pc,
1778 struct agent_expr *ax, struct axs_value *value)
1780 /* Used to hold the descriptions of operand expressions. */
1781 struct axs_value value1, value2, value3;
1782 enum exp_opcode op = (*pc)[0].opcode, op2;
1783 int if1, go1, if2, go2, end;
1784 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
1786 /* If we're looking at a constant expression, just push its value. */
1788 struct value *v = maybe_const_expr (pc);
1792 ax_const_l (ax, value_as_long (v));
1793 value->kind = axs_rvalue;
1794 value->type = check_typedef (value_type (v));
1799 /* Otherwise, go ahead and generate code for it. */
1802 /* Binary arithmetic operators. */
1810 case BINOP_SUBSCRIPT:
1811 case BINOP_BITWISE_AND:
1812 case BINOP_BITWISE_IOR:
1813 case BINOP_BITWISE_XOR:
1815 case BINOP_NOTEQUAL:
1821 gen_expr (exp, pc, ax, &value1);
1822 gen_usual_unary (exp, ax, &value1);
1823 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1826 case BINOP_LOGICAL_AND:
1828 /* Generate the obvious sequence of tests and jumps. */
1829 gen_expr (exp, pc, ax, &value1);
1830 gen_usual_unary (exp, ax, &value1);
1831 if1 = ax_goto (ax, aop_if_goto);
1832 go1 = ax_goto (ax, aop_goto);
1833 ax_label (ax, if1, ax->len);
1834 gen_expr (exp, pc, ax, &value2);
1835 gen_usual_unary (exp, ax, &value2);
1836 if2 = ax_goto (ax, aop_if_goto);
1837 go2 = ax_goto (ax, aop_goto);
1838 ax_label (ax, if2, ax->len);
1840 end = ax_goto (ax, aop_goto);
1841 ax_label (ax, go1, ax->len);
1842 ax_label (ax, go2, ax->len);
1844 ax_label (ax, end, ax->len);
1845 value->kind = axs_rvalue;
1846 value->type = int_type;
1849 case BINOP_LOGICAL_OR:
1851 /* Generate the obvious sequence of tests and jumps. */
1852 gen_expr (exp, pc, ax, &value1);
1853 gen_usual_unary (exp, ax, &value1);
1854 if1 = ax_goto (ax, aop_if_goto);
1855 gen_expr (exp, pc, ax, &value2);
1856 gen_usual_unary (exp, ax, &value2);
1857 if2 = ax_goto (ax, aop_if_goto);
1859 end = ax_goto (ax, aop_goto);
1860 ax_label (ax, if1, ax->len);
1861 ax_label (ax, if2, ax->len);
1863 ax_label (ax, end, ax->len);
1864 value->kind = axs_rvalue;
1865 value->type = int_type;
1870 gen_expr (exp, pc, ax, &value1);
1871 gen_usual_unary (exp, ax, &value1);
1872 /* For (A ? B : C), it's easiest to generate subexpression
1873 bytecodes in order, but if_goto jumps on true, so we invert
1874 the sense of A. Then we can do B by dropping through, and
1876 gen_logical_not (ax, &value1, int_type);
1877 if1 = ax_goto (ax, aop_if_goto);
1878 gen_expr (exp, pc, ax, &value2);
1879 gen_usual_unary (exp, ax, &value2);
1880 end = ax_goto (ax, aop_goto);
1881 ax_label (ax, if1, ax->len);
1882 gen_expr (exp, pc, ax, &value3);
1883 gen_usual_unary (exp, ax, &value3);
1884 ax_label (ax, end, ax->len);
1885 /* This is arbitary - what if B and C are incompatible types? */
1886 value->type = value2.type;
1887 value->kind = value2.kind;
1892 if ((*pc)[0].opcode == OP_INTERNALVAR)
1894 char *name = internalvar_name ((*pc)[1].internalvar);
1895 struct trace_state_variable *tsv;
1898 gen_expr (exp, pc, ax, value);
1899 tsv = find_trace_state_variable (name);
1902 ax_tsv (ax, aop_setv, tsv->number);
1904 ax_tsv (ax, aop_tracev, tsv->number);
1907 error (_("$%s is not a trace state variable, "
1908 "may not assign to it"), name);
1911 error (_("May only assign to trace state variables"));
1914 case BINOP_ASSIGN_MODIFY:
1916 op2 = (*pc)[0].opcode;
1919 if ((*pc)[0].opcode == OP_INTERNALVAR)
1921 char *name = internalvar_name ((*pc)[1].internalvar);
1922 struct trace_state_variable *tsv;
1925 tsv = find_trace_state_variable (name);
1928 /* The tsv will be the left half of the binary operation. */
1929 ax_tsv (ax, aop_getv, tsv->number);
1931 ax_tsv (ax, aop_tracev, tsv->number);
1932 /* Trace state variables are always 64-bit integers. */
1933 value1.kind = axs_rvalue;
1934 value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1935 /* Now do right half of expression. */
1936 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1937 /* We have a result of the binary op, set the tsv. */
1938 ax_tsv (ax, aop_setv, tsv->number);
1940 ax_tsv (ax, aop_tracev, tsv->number);
1943 error (_("$%s is not a trace state variable, "
1944 "may not assign to it"), name);
1947 error (_("May only assign to trace state variables"));
1950 /* Note that we need to be a little subtle about generating code
1951 for comma. In C, we can do some optimizations here because
1952 we know the left operand is only being evaluated for effect.
1953 However, if the tracing kludge is in effect, then we always
1954 need to evaluate the left hand side fully, so that all the
1955 variables it mentions get traced. */
1958 gen_expr (exp, pc, ax, &value1);
1959 /* Don't just dispose of the left operand. We might be tracing,
1960 in which case we want to emit code to trace it if it's an
1962 gen_traced_pop (exp->gdbarch, ax, &value1);
1963 gen_expr (exp, pc, ax, value);
1964 /* It's the consumer's responsibility to trace the right operand. */
1967 case OP_LONG: /* some integer constant */
1969 struct type *type = (*pc)[1].type;
1970 LONGEST k = (*pc)[2].longconst;
1973 gen_int_literal (ax, value, k, type);
1978 gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
1980 if (value->optimized_out)
1981 error (_("`%s' has been optimized out, cannot use"),
1982 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
1989 const char *name = &(*pc)[2].string;
1992 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1993 reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
1995 internal_error (__FILE__, __LINE__,
1996 _("Register $%s not available"), name);
1997 /* No support for tracing user registers yet. */
1998 if (reg >= gdbarch_num_regs (exp->gdbarch)
1999 + gdbarch_num_pseudo_regs (exp->gdbarch))
2000 error (_("'%s' is a user-register; "
2001 "GDB cannot yet trace user-register contents."),
2003 value->kind = axs_lvalue_register;
2005 value->type = register_type (exp->gdbarch, reg);
2009 case OP_INTERNALVAR:
2011 struct internalvar *var = (*pc)[1].internalvar;
2012 const char *name = internalvar_name (var);
2013 struct trace_state_variable *tsv;
2016 tsv = find_trace_state_variable (name);
2019 ax_tsv (ax, aop_getv, tsv->number);
2021 ax_tsv (ax, aop_tracev, tsv->number);
2022 /* Trace state variables are always 64-bit integers. */
2023 value->kind = axs_rvalue;
2024 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2026 else if (! compile_internalvar_to_ax (var, ax, value))
2027 error (_("$%s is not a trace state variable; GDB agent "
2028 "expressions cannot use convenience variables."), name);
2032 /* Weirdo operator: see comments for gen_repeat for details. */
2034 /* Note that gen_repeat handles its own argument evaluation. */
2036 gen_repeat (exp, pc, ax, value);
2041 struct type *type = (*pc)[1].type;
2044 gen_expr (exp, pc, ax, value);
2045 gen_cast (ax, value, type);
2049 case UNOP_CAST_TYPE:
2056 offset = *pc - exp->elts;
2057 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2058 type = value_type (val);
2059 *pc = &exp->elts[offset];
2061 gen_expr (exp, pc, ax, value);
2062 gen_cast (ax, value, type);
2068 struct type *type = check_typedef ((*pc)[1].type);
2071 gen_expr (exp, pc, ax, value);
2073 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2074 already have the right value on the stack. For
2075 axs_lvalue_register, we must convert. */
2076 if (value->kind == axs_lvalue_register)
2077 require_rvalue (ax, value);
2080 value->kind = axs_lvalue_memory;
2084 case UNOP_MEMVAL_TYPE:
2091 offset = *pc - exp->elts;
2092 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2093 type = value_type (val);
2094 *pc = &exp->elts[offset];
2096 gen_expr (exp, pc, ax, value);
2098 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2099 already have the right value on the stack. For
2100 axs_lvalue_register, we must convert. */
2101 if (value->kind == axs_lvalue_register)
2102 require_rvalue (ax, value);
2105 value->kind = axs_lvalue_memory;
2111 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2112 gen_expr (exp, pc, ax, value);
2113 gen_usual_unary (exp, ax, value);
2118 /* -FOO is equivalent to 0 - FOO. */
2119 gen_int_literal (ax, &value1, 0,
2120 builtin_type (exp->gdbarch)->builtin_int);
2121 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2122 gen_expr (exp, pc, ax, &value2);
2123 gen_usual_unary (exp, ax, &value2);
2124 gen_usual_arithmetic (exp, ax, &value1, &value2);
2125 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2128 case UNOP_LOGICAL_NOT:
2130 gen_expr (exp, pc, ax, value);
2131 gen_usual_unary (exp, ax, value);
2132 gen_logical_not (ax, value, int_type);
2135 case UNOP_COMPLEMENT:
2137 gen_expr (exp, pc, ax, value);
2138 gen_usual_unary (exp, ax, value);
2139 gen_integral_promotions (exp, ax, value);
2140 gen_complement (ax, value);
2145 gen_expr (exp, pc, ax, value);
2146 gen_usual_unary (exp, ax, value);
2147 if (!pointer_type (value->type))
2148 error (_("Argument of unary `*' is not a pointer."));
2149 gen_deref (ax, value);
2154 gen_expr (exp, pc, ax, value);
2155 gen_address_of (ax, value);
2160 /* Notice that gen_sizeof handles its own operand, unlike most
2161 of the other unary operator functions. This is because we
2162 have to throw away the code we generate. */
2163 gen_sizeof (exp, pc, ax, value,
2164 builtin_type (exp->gdbarch)->builtin_int);
2167 case STRUCTOP_STRUCT:
2170 int length = (*pc)[1].longconst;
2171 char *name = &(*pc)[2].string;
2173 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2174 gen_expr (exp, pc, ax, value);
2175 if (op == STRUCTOP_STRUCT)
2176 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2177 else if (op == STRUCTOP_PTR)
2178 gen_struct_ref (exp, ax, value, name, "->",
2179 "pointer to a structure or union");
2181 /* If this `if' chain doesn't handle it, then the case list
2182 shouldn't mention it, and we shouldn't be here. */
2183 internal_error (__FILE__, __LINE__,
2184 _("gen_expr: unhandled struct case"));
2190 struct symbol *sym, *func;
2192 const struct language_defn *lang;
2194 b = block_for_pc (ax->scope);
2195 func = block_linkage_function (b);
2196 lang = language_def (SYMBOL_LANGUAGE (func));
2198 sym = lookup_language_this (lang, b);
2200 error (_("no `%s' found"), lang->la_name_of_this);
2202 gen_var_ref (exp->gdbarch, ax, value, sym);
2204 if (value->optimized_out)
2205 error (_("`%s' has been optimized out, cannot use"),
2206 SYMBOL_PRINT_NAME (sym));
2214 struct type *type = (*pc)[1].type;
2215 int length = longest_to_int ((*pc)[2].longconst);
2216 char *name = &(*pc)[3].string;
2219 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2222 error (_("There is no field named %s"), name);
2223 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2230 error (_("Attempt to use a type name as an expression."));
2233 error (_("Unsupported operator %s (%d) in expression."),
2234 op_name (exp, op), op);
2238 /* This handles the middle-to-right-side of code generation for binary
2239 expressions, which is shared between regular binary operations and
2240 assign-modify (+= and friends) expressions. */
2243 gen_expr_binop_rest (struct expression *exp,
2244 enum exp_opcode op, union exp_element **pc,
2245 struct agent_expr *ax, struct axs_value *value,
2246 struct axs_value *value1, struct axs_value *value2)
2248 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2250 gen_expr (exp, pc, ax, value2);
2251 gen_usual_unary (exp, ax, value2);
2252 gen_usual_arithmetic (exp, ax, value1, value2);
2256 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2257 && pointer_type (value2->type))
2259 /* Swap the values and proceed normally. */
2260 ax_simple (ax, aop_swap);
2261 gen_ptradd (ax, value, value2, value1);
2263 else if (pointer_type (value1->type)
2264 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2265 gen_ptradd (ax, value, value1, value2);
2267 gen_binop (ax, value, value1, value2,
2268 aop_add, aop_add, 1, "addition");
2271 if (pointer_type (value1->type)
2272 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2273 gen_ptrsub (ax,value, value1, value2);
2274 else if (pointer_type (value1->type)
2275 && pointer_type (value2->type))
2276 /* FIXME --- result type should be ptrdiff_t */
2277 gen_ptrdiff (ax, value, value1, value2,
2278 builtin_type (exp->gdbarch)->builtin_long);
2280 gen_binop (ax, value, value1, value2,
2281 aop_sub, aop_sub, 1, "subtraction");
2284 gen_binop (ax, value, value1, value2,
2285 aop_mul, aop_mul, 1, "multiplication");
2288 gen_binop (ax, value, value1, value2,
2289 aop_div_signed, aop_div_unsigned, 1, "division");
2292 gen_binop (ax, value, value1, value2,
2293 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2296 gen_binop (ax, value, value1, value2,
2297 aop_lsh, aop_lsh, 1, "left shift");
2300 gen_binop (ax, value, value1, value2,
2301 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2303 case BINOP_SUBSCRIPT:
2307 if (binop_types_user_defined_p (op, value1->type, value2->type))
2309 error (_("cannot subscript requested type: "
2310 "cannot call user defined functions"));
2314 /* If the user attempts to subscript something that is not
2315 an array or pointer type (like a plain int variable for
2316 example), then report this as an error. */
2317 type = check_typedef (value1->type);
2318 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2319 && TYPE_CODE (type) != TYPE_CODE_PTR)
2321 if (TYPE_NAME (type))
2322 error (_("cannot subscript something of type `%s'"),
2325 error (_("cannot subscript requested type"));
2329 if (!is_integral_type (value2->type))
2330 error (_("Argument to arithmetic operation "
2331 "not a number or boolean."));
2333 gen_ptradd (ax, value, value1, value2);
2334 gen_deref (ax, value);
2337 case BINOP_BITWISE_AND:
2338 gen_binop (ax, value, value1, value2,
2339 aop_bit_and, aop_bit_and, 0, "bitwise and");
2342 case BINOP_BITWISE_IOR:
2343 gen_binop (ax, value, value1, value2,
2344 aop_bit_or, aop_bit_or, 0, "bitwise or");
2347 case BINOP_BITWISE_XOR:
2348 gen_binop (ax, value, value1, value2,
2349 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2353 gen_equal (ax, value, value1, value2, int_type);
2356 case BINOP_NOTEQUAL:
2357 gen_equal (ax, value, value1, value2, int_type);
2358 gen_logical_not (ax, value, int_type);
2362 gen_less (ax, value, value1, value2, int_type);
2366 ax_simple (ax, aop_swap);
2367 gen_less (ax, value, value1, value2, int_type);
2371 ax_simple (ax, aop_swap);
2372 gen_less (ax, value, value1, value2, int_type);
2373 gen_logical_not (ax, value, int_type);
2377 gen_less (ax, value, value1, value2, int_type);
2378 gen_logical_not (ax, value, int_type);
2382 /* We should only list operators in the outer case statement
2383 that we actually handle in the inner case statement. */
2384 internal_error (__FILE__, __LINE__,
2385 _("gen_expr: op case sets don't match"));
2390 /* Given a single variable and a scope, generate bytecodes to trace
2391 its value. This is for use in situations where we have only a
2392 variable's name, and no parsed expression; for instance, when the
2393 name comes from a list of local variables of a function. */
2396 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2397 struct symbol *var, int trace_string)
2399 struct cleanup *old_chain = 0;
2400 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2401 struct axs_value value;
2403 old_chain = make_cleanup_free_agent_expr (ax);
2406 ax->trace_string = trace_string;
2407 gen_var_ref (gdbarch, ax, &value, var);
2409 /* If there is no actual variable to trace, flag it by returning
2410 an empty agent expression. */
2411 if (value.optimized_out)
2413 do_cleanups (old_chain);
2417 /* Make sure we record the final object, and get rid of it. */
2418 gen_traced_pop (gdbarch, ax, &value);
2420 /* Oh, and terminate. */
2421 ax_simple (ax, aop_end);
2423 /* We have successfully built the agent expr, so cancel the cleanup
2424 request. If we add more cleanups that we always want done, this
2425 will have to get more complicated. */
2426 discard_cleanups (old_chain);
2430 /* Generating bytecode from GDB expressions: driver */
2432 /* Given a GDB expression EXPR, return bytecode to trace its value.
2433 The result will use the `trace' and `trace_quick' bytecodes to
2434 record the value of all memory touched by the expression. The
2435 caller can then use the ax_reqs function to discover which
2436 registers it relies upon. */
2438 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
2441 struct cleanup *old_chain = 0;
2442 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2443 union exp_element *pc;
2444 struct axs_value value;
2446 old_chain = make_cleanup_free_agent_expr (ax);
2450 ax->trace_string = trace_string;
2451 value.optimized_out = 0;
2452 gen_expr (expr, &pc, ax, &value);
2454 /* Make sure we record the final object, and get rid of it. */
2455 gen_traced_pop (expr->gdbarch, ax, &value);
2457 /* Oh, and terminate. */
2458 ax_simple (ax, aop_end);
2460 /* We have successfully built the agent expr, so cancel the cleanup
2461 request. If we add more cleanups that we always want done, this
2462 will have to get more complicated. */
2463 discard_cleanups (old_chain);
2467 /* Given a GDB expression EXPR, return a bytecode sequence that will
2468 evaluate and return a result. The bytecodes will do a direct
2469 evaluation, using the current data on the target, rather than
2470 recording blocks of memory and registers for later use, as
2471 gen_trace_for_expr does. The generated bytecode sequence leaves
2472 the result of expression evaluation on the top of the stack. */
2475 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2477 struct cleanup *old_chain = 0;
2478 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2479 union exp_element *pc;
2480 struct axs_value value;
2482 old_chain = make_cleanup_free_agent_expr (ax);
2486 value.optimized_out = 0;
2487 gen_expr (expr, &pc, ax, &value);
2489 require_rvalue (ax, &value);
2491 /* Oh, and terminate. */
2492 ax_simple (ax, aop_end);
2494 /* We have successfully built the agent expr, so cancel the cleanup
2495 request. If we add more cleanups that we always want done, this
2496 will have to get more complicated. */
2497 discard_cleanups (old_chain);
2502 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
2505 struct cleanup *old_chain = 0;
2506 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2507 struct axs_value value;
2509 old_chain = make_cleanup_free_agent_expr (ax);
2512 ax->trace_string = trace_string;
2514 gdbarch_gen_return_address (gdbarch, ax, &value, scope);
2516 /* Make sure we record the final object, and get rid of it. */
2517 gen_traced_pop (gdbarch, ax, &value);
2519 /* Oh, and terminate. */
2520 ax_simple (ax, aop_end);
2522 /* We have successfully built the agent expr, so cancel the cleanup
2523 request. If we add more cleanups that we always want done, this
2524 will have to get more complicated. */
2525 discard_cleanups (old_chain);
2529 /* Given a collection of printf-style arguments, generate code to
2530 evaluate the arguments and pass everything to a special
2534 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2535 CORE_ADDR function, LONGEST channel,
2536 const char *format, int fmtlen,
2537 struct format_piece *frags,
2538 int nargs, struct expression **exprs)
2540 struct cleanup *old_chain = 0;
2541 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2542 union exp_element *pc;
2543 struct axs_value value;
2546 old_chain = make_cleanup_free_agent_expr (ax);
2548 /* We're computing values, not doing side effects. */
2551 /* Evaluate and push the args on the stack in reverse order,
2552 for simplicity of collecting them on the target side. */
2553 for (tem = nargs - 1; tem >= 0; --tem)
2555 pc = exprs[tem]->elts;
2556 value.optimized_out = 0;
2557 gen_expr (exprs[tem], &pc, ax, &value);
2558 require_rvalue (ax, &value);
2561 /* Push function and channel. */
2562 ax_const_l (ax, channel);
2563 ax_const_l (ax, function);
2565 /* Issue the printf bytecode proper. */
2566 ax_simple (ax, aop_printf);
2567 ax_simple (ax, nargs);
2568 ax_string (ax, format, fmtlen);
2570 /* And terminate. */
2571 ax_simple (ax, aop_end);
2573 /* We have successfully built the agent expr, so cancel the cleanup
2574 request. If we add more cleanups that we always want done, this
2575 will have to get more complicated. */
2576 discard_cleanups (old_chain);
2582 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
2584 struct cleanup *old_chain = 0;
2585 struct expression *expr;
2586 struct agent_expr *agent;
2588 int trace_string = 0;
2593 exp = decode_agent_options (exp, &trace_string);
2597 if (!eval && strcmp (arg, "$_ret") == 0)
2599 agent = gen_trace_for_return_address (pc, get_current_arch (),
2601 old_chain = make_cleanup_free_agent_expr (agent);
2605 expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
2606 old_chain = make_cleanup (free_current_contents, &expr);
2609 gdb_assert (trace_string == 0);
2610 agent = gen_eval_for_expr (pc, expr);
2613 agent = gen_trace_for_expr (pc, expr, trace_string);
2614 make_cleanup_free_agent_expr (agent);
2618 ax_print (gdb_stdout, agent);
2620 /* It would be nice to call ax_reqs here to gather some general info
2621 about the expression, and then print out the result. */
2623 do_cleanups (old_chain);
2628 agent_command_1 (char *exp, int eval)
2630 /* We don't deal with overlay debugging at the moment. We need to
2631 think more carefully about this. If you copy this code into
2632 another command, change the error message; the user shouldn't
2633 have to know anything about agent expressions. */
2634 if (overlay_debugging)
2635 error (_("GDB can't do agent expression translation with overlays."));
2638 error_no_arg (_("expression to translate"));
2640 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2642 struct linespec_result canonical;
2644 struct linespec_sals *iter;
2645 struct cleanup *old_chain;
2647 exp = skip_spaces (exp);
2648 init_linespec_result (&canonical);
2649 decode_line_full (&exp, DECODE_LINE_FUNFIRSTLINE,
2650 (struct symtab *) NULL, 0, &canonical,
2652 old_chain = make_cleanup_destroy_linespec_result (&canonical);
2653 exp = skip_spaces (exp);
2657 exp = skip_spaces (exp);
2659 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
2663 for (i = 0; i < iter->sals.nelts; i++)
2664 agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
2666 do_cleanups (old_chain);
2669 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
2675 agent_command (char *exp, int from_tty)
2677 agent_command_1 (exp, 0);
2680 /* Parse the given expression, compile it into an agent expression
2681 that does direct evaluation, and display the resulting
2685 agent_eval_command (char *exp, int from_tty)
2687 agent_command_1 (exp, 1);
2690 /* Parse the given expression, compile it into an agent expression
2691 that does a printf, and display the resulting expression. */
2694 maint_agent_printf_command (char *exp, int from_tty)
2696 struct cleanup *old_chain = 0;
2697 struct expression *expr;
2698 struct expression *argvec[100];
2699 struct agent_expr *agent;
2700 struct frame_info *fi = get_current_frame (); /* need current scope */
2701 const char *cmdrest;
2702 const char *format_start, *format_end;
2703 struct format_piece *fpieces;
2706 /* We don't deal with overlay debugging at the moment. We need to
2707 think more carefully about this. If you copy this code into
2708 another command, change the error message; the user shouldn't
2709 have to know anything about agent expressions. */
2710 if (overlay_debugging)
2711 error (_("GDB can't do agent expression translation with overlays."));
2714 error_no_arg (_("expression to translate"));
2718 cmdrest = skip_spaces_const (cmdrest);
2720 if (*cmdrest++ != '"')
2721 error (_("Must start with a format string."));
2723 format_start = cmdrest;
2725 fpieces = parse_format_string (&cmdrest);
2727 old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2729 format_end = cmdrest;
2731 if (*cmdrest++ != '"')
2732 error (_("Bad format string, non-terminated '\"'."));
2734 cmdrest = skip_spaces_const (cmdrest);
2736 if (*cmdrest != ',' && *cmdrest != 0)
2737 error (_("Invalid argument syntax"));
2739 if (*cmdrest == ',')
2741 cmdrest = skip_spaces_const (cmdrest);
2744 while (*cmdrest != '\0')
2749 expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2750 argvec[nargs] = expr;
2753 if (*cmdrest == ',')
2755 /* else complain? */
2759 agent = gen_printf (get_frame_pc (fi), get_current_arch (), 0, 0,
2760 format_start, format_end - format_start,
2761 fpieces, nargs, argvec);
2762 make_cleanup_free_agent_expr (agent);
2764 ax_print (gdb_stdout, agent);
2766 /* It would be nice to call ax_reqs here to gather some general info
2767 about the expression, and then print out the result. */
2769 do_cleanups (old_chain);
2774 /* Initialization code. */
2776 void _initialize_ax_gdb (void);
2778 _initialize_ax_gdb (void)
2780 add_cmd ("agent", class_maintenance, agent_command,
2782 Translate an expression into remote agent bytecode for tracing.\n\
2783 Usage: maint agent [-at location,] EXPRESSION\n\
2784 If -at is given, generate remote agent bytecode for this location.\n\
2785 If not, generate remote agent bytecode for current frame pc address."),
2788 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2790 Translate an expression into remote agent bytecode for evaluation.\n\
2791 Usage: maint agent-eval [-at location,] EXPRESSION\n\
2792 If -at is given, generate remote agent bytecode for this location.\n\
2793 If not, generate remote agent bytecode for current frame pc address."),
2796 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2797 _("Translate an expression into remote "
2798 "agent bytecode for evaluation and display the bytecodes."),