1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998-2017 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"
35 #include "user-regs.h"
36 #include "dictionary.h"
37 #include "breakpoint.h"
38 #include "tracepoint.h"
39 #include "cp-support.h"
40 #include "arch-utils.h"
41 #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 agent_expr *ax, 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 agent_expr *ax,
108 struct axs_value *value1,
109 struct axs_value *value2);
110 static void gen_integral_promotions (struct agent_expr *ax,
111 struct axs_value *value);
112 static void gen_cast (struct agent_expr *ax,
113 struct axs_value *value, struct type *type);
114 static void gen_scale (struct agent_expr *ax,
115 enum agent_op op, struct type *type);
116 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
117 struct axs_value *value1, struct axs_value *value2);
118 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
119 struct axs_value *value1, struct axs_value *value2);
120 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
121 struct axs_value *value1, struct axs_value *value2,
122 struct type *result_type);
123 static void gen_binop (struct agent_expr *ax,
124 struct axs_value *value,
125 struct axs_value *value1,
126 struct axs_value *value2,
128 enum agent_op op_unsigned, int may_carry,
130 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
131 struct type *result_type);
132 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
133 static void gen_deref (struct axs_value *);
134 static void gen_address_of (struct axs_value *);
135 static void gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
136 struct type *type, int start, int end);
137 static void gen_primitive_field (struct agent_expr *ax,
138 struct axs_value *value,
139 int offset, int fieldno, struct type *type);
140 static int gen_struct_ref_recursive (struct agent_expr *ax,
141 struct axs_value *value,
142 const char *field, int offset,
144 static void gen_struct_ref (struct agent_expr *ax,
145 struct axs_value *value,
147 const char *operator_name,
148 const char *operand_name);
149 static void gen_static_field (struct gdbarch *gdbarch,
150 struct agent_expr *ax, struct axs_value *value,
151 struct type *type, int fieldno);
152 static void gen_repeat (struct expression *exp, union exp_element **pc,
153 struct agent_expr *ax, struct axs_value *value);
154 static void gen_sizeof (struct expression *exp, union exp_element **pc,
155 struct agent_expr *ax, struct axs_value *value,
156 struct type *size_type);
157 static void gen_expr_binop_rest (struct expression *exp,
158 enum exp_opcode op, union exp_element **pc,
159 struct agent_expr *ax,
160 struct axs_value *value,
161 struct axs_value *value1,
162 struct axs_value *value2);
164 static void agent_command (char *exp, int from_tty);
167 /* Detecting constant expressions. */
169 /* If the variable reference at *PC is a constant, return its value.
170 Otherwise, return zero.
172 Hey, Wally! How can a variable reference be a constant?
174 Well, Beav, this function really handles the OP_VAR_VALUE operator,
175 not specifically variable references. GDB uses OP_VAR_VALUE to
176 refer to any kind of symbolic reference: function names, enum
177 elements, and goto labels are all handled through the OP_VAR_VALUE
178 operator, even though they're constants. It makes sense given the
181 Gee, Wally, don'cha wonder sometimes if data representations that
182 subvert commonly accepted definitions of terms in favor of heavily
183 context-specific interpretations are really just a tool of the
184 programming hegemony to preserve their power and exclude the
187 static struct value *
188 const_var_ref (struct symbol *var)
190 struct type *type = SYMBOL_TYPE (var);
192 switch (SYMBOL_CLASS (var))
195 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
198 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
206 /* If the expression starting at *PC has a constant value, return it.
207 Otherwise, return zero. If we return a value, then *PC will be
208 advanced to the end of it. If we return zero, *PC could be
210 static struct value *
211 const_expr (union exp_element **pc)
213 enum exp_opcode op = (*pc)->opcode;
220 struct type *type = (*pc)[1].type;
221 LONGEST k = (*pc)[2].longconst;
224 return value_from_longest (type, k);
229 struct value *v = const_var_ref ((*pc)[2].symbol);
235 /* We could add more operators in here. */
239 v1 = const_expr (pc);
241 return value_neg (v1);
251 /* Like const_expr, but guarantee also that *PC is undisturbed if the
252 expression is not constant. */
253 static struct value *
254 maybe_const_expr (union exp_element **pc)
256 union exp_element *tentative_pc = *pc;
257 struct value *v = const_expr (&tentative_pc);
259 /* If we got a value, then update the real PC. */
267 /* Generating bytecode from GDB expressions: general assumptions */
269 /* Here are a few general assumptions made throughout the code; if you
270 want to make a change that contradicts one of these, then you'd
271 better scan things pretty thoroughly.
273 - We assume that all values occupy one stack element. For example,
274 sometimes we'll swap to get at the left argument to a binary
275 operator. If we decide that void values should occupy no stack
276 elements, or that synthetic arrays (whose size is determined at
277 run time, created by the `@' operator) should occupy two stack
278 elements (address and length), then this will cause trouble.
280 - We assume the stack elements are infinitely wide, and that we
281 don't have to worry what happens if the user requests an
282 operation that is wider than the actual interpreter's stack.
283 That is, it's up to the interpreter to handle directly all the
284 integer widths the user has access to. (Woe betide the language
287 - We don't support side effects. Thus, we don't have to worry about
288 GCC's generalized lvalues, function calls, etc.
290 - We don't support floating point. Many places where we switch on
291 some type don't bother to include cases for floating point; there
292 may be even more subtle ways this assumption exists. For
293 example, the arguments to % must be integers.
295 - We assume all subexpressions have a static, unchanging type. If
296 we tried to support convenience variables, this would be a
299 - All values on the stack should always be fully zero- or
302 (I wasn't sure whether to choose this or its opposite --- that
303 only addresses are assumed extended --- but it turns out that
304 neither convention completely eliminates spurious extend
305 operations (if everything is always extended, then you have to
306 extend after add, because it could overflow; if nothing is
307 extended, then you end up producing extends whenever you change
308 sizes), and this is simpler.) */
311 /* Scan for all static fields in the given class, including any base
312 classes, and generate tracing bytecodes for each. */
315 gen_trace_static_fields (struct gdbarch *gdbarch,
316 struct agent_expr *ax,
319 int i, nbases = TYPE_N_BASECLASSES (type);
320 struct axs_value value;
322 type = check_typedef (type);
324 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
326 if (field_is_static (&TYPE_FIELD (type, i)))
328 gen_static_field (gdbarch, ax, &value, type, i);
329 if (value.optimized_out)
333 case axs_lvalue_memory:
335 /* Initialize the TYPE_LENGTH if it is a typedef. */
336 check_typedef (value.type);
337 ax_const_l (ax, TYPE_LENGTH (value.type));
338 ax_simple (ax, aop_trace);
342 case axs_lvalue_register:
343 /* We don't actually need the register's value to be pushed,
344 just note that we need it to be collected. */
345 ax_reg_mask (ax, value.u.reg);
353 /* Now scan through base classes recursively. */
354 for (i = 0; i < nbases; i++)
356 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
358 gen_trace_static_fields (gdbarch, ax, basetype);
362 /* Trace the lvalue on the stack, if it needs it. In either case, pop
363 the value. Useful on the left side of a comma, and at the end of
364 an expression being used for tracing. */
366 gen_traced_pop (struct gdbarch *gdbarch,
367 struct agent_expr *ax, struct axs_value *value)
369 int string_trace = 0;
371 && TYPE_CODE (value->type) == TYPE_CODE_PTR
372 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
382 ax_const_l (ax, ax->trace_string);
383 ax_simple (ax, aop_tracenz);
386 /* We don't trace rvalues, just the lvalues necessary to
387 produce them. So just dispose of this value. */
388 ax_simple (ax, aop_pop);
391 case axs_lvalue_memory:
393 /* Initialize the TYPE_LENGTH if it is a typedef. */
394 check_typedef (value->type);
398 gen_fetch (ax, value->type);
399 ax_const_l (ax, ax->trace_string);
400 ax_simple (ax, aop_tracenz);
404 /* There's no point in trying to use a trace_quick bytecode
405 here, since "trace_quick SIZE pop" is three bytes, whereas
406 "const8 SIZE trace" is also three bytes, does the same
407 thing, and the simplest code which generates that will also
408 work correctly for objects with large sizes. */
409 ax_const_l (ax, TYPE_LENGTH (value->type));
410 ax_simple (ax, aop_trace);
415 case axs_lvalue_register:
416 /* We don't actually need the register's value to be on the
417 stack, and the target will get heartburn if the register is
418 larger than will fit in a stack, so just mark it for
419 collection and be done with it. */
420 ax_reg_mask (ax, value->u.reg);
422 /* But if the register points to a string, assume the value
423 will fit on the stack and push it anyway. */
426 ax_reg (ax, value->u.reg);
427 ax_const_l (ax, ax->trace_string);
428 ax_simple (ax, aop_tracenz);
433 /* If we're not tracing, just pop the value. */
434 ax_simple (ax, aop_pop);
436 /* To trace C++ classes with static fields stored elsewhere. */
438 && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
439 || TYPE_CODE (value->type) == TYPE_CODE_UNION))
440 gen_trace_static_fields (gdbarch, ax, value->type);
445 /* Generating bytecode from GDB expressions: helper functions */
447 /* Assume that the lower bits of the top of the stack is a value of
448 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
450 gen_sign_extend (struct agent_expr *ax, struct type *type)
452 /* Do we need to sign-extend this? */
453 if (!TYPE_UNSIGNED (type))
454 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
458 /* Assume the lower bits of the top of the stack hold a value of type
459 TYPE, and the upper bits are garbage. Sign-extend or truncate as
462 gen_extend (struct agent_expr *ax, struct type *type)
464 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
467 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
471 /* Assume that the top of the stack contains a value of type "pointer
472 to TYPE"; generate code to fetch its value. Note that TYPE is the
473 target type, not the pointer type. */
475 gen_fetch (struct agent_expr *ax, struct type *type)
479 /* Record the area of memory we're about to fetch. */
480 ax_trace_quick (ax, TYPE_LENGTH (type));
483 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
484 type = TYPE_TARGET_TYPE (type);
486 switch (TYPE_CODE (type))
490 case TYPE_CODE_RVALUE_REF:
495 /* It's a scalar value, so we know how to dereference it. How
496 many bytes long is it? */
497 switch (TYPE_LENGTH (type))
499 case 8 / TARGET_CHAR_BIT:
500 ax_simple (ax, aop_ref8);
502 case 16 / TARGET_CHAR_BIT:
503 ax_simple (ax, aop_ref16);
505 case 32 / TARGET_CHAR_BIT:
506 ax_simple (ax, aop_ref32);
508 case 64 / TARGET_CHAR_BIT:
509 ax_simple (ax, aop_ref64);
512 /* Either our caller shouldn't have asked us to dereference
513 that pointer (other code's fault), or we're not
514 implementing something we should be (this code's fault).
515 In any case, it's a bug the user shouldn't see. */
517 internal_error (__FILE__, __LINE__,
518 _("gen_fetch: strange size"));
521 gen_sign_extend (ax, type);
525 /* Our caller requested us to dereference a pointer from an unsupported
526 type. Error out and give callers a chance to handle the failure
528 error (_("gen_fetch: Unsupported type code `%s'."),
534 /* Generate code to left shift the top of the stack by DISTANCE bits, or
535 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
536 unsigned (logical) right shifts. */
538 gen_left_shift (struct agent_expr *ax, int distance)
542 ax_const_l (ax, distance);
543 ax_simple (ax, aop_lsh);
545 else if (distance < 0)
547 ax_const_l (ax, -distance);
548 ax_simple (ax, aop_rsh_unsigned);
554 /* Generating bytecode from GDB expressions: symbol references */
556 /* Generate code to push the base address of the argument portion of
557 the top stack frame. */
559 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
562 LONGEST frame_offset;
564 gdbarch_virtual_frame_pointer (gdbarch,
565 ax->scope, &frame_reg, &frame_offset);
566 ax_reg (ax, frame_reg);
567 gen_offset (ax, frame_offset);
571 /* Generate code to push the base address of the locals portion of the
574 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
577 LONGEST frame_offset;
579 gdbarch_virtual_frame_pointer (gdbarch,
580 ax->scope, &frame_reg, &frame_offset);
581 ax_reg (ax, frame_reg);
582 gen_offset (ax, frame_offset);
586 /* Generate code to add OFFSET to the top of the stack. Try to
587 generate short and readable code. We use this for getting to
588 variables on the stack, and structure members. If we were
589 programming in ML, it would be clearer why these are the same
592 gen_offset (struct agent_expr *ax, int offset)
594 /* It would suffice to simply push the offset and add it, but this
595 makes it easier to read positive and negative offsets in the
599 ax_const_l (ax, offset);
600 ax_simple (ax, aop_add);
604 ax_const_l (ax, -offset);
605 ax_simple (ax, aop_sub);
610 /* In many cases, a symbol's value is the offset from some other
611 address (stack frame, base register, etc.) Generate code to add
612 VAR's value to the top of the stack. */
614 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
616 gen_offset (ax, SYMBOL_VALUE (var));
620 /* Generate code for a variable reference to AX. The variable is the
621 symbol VAR. Set VALUE to describe the result. */
624 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
625 struct axs_value *value, struct symbol *var)
627 /* Dereference any typedefs. */
628 value->type = check_typedef (SYMBOL_TYPE (var));
629 value->optimized_out = 0;
631 if (SYMBOL_COMPUTED_OPS (var) != NULL)
633 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
637 /* I'm imitating the code in read_var_value. */
638 switch (SYMBOL_CLASS (var))
640 case LOC_CONST: /* A constant, like an enum value. */
641 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
642 value->kind = axs_rvalue;
645 case LOC_LABEL: /* A goto label, being used as a value. */
646 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
647 value->kind = axs_rvalue;
650 case LOC_CONST_BYTES:
651 internal_error (__FILE__, __LINE__,
652 _("gen_var_ref: LOC_CONST_BYTES "
653 "symbols are not supported"));
655 /* Variable at a fixed location in memory. Easy. */
657 /* Push the address of the variable. */
658 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
659 value->kind = axs_lvalue_memory;
662 case LOC_ARG: /* var lives in argument area of frame */
663 gen_frame_args_address (gdbarch, ax);
664 gen_sym_offset (ax, var);
665 value->kind = axs_lvalue_memory;
668 case LOC_REF_ARG: /* As above, but the frame slot really
669 holds the address of the variable. */
670 gen_frame_args_address (gdbarch, ax);
671 gen_sym_offset (ax, var);
672 /* Don't assume any particular pointer size. */
673 gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
674 value->kind = axs_lvalue_memory;
677 case LOC_LOCAL: /* var lives in locals area of frame */
678 gen_frame_locals_address (gdbarch, ax);
679 gen_sym_offset (ax, var);
680 value->kind = axs_lvalue_memory;
684 error (_("Cannot compute value of typedef `%s'."),
685 SYMBOL_PRINT_NAME (var));
689 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
690 value->kind = axs_rvalue;
694 /* Don't generate any code at all; in the process of treating
695 this as an lvalue or rvalue, the caller will generate the
697 value->kind = axs_lvalue_register;
698 value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
701 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
702 register, not on the stack. Simpler than LOC_REGISTER
703 because it's just like any other case where the thing
704 has a real address. */
705 case LOC_REGPARM_ADDR:
706 ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
707 value->kind = axs_lvalue_memory;
712 struct bound_minimal_symbol msym
713 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
716 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
718 /* Push the address of the variable. */
719 ax_const_l (ax, BMSYMBOL_VALUE_ADDRESS (msym));
720 value->kind = axs_lvalue_memory;
725 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
727 case LOC_OPTIMIZED_OUT:
728 /* Flag this, but don't say anything; leave it up to callers to
730 value->optimized_out = 1;
734 error (_("Cannot find value of botched symbol `%s'."),
735 SYMBOL_PRINT_NAME (var));
742 /* Generating bytecode from GDB expressions: literals */
745 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
749 value->kind = axs_rvalue;
750 value->type = check_typedef (type);
755 /* Generating bytecode from GDB expressions: unary conversions, casts */
757 /* Take what's on the top of the stack (as described by VALUE), and
758 try to make an rvalue out of it. Signal an error if we can't do
761 require_rvalue (struct agent_expr *ax, struct axs_value *value)
763 /* Only deal with scalars, structs and such may be too large
764 to fit in a stack entry. */
765 value->type = check_typedef (value->type);
766 if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
767 || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
768 || TYPE_CODE (value->type) == TYPE_CODE_UNION
769 || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
770 error (_("Value not scalar: cannot be an rvalue."));
775 /* It's already an rvalue. */
778 case axs_lvalue_memory:
779 /* The top of stack is the address of the object. Dereference. */
780 gen_fetch (ax, value->type);
783 case axs_lvalue_register:
784 /* There's nothing on the stack, but value->u.reg is the
785 register number containing the value.
787 When we add floating-point support, this is going to have to
788 change. What about SPARC register pairs, for example? */
789 ax_reg (ax, value->u.reg);
790 gen_extend (ax, value->type);
794 value->kind = axs_rvalue;
798 /* Assume the top of the stack is described by VALUE, and perform the
799 usual unary conversions. This is motivated by ANSI 6.2.2, but of
800 course GDB expressions are not ANSI; they're the mishmash union of
801 a bunch of languages. Rah.
803 NOTE! This function promises to produce an rvalue only when the
804 incoming value is of an appropriate type. In other words, the
805 consumer of the value this function produces may assume the value
806 is an rvalue only after checking its type.
808 The immediate issue is that if the user tries to use a structure or
809 union as an operand of, say, the `+' operator, we don't want to try
810 to convert that structure to an rvalue; require_rvalue will bomb on
811 structs and unions. Rather, we want to simply pass the struct
812 lvalue through unchanged, and let `+' raise an error. */
815 gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
817 /* We don't have to generate any code for the usual integral
818 conversions, since values are always represented as full-width on
819 the stack. Should we tweak the type? */
821 /* Some types require special handling. */
822 switch (TYPE_CODE (value->type))
824 /* Functions get converted to a pointer to the function. */
826 value->type = lookup_pointer_type (value->type);
827 value->kind = axs_rvalue; /* Should always be true, but just in case. */
830 /* Arrays get converted to a pointer to their first element, and
831 are no longer an lvalue. */
832 case TYPE_CODE_ARRAY:
834 struct type *elements = TYPE_TARGET_TYPE (value->type);
836 value->type = lookup_pointer_type (elements);
837 value->kind = axs_rvalue;
838 /* We don't need to generate any code; the address of the array
839 is also the address of its first element. */
843 /* Don't try to convert structures and unions to rvalues. Let the
844 consumer signal an error. */
845 case TYPE_CODE_STRUCT:
846 case TYPE_CODE_UNION:
850 /* If the value is an lvalue, dereference it. */
851 require_rvalue (ax, value);
855 /* Return non-zero iff the type TYPE1 is considered "wider" than the
856 type TYPE2, according to the rules described in gen_usual_arithmetic. */
858 type_wider_than (struct type *type1, struct type *type2)
860 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
861 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
862 && TYPE_UNSIGNED (type1)
863 && !TYPE_UNSIGNED (type2)));
867 /* Return the "wider" of the two types TYPE1 and TYPE2. */
869 max_type (struct type *type1, struct type *type2)
871 return type_wider_than (type1, type2) ? type1 : type2;
875 /* Generate code to convert a scalar value of type FROM to type TO. */
877 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
879 /* Perhaps there is a more graceful way to state these rules. */
881 /* If we're converting to a narrower type, then we need to clear out
883 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
886 /* If the two values have equal width, but different signednesses,
887 then we need to extend. */
888 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
890 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
894 /* If we're converting to a wider type, and becoming unsigned, then
895 we need to zero out any possible sign bits. */
896 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
898 if (TYPE_UNSIGNED (to))
904 /* Return non-zero iff the type FROM will require any bytecodes to be
905 emitted to be converted to the type TO. */
907 is_nontrivial_conversion (struct type *from, struct type *to)
909 agent_expr_up ax (new agent_expr (NULL, 0));
912 /* Actually generate the code, and see if anything came out. At the
913 moment, it would be trivial to replicate the code in
914 gen_conversion here, but in the future, when we're supporting
915 floating point and the like, it may not be. Doing things this
916 way allows this function to be independent of the logic in
918 gen_conversion (ax.get (), from, to);
919 nontrivial = ax->len > 0;
924 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
925 6.2.1.5) for the two operands of an arithmetic operator. This
926 effectively finds a "least upper bound" type for the two arguments,
927 and promotes each argument to that type. *VALUE1 and *VALUE2
928 describe the values as they are passed in, and as they are left. */
930 gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
931 struct axs_value *value2)
933 /* Do the usual binary conversions. */
934 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
935 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
937 /* The ANSI integral promotions seem to work this way: Order the
938 integer types by size, and then by signedness: an n-bit
939 unsigned type is considered "wider" than an n-bit signed
940 type. Promote to the "wider" of the two types, and always
941 promote at least to int. */
942 struct type *target = max_type (builtin_type (ax->gdbarch)->builtin_int,
943 max_type (value1->type, value2->type));
945 /* Deal with value2, on the top of the stack. */
946 gen_conversion (ax, value2->type, target);
948 /* Deal with value1, not on the top of the stack. Don't
949 generate the `swap' instructions if we're not actually going
951 if (is_nontrivial_conversion (value1->type, target))
953 ax_simple (ax, aop_swap);
954 gen_conversion (ax, value1->type, target);
955 ax_simple (ax, aop_swap);
958 value1->type = value2->type = check_typedef (target);
963 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
964 the value on the top of the stack, as described by VALUE. Assume
965 the value has integral type. */
967 gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
969 const struct builtin_type *builtin = builtin_type (ax->gdbarch);
971 if (!type_wider_than (value->type, builtin->builtin_int))
973 gen_conversion (ax, value->type, builtin->builtin_int);
974 value->type = builtin->builtin_int;
976 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
978 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
979 value->type = builtin->builtin_unsigned_int;
984 /* Generate code for a cast to TYPE. */
986 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
988 /* GCC does allow casts to yield lvalues, so this should be fixed
989 before merging these changes into the trunk. */
990 require_rvalue (ax, value);
991 /* Dereference typedefs. */
992 type = check_typedef (type);
994 switch (TYPE_CODE (type))
998 case TYPE_CODE_RVALUE_REF:
999 /* It's implementation-defined, and I'll bet this is what GCC
1003 case TYPE_CODE_ARRAY:
1004 case TYPE_CODE_STRUCT:
1005 case TYPE_CODE_UNION:
1006 case TYPE_CODE_FUNC:
1007 error (_("Invalid type cast: intended type must be scalar."));
1009 case TYPE_CODE_ENUM:
1010 case TYPE_CODE_BOOL:
1011 /* We don't have to worry about the size of the value, because
1012 all our integral values are fully sign-extended, and when
1013 casting pointers we can do anything we like. Is there any
1014 way for us to know what GCC actually does with a cast like
1019 gen_conversion (ax, value->type, type);
1022 case TYPE_CODE_VOID:
1023 /* We could pop the value, and rely on everyone else to check
1024 the type and notice that this value doesn't occupy a stack
1025 slot. But for now, leave the value on the stack, and
1026 preserve the "value == stack element" assumption. */
1030 error (_("Casts to requested type are not yet implemented."));
1038 /* Generating bytecode from GDB expressions: arithmetic */
1040 /* Scale the integer on the top of the stack by the size of the target
1041 of the pointer type TYPE. */
1043 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1045 struct type *element = TYPE_TARGET_TYPE (type);
1047 if (TYPE_LENGTH (element) != 1)
1049 ax_const_l (ax, TYPE_LENGTH (element));
1055 /* Generate code for pointer arithmetic PTR + INT. */
1057 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1058 struct axs_value *value1, struct axs_value *value2)
1060 gdb_assert (pointer_type (value1->type));
1061 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1063 gen_scale (ax, aop_mul, value1->type);
1064 ax_simple (ax, aop_add);
1065 gen_extend (ax, value1->type); /* Catch overflow. */
1066 value->type = value1->type;
1067 value->kind = axs_rvalue;
1071 /* Generate code for pointer arithmetic PTR - INT. */
1073 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1074 struct axs_value *value1, struct axs_value *value2)
1076 gdb_assert (pointer_type (value1->type));
1077 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1079 gen_scale (ax, aop_mul, value1->type);
1080 ax_simple (ax, aop_sub);
1081 gen_extend (ax, value1->type); /* Catch overflow. */
1082 value->type = value1->type;
1083 value->kind = axs_rvalue;
1087 /* Generate code for pointer arithmetic PTR - PTR. */
1089 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1090 struct axs_value *value1, struct axs_value *value2,
1091 struct type *result_type)
1093 gdb_assert (pointer_type (value1->type));
1094 gdb_assert (pointer_type (value2->type));
1096 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1097 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1099 First argument of `-' is a pointer, but second argument is neither\n\
1100 an integer nor a pointer of the same type."));
1102 ax_simple (ax, aop_sub);
1103 gen_scale (ax, aop_div_unsigned, value1->type);
1104 value->type = result_type;
1105 value->kind = axs_rvalue;
1109 gen_equal (struct agent_expr *ax, struct axs_value *value,
1110 struct axs_value *value1, struct axs_value *value2,
1111 struct type *result_type)
1113 if (pointer_type (value1->type) || pointer_type (value2->type))
1114 ax_simple (ax, aop_equal);
1116 gen_binop (ax, value, value1, value2,
1117 aop_equal, aop_equal, 0, "equal");
1118 value->type = result_type;
1119 value->kind = axs_rvalue;
1123 gen_less (struct agent_expr *ax, struct axs_value *value,
1124 struct axs_value *value1, struct axs_value *value2,
1125 struct type *result_type)
1127 if (pointer_type (value1->type) || pointer_type (value2->type))
1128 ax_simple (ax, aop_less_unsigned);
1130 gen_binop (ax, value, value1, value2,
1131 aop_less_signed, aop_less_unsigned, 0, "less than");
1132 value->type = result_type;
1133 value->kind = axs_rvalue;
1136 /* Generate code for a binary operator that doesn't do pointer magic.
1137 We set VALUE to describe the result value; we assume VALUE1 and
1138 VALUE2 describe the two operands, and that they've undergone the
1139 usual binary conversions. MAY_CARRY should be non-zero iff the
1140 result needs to be extended. NAME is the English name of the
1141 operator, used in error messages */
1143 gen_binop (struct agent_expr *ax, struct axs_value *value,
1144 struct axs_value *value1, struct axs_value *value2,
1145 enum agent_op op, enum agent_op op_unsigned,
1146 int may_carry, const char *name)
1148 /* We only handle INT op INT. */
1149 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1150 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1151 error (_("Invalid combination of types in %s."), name);
1154 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1156 gen_extend (ax, value1->type); /* catch overflow */
1157 value->type = value1->type;
1158 value->kind = axs_rvalue;
1163 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1164 struct type *result_type)
1166 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1167 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1168 error (_("Invalid type of operand to `!'."));
1170 ax_simple (ax, aop_log_not);
1171 value->type = result_type;
1176 gen_complement (struct agent_expr *ax, struct axs_value *value)
1178 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1179 error (_("Invalid type of operand to `~'."));
1181 ax_simple (ax, aop_bit_not);
1182 gen_extend (ax, value->type);
1187 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1189 /* Dereference the value on the top of the stack. */
1191 gen_deref (struct axs_value *value)
1193 /* The caller should check the type, because several operators use
1194 this, and we don't know what error message to generate. */
1195 if (!pointer_type (value->type))
1196 internal_error (__FILE__, __LINE__,
1197 _("gen_deref: expected a pointer"));
1199 /* We've got an rvalue now, which is a pointer. We want to yield an
1200 lvalue, whose address is exactly that pointer. So we don't
1201 actually emit any code; we just change the type from "Pointer to
1202 T" to "T", and mark the value as an lvalue in memory. Leave it
1203 to the consumer to actually dereference it. */
1204 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1205 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1206 error (_("Attempt to dereference a generic pointer."));
1207 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1208 ? axs_rvalue : axs_lvalue_memory);
1212 /* Produce the address of the lvalue on the top of the stack. */
1214 gen_address_of (struct axs_value *value)
1216 /* Special case for taking the address of a function. The ANSI
1217 standard describes this as a special case, too, so this
1218 arrangement is not without motivation. */
1219 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1220 /* The value's already an rvalue on the stack, so we just need to
1222 value->type = lookup_pointer_type (value->type);
1224 switch (value->kind)
1227 error (_("Operand of `&' is an rvalue, which has no address."));
1229 case axs_lvalue_register:
1230 error (_("Operand of `&' is in a register, and has no address."));
1232 case axs_lvalue_memory:
1233 value->kind = axs_rvalue;
1234 value->type = lookup_pointer_type (value->type);
1239 /* Generate code to push the value of a bitfield of a structure whose
1240 address is on the top of the stack. START and END give the
1241 starting and one-past-ending *bit* numbers of the field within the
1244 gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1245 struct type *type, int start, int end)
1247 /* Note that ops[i] fetches 8 << i bits. */
1248 static enum agent_op ops[]
1249 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1250 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1252 /* We don't want to touch any byte that the bitfield doesn't
1253 actually occupy; we shouldn't make any accesses we're not
1254 explicitly permitted to. We rely here on the fact that the
1255 bytecode `ref' operators work on unaligned addresses.
1257 It takes some fancy footwork to get the stack to work the way
1258 we'd like. Say we're retrieving a bitfield that requires three
1259 fetches. Initially, the stack just contains the address:
1261 For the first fetch, we duplicate the address
1263 then add the byte offset, do the fetch, and shift and mask as
1264 needed, yielding a fragment of the value, properly aligned for
1265 the final bitwise or:
1267 then we swap, and repeat the process:
1268 frag1 addr --- address on top
1269 frag1 addr addr --- duplicate it
1270 frag1 addr frag2 --- get second fragment
1271 frag1 frag2 addr --- swap again
1272 frag1 frag2 frag3 --- get third fragment
1273 Notice that, since the third fragment is the last one, we don't
1274 bother duplicating the address this time. Now we have all the
1275 fragments on the stack, and we can simply `or' them together,
1276 yielding the final value of the bitfield. */
1278 /* The first and one-after-last bits in the field, but rounded down
1279 and up to byte boundaries. */
1280 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1281 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1285 /* current bit offset within the structure */
1288 /* The index in ops of the opcode we're considering. */
1291 /* The number of fragments we generated in the process. Probably
1292 equal to the number of `one' bits in bytesize, but who cares? */
1295 /* Dereference any typedefs. */
1296 type = check_typedef (type);
1298 /* Can we fetch the number of bits requested at all? */
1299 if ((end - start) > ((1 << num_ops) * 8))
1300 internal_error (__FILE__, __LINE__,
1301 _("gen_bitfield_ref: bitfield too wide"));
1303 /* Note that we know here that we only need to try each opcode once.
1304 That may not be true on machines with weird byte sizes. */
1305 offset = bound_start;
1307 for (op = num_ops - 1; op >= 0; op--)
1309 /* number of bits that ops[op] would fetch */
1310 int op_size = 8 << op;
1312 /* The stack at this point, from bottom to top, contains zero or
1313 more fragments, then the address. */
1315 /* Does this fetch fit within the bitfield? */
1316 if (offset + op_size <= bound_end)
1318 /* Is this the last fragment? */
1319 int last_frag = (offset + op_size == bound_end);
1322 ax_simple (ax, aop_dup); /* keep a copy of the address */
1324 /* Add the offset. */
1325 gen_offset (ax, offset / TARGET_CHAR_BIT);
1329 /* Record the area of memory we're about to fetch. */
1330 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1333 /* Perform the fetch. */
1334 ax_simple (ax, ops[op]);
1336 /* Shift the bits we have to their proper position.
1337 gen_left_shift will generate right shifts when the operand
1340 A big-endian field diagram to ponder:
1341 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1342 +------++------++------++------++------++------++------++------+
1343 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1345 bit number 16 32 48 53
1346 These are bit numbers as supplied by GDB. Note that the
1347 bit numbers run from right to left once you've fetched the
1350 A little-endian field diagram to ponder:
1351 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1352 +------++------++------++------++------++------++------++------+
1353 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1355 bit number 48 32 16 4 0
1357 In both cases, the most significant end is on the left
1358 (i.e. normal numeric writing order), which means that you
1359 don't go crazy thinking about `left' and `right' shifts.
1361 We don't have to worry about masking yet:
1362 - If they contain garbage off the least significant end, then we
1363 must be looking at the low end of the field, and the right
1364 shift will wipe them out.
1365 - If they contain garbage off the most significant end, then we
1366 must be looking at the most significant end of the word, and
1367 the sign/zero extension will wipe them out.
1368 - If we're in the interior of the word, then there is no garbage
1369 on either end, because the ref operators zero-extend. */
1370 if (gdbarch_byte_order (ax->gdbarch) == BFD_ENDIAN_BIG)
1371 gen_left_shift (ax, end - (offset + op_size));
1373 gen_left_shift (ax, offset - start);
1376 /* Bring the copy of the address up to the top. */
1377 ax_simple (ax, aop_swap);
1384 /* Generate enough bitwise `or' operations to combine all the
1385 fragments we left on the stack. */
1386 while (fragment_count-- > 1)
1387 ax_simple (ax, aop_bit_or);
1389 /* Sign- or zero-extend the value as appropriate. */
1390 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1392 /* This is *not* an lvalue. Ugh. */
1393 value->kind = axs_rvalue;
1397 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1398 is an accumulated offset (in bytes), will be nonzero for objects
1399 embedded in other objects, like C++ base classes. Behavior should
1400 generally follow value_primitive_field. */
1403 gen_primitive_field (struct agent_expr *ax, struct axs_value *value,
1404 int offset, int fieldno, struct type *type)
1406 /* Is this a bitfield? */
1407 if (TYPE_FIELD_PACKED (type, fieldno))
1408 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, fieldno),
1409 (offset * TARGET_CHAR_BIT
1410 + TYPE_FIELD_BITPOS (type, fieldno)),
1411 (offset * TARGET_CHAR_BIT
1412 + TYPE_FIELD_BITPOS (type, fieldno)
1413 + TYPE_FIELD_BITSIZE (type, fieldno)));
1416 gen_offset (ax, offset
1417 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1418 value->kind = axs_lvalue_memory;
1419 value->type = TYPE_FIELD_TYPE (type, fieldno);
1423 /* Search for the given field in either the given type or one of its
1424 base classes. Return 1 if found, 0 if not. */
1427 gen_struct_ref_recursive (struct agent_expr *ax, struct axs_value *value,
1428 const char *field, int offset, struct type *type)
1431 int nbases = TYPE_N_BASECLASSES (type);
1433 type = check_typedef (type);
1435 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1437 const char *this_name = TYPE_FIELD_NAME (type, i);
1441 if (strcmp (field, this_name) == 0)
1443 /* Note that bytecodes for the struct's base (aka
1444 "this") will have been generated already, which will
1445 be unnecessary but not harmful if the static field is
1446 being handled as a global. */
1447 if (field_is_static (&TYPE_FIELD (type, i)))
1449 gen_static_field (ax->gdbarch, ax, value, type, i);
1450 if (value->optimized_out)
1451 error (_("static field `%s' has been "
1452 "optimized out, cannot use"),
1457 gen_primitive_field (ax, value, offset, i, type);
1460 #if 0 /* is this right? */
1461 if (this_name[0] == '\0')
1462 internal_error (__FILE__, __LINE__,
1463 _("find_field: anonymous unions not supported"));
1468 /* Now scan through base classes recursively. */
1469 for (i = 0; i < nbases; i++)
1471 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1473 rslt = gen_struct_ref_recursive (ax, value, field,
1474 offset + TYPE_BASECLASS_BITPOS (type, i)
1481 /* Not found anywhere, flag so caller can complain. */
1485 /* Generate code to reference the member named FIELD of a structure or
1486 union. The top of the stack, as described by VALUE, should have
1487 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1488 the operator being compiled, and OPERAND_NAME is the kind of thing
1489 it operates on; we use them in error messages. */
1491 gen_struct_ref (struct agent_expr *ax, struct axs_value *value,
1492 const char *field, const char *operator_name,
1493 const char *operand_name)
1498 /* Follow pointers until we reach a non-pointer. These aren't the C
1499 semantics, but they're what the normal GDB evaluator does, so we
1500 should at least be consistent. */
1501 while (pointer_type (value->type))
1503 require_rvalue (ax, value);
1506 type = check_typedef (value->type);
1508 /* This must yield a structure or a union. */
1509 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1510 && TYPE_CODE (type) != TYPE_CODE_UNION)
1511 error (_("The left operand of `%s' is not a %s."),
1512 operator_name, operand_name);
1514 /* And it must be in memory; we don't deal with structure rvalues,
1515 or structures living in registers. */
1516 if (value->kind != axs_lvalue_memory)
1517 error (_("Structure does not live in memory."));
1519 /* Search through fields and base classes recursively. */
1520 found = gen_struct_ref_recursive (ax, value, field, 0, type);
1523 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1524 field, TYPE_TAG_NAME (type));
1528 gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1529 const struct type *curtype, char *name);
1531 gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1532 const struct type *curtype, char *name);
1535 gen_static_field (struct gdbarch *gdbarch,
1536 struct agent_expr *ax, struct axs_value *value,
1537 struct type *type, int fieldno)
1539 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1541 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1542 value->kind = axs_lvalue_memory;
1543 value->type = TYPE_FIELD_TYPE (type, fieldno);
1544 value->optimized_out = 0;
1548 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1549 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0).symbol;
1553 gen_var_ref (gdbarch, ax, value, sym);
1555 /* Don't error if the value was optimized out, we may be
1556 scanning all static fields and just want to pass over this
1557 and continue with the rest. */
1561 /* Silently assume this was optimized out; class printing
1562 will let the user know why the data is missing. */
1563 value->optimized_out = 1;
1569 gen_struct_elt_for_reference (struct agent_expr *ax, struct axs_value *value,
1570 struct type *type, char *fieldname)
1572 struct type *t = type;
1575 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1576 && TYPE_CODE (t) != TYPE_CODE_UNION)
1577 internal_error (__FILE__, __LINE__,
1578 _("non-aggregate type to gen_struct_elt_for_reference"));
1580 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1582 const char *t_field_name = TYPE_FIELD_NAME (t, i);
1584 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1586 if (field_is_static (&TYPE_FIELD (t, i)))
1588 gen_static_field (ax->gdbarch, ax, value, t, i);
1589 if (value->optimized_out)
1590 error (_("static field `%s' has been "
1591 "optimized out, cannot use"),
1595 if (TYPE_FIELD_PACKED (t, i))
1596 error (_("pointers to bitfield members not allowed"));
1598 /* FIXME we need a way to do "want_address" equivalent */
1600 error (_("Cannot reference non-static field \"%s\""), fieldname);
1604 /* FIXME add other scoped-reference cases here */
1606 /* Do a last-ditch lookup. */
1607 return gen_maybe_namespace_elt (ax, value, type, fieldname);
1610 /* C++: Return the member NAME of the namespace given by the type
1614 gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1615 const struct type *curtype, char *name)
1617 int found = gen_maybe_namespace_elt (ax, value, curtype, name);
1620 error (_("No symbol \"%s\" in namespace \"%s\"."),
1621 name, TYPE_TAG_NAME (curtype));
1626 /* A helper function used by value_namespace_elt and
1627 value_struct_elt_for_reference. It looks up NAME inside the
1628 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1629 is a class and NAME refers to a type in CURTYPE itself (as opposed
1630 to, say, some base class of CURTYPE). */
1633 gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1634 const struct type *curtype, char *name)
1636 const char *namespace_name = TYPE_TAG_NAME (curtype);
1637 struct block_symbol sym;
1639 sym = cp_lookup_symbol_namespace (namespace_name, name,
1640 block_for_pc (ax->scope),
1643 if (sym.symbol == NULL)
1646 gen_var_ref (ax->gdbarch, ax, value, sym.symbol);
1648 if (value->optimized_out)
1649 error (_("`%s' has been optimized out, cannot use"),
1650 SYMBOL_PRINT_NAME (sym.symbol));
1657 gen_aggregate_elt_ref (struct agent_expr *ax, struct axs_value *value,
1658 struct type *type, char *field,
1659 const char *operator_name,
1660 const char *operand_name)
1662 switch (TYPE_CODE (type))
1664 case TYPE_CODE_STRUCT:
1665 case TYPE_CODE_UNION:
1666 return gen_struct_elt_for_reference (ax, value, type, field);
1668 case TYPE_CODE_NAMESPACE:
1669 return gen_namespace_elt (ax, value, type, field);
1672 internal_error (__FILE__, __LINE__,
1673 _("non-aggregate type in gen_aggregate_elt_ref"));
1679 /* Generate code for GDB's magical `repeat' operator.
1680 LVALUE @ INT creates an array INT elements long, and whose elements
1681 have the same type as LVALUE, located in memory so that LVALUE is
1682 its first element. For example, argv[0]@argc gives you the array
1683 of command-line arguments.
1685 Unfortunately, because we have to know the types before we actually
1686 have a value for the expression, we can't implement this perfectly
1687 without changing the type system, having values that occupy two
1688 stack slots, doing weird things with sizeof, etc. So we require
1689 the right operand to be a constant expression. */
1691 gen_repeat (struct expression *exp, union exp_element **pc,
1692 struct agent_expr *ax, struct axs_value *value)
1694 struct axs_value value1;
1696 /* We don't want to turn this into an rvalue, so no conversions
1698 gen_expr (exp, pc, ax, &value1);
1699 if (value1.kind != axs_lvalue_memory)
1700 error (_("Left operand of `@' must be an object in memory."));
1702 /* Evaluate the length; it had better be a constant. */
1704 struct value *v = const_expr (pc);
1708 error (_("Right operand of `@' must be a "
1709 "constant, in agent expressions."));
1710 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1711 error (_("Right operand of `@' must be an integer."));
1712 length = value_as_long (v);
1714 error (_("Right operand of `@' must be positive."));
1716 /* The top of the stack is already the address of the object, so
1717 all we need to do is frob the type of the lvalue. */
1719 /* FIXME-type-allocation: need a way to free this type when we are
1722 = lookup_array_range_type (value1.type, 0, length - 1);
1724 value->kind = axs_lvalue_memory;
1725 value->type = array;
1731 /* Emit code for the `sizeof' operator.
1732 *PC should point at the start of the operand expression; we advance it
1733 to the first instruction after the operand. */
1735 gen_sizeof (struct expression *exp, union exp_element **pc,
1736 struct agent_expr *ax, struct axs_value *value,
1737 struct type *size_type)
1739 /* We don't care about the value of the operand expression; we only
1740 care about its type. However, in the current arrangement, the
1741 only way to find an expression's type is to generate code for it.
1742 So we generate code for the operand, and then throw it away,
1743 replacing it with code that simply pushes its size. */
1744 int start = ax->len;
1746 gen_expr (exp, pc, ax, value);
1748 /* Throw away the code we just generated. */
1751 ax_const_l (ax, TYPE_LENGTH (value->type));
1752 value->kind = axs_rvalue;
1753 value->type = size_type;
1757 /* Generating bytecode from GDB expressions: general recursive thingy */
1760 /* A gen_expr function written by a Gen-X'er guy.
1761 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1763 gen_expr (struct expression *exp, union exp_element **pc,
1764 struct agent_expr *ax, struct axs_value *value)
1766 /* Used to hold the descriptions of operand expressions. */
1767 struct axs_value value1, value2, value3;
1768 enum exp_opcode op = (*pc)[0].opcode, op2;
1769 int if1, go1, if2, go2, end;
1770 struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
1772 /* If we're looking at a constant expression, just push its value. */
1774 struct value *v = maybe_const_expr (pc);
1778 ax_const_l (ax, value_as_long (v));
1779 value->kind = axs_rvalue;
1780 value->type = check_typedef (value_type (v));
1785 /* Otherwise, go ahead and generate code for it. */
1788 /* Binary arithmetic operators. */
1796 case BINOP_SUBSCRIPT:
1797 case BINOP_BITWISE_AND:
1798 case BINOP_BITWISE_IOR:
1799 case BINOP_BITWISE_XOR:
1801 case BINOP_NOTEQUAL:
1807 gen_expr (exp, pc, ax, &value1);
1808 gen_usual_unary (ax, &value1);
1809 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1812 case BINOP_LOGICAL_AND:
1814 /* Generate the obvious sequence of tests and jumps. */
1815 gen_expr (exp, pc, ax, &value1);
1816 gen_usual_unary (ax, &value1);
1817 if1 = ax_goto (ax, aop_if_goto);
1818 go1 = ax_goto (ax, aop_goto);
1819 ax_label (ax, if1, ax->len);
1820 gen_expr (exp, pc, ax, &value2);
1821 gen_usual_unary (ax, &value2);
1822 if2 = ax_goto (ax, aop_if_goto);
1823 go2 = ax_goto (ax, aop_goto);
1824 ax_label (ax, if2, ax->len);
1826 end = ax_goto (ax, aop_goto);
1827 ax_label (ax, go1, ax->len);
1828 ax_label (ax, go2, ax->len);
1830 ax_label (ax, end, ax->len);
1831 value->kind = axs_rvalue;
1832 value->type = int_type;
1835 case BINOP_LOGICAL_OR:
1837 /* Generate the obvious sequence of tests and jumps. */
1838 gen_expr (exp, pc, ax, &value1);
1839 gen_usual_unary (ax, &value1);
1840 if1 = ax_goto (ax, aop_if_goto);
1841 gen_expr (exp, pc, ax, &value2);
1842 gen_usual_unary (ax, &value2);
1843 if2 = ax_goto (ax, aop_if_goto);
1845 end = ax_goto (ax, aop_goto);
1846 ax_label (ax, if1, ax->len);
1847 ax_label (ax, if2, ax->len);
1849 ax_label (ax, end, ax->len);
1850 value->kind = axs_rvalue;
1851 value->type = int_type;
1856 gen_expr (exp, pc, ax, &value1);
1857 gen_usual_unary (ax, &value1);
1858 /* For (A ? B : C), it's easiest to generate subexpression
1859 bytecodes in order, but if_goto jumps on true, so we invert
1860 the sense of A. Then we can do B by dropping through, and
1862 gen_logical_not (ax, &value1, int_type);
1863 if1 = ax_goto (ax, aop_if_goto);
1864 gen_expr (exp, pc, ax, &value2);
1865 gen_usual_unary (ax, &value2);
1866 end = ax_goto (ax, aop_goto);
1867 ax_label (ax, if1, ax->len);
1868 gen_expr (exp, pc, ax, &value3);
1869 gen_usual_unary (ax, &value3);
1870 ax_label (ax, end, ax->len);
1871 /* This is arbitary - what if B and C are incompatible types? */
1872 value->type = value2.type;
1873 value->kind = value2.kind;
1878 if ((*pc)[0].opcode == OP_INTERNALVAR)
1880 char *name = internalvar_name ((*pc)[1].internalvar);
1881 struct trace_state_variable *tsv;
1884 gen_expr (exp, pc, ax, value);
1885 tsv = find_trace_state_variable (name);
1888 ax_tsv (ax, aop_setv, tsv->number);
1890 ax_tsv (ax, aop_tracev, tsv->number);
1893 error (_("$%s is not a trace state variable, "
1894 "may not assign to it"), name);
1897 error (_("May only assign to trace state variables"));
1900 case BINOP_ASSIGN_MODIFY:
1902 op2 = (*pc)[0].opcode;
1905 if ((*pc)[0].opcode == OP_INTERNALVAR)
1907 char *name = internalvar_name ((*pc)[1].internalvar);
1908 struct trace_state_variable *tsv;
1911 tsv = find_trace_state_variable (name);
1914 /* The tsv will be the left half of the binary operation. */
1915 ax_tsv (ax, aop_getv, tsv->number);
1917 ax_tsv (ax, aop_tracev, tsv->number);
1918 /* Trace state variables are always 64-bit integers. */
1919 value1.kind = axs_rvalue;
1920 value1.type = builtin_type (ax->gdbarch)->builtin_long_long;
1921 /* Now do right half of expression. */
1922 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1923 /* We have a result of the binary op, set the tsv. */
1924 ax_tsv (ax, aop_setv, tsv->number);
1926 ax_tsv (ax, aop_tracev, tsv->number);
1929 error (_("$%s is not a trace state variable, "
1930 "may not assign to it"), name);
1933 error (_("May only assign to trace state variables"));
1936 /* Note that we need to be a little subtle about generating code
1937 for comma. In C, we can do some optimizations here because
1938 we know the left operand is only being evaluated for effect.
1939 However, if the tracing kludge is in effect, then we always
1940 need to evaluate the left hand side fully, so that all the
1941 variables it mentions get traced. */
1944 gen_expr (exp, pc, ax, &value1);
1945 /* Don't just dispose of the left operand. We might be tracing,
1946 in which case we want to emit code to trace it if it's an
1948 gen_traced_pop (ax->gdbarch, ax, &value1);
1949 gen_expr (exp, pc, ax, value);
1950 /* It's the consumer's responsibility to trace the right operand. */
1953 case OP_LONG: /* some integer constant */
1955 struct type *type = (*pc)[1].type;
1956 LONGEST k = (*pc)[2].longconst;
1959 gen_int_literal (ax, value, k, type);
1964 gen_var_ref (ax->gdbarch, ax, value, (*pc)[2].symbol);
1966 if (value->optimized_out)
1967 error (_("`%s' has been optimized out, cannot use"),
1968 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
1975 const char *name = &(*pc)[2].string;
1978 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1979 reg = user_reg_map_name_to_regnum (ax->gdbarch, name, strlen (name));
1981 internal_error (__FILE__, __LINE__,
1982 _("Register $%s not available"), name);
1983 /* No support for tracing user registers yet. */
1984 if (reg >= gdbarch_num_regs (ax->gdbarch)
1985 + gdbarch_num_pseudo_regs (ax->gdbarch))
1986 error (_("'%s' is a user-register; "
1987 "GDB cannot yet trace user-register contents."),
1989 value->kind = axs_lvalue_register;
1991 value->type = register_type (ax->gdbarch, reg);
1995 case OP_INTERNALVAR:
1997 struct internalvar *var = (*pc)[1].internalvar;
1998 const char *name = internalvar_name (var);
1999 struct trace_state_variable *tsv;
2002 tsv = find_trace_state_variable (name);
2005 ax_tsv (ax, aop_getv, tsv->number);
2007 ax_tsv (ax, aop_tracev, tsv->number);
2008 /* Trace state variables are always 64-bit integers. */
2009 value->kind = axs_rvalue;
2010 value->type = builtin_type (ax->gdbarch)->builtin_long_long;
2012 else if (! compile_internalvar_to_ax (var, ax, value))
2013 error (_("$%s is not a trace state variable; GDB agent "
2014 "expressions cannot use convenience variables."), name);
2018 /* Weirdo operator: see comments for gen_repeat for details. */
2020 /* Note that gen_repeat handles its own argument evaluation. */
2022 gen_repeat (exp, pc, ax, value);
2027 struct type *type = (*pc)[1].type;
2030 gen_expr (exp, pc, ax, value);
2031 gen_cast (ax, value, type);
2035 case UNOP_CAST_TYPE:
2042 offset = *pc - exp->elts;
2043 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2044 type = value_type (val);
2045 *pc = &exp->elts[offset];
2047 gen_expr (exp, pc, ax, value);
2048 gen_cast (ax, value, type);
2054 struct type *type = check_typedef ((*pc)[1].type);
2057 gen_expr (exp, pc, ax, value);
2059 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2060 already have the right value on the stack. For
2061 axs_lvalue_register, we must convert. */
2062 if (value->kind == axs_lvalue_register)
2063 require_rvalue (ax, value);
2066 value->kind = axs_lvalue_memory;
2070 case UNOP_MEMVAL_TYPE:
2077 offset = *pc - exp->elts;
2078 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2079 type = value_type (val);
2080 *pc = &exp->elts[offset];
2082 gen_expr (exp, pc, ax, value);
2084 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2085 already have the right value on the stack. For
2086 axs_lvalue_register, we must convert. */
2087 if (value->kind == axs_lvalue_register)
2088 require_rvalue (ax, value);
2091 value->kind = axs_lvalue_memory;
2097 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2098 gen_expr (exp, pc, ax, value);
2099 gen_usual_unary (ax, value);
2104 /* -FOO is equivalent to 0 - FOO. */
2105 gen_int_literal (ax, &value1, 0,
2106 builtin_type (ax->gdbarch)->builtin_int);
2107 gen_usual_unary (ax, &value1); /* shouldn't do much */
2108 gen_expr (exp, pc, ax, &value2);
2109 gen_usual_unary (ax, &value2);
2110 gen_usual_arithmetic (ax, &value1, &value2);
2111 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2114 case UNOP_LOGICAL_NOT:
2116 gen_expr (exp, pc, ax, value);
2117 gen_usual_unary (ax, value);
2118 gen_logical_not (ax, value, int_type);
2121 case UNOP_COMPLEMENT:
2123 gen_expr (exp, pc, ax, value);
2124 gen_usual_unary (ax, value);
2125 gen_integral_promotions (ax, value);
2126 gen_complement (ax, value);
2131 gen_expr (exp, pc, ax, value);
2132 gen_usual_unary (ax, value);
2133 if (!pointer_type (value->type))
2134 error (_("Argument of unary `*' is not a pointer."));
2140 gen_expr (exp, pc, ax, value);
2141 gen_address_of (value);
2146 /* Notice that gen_sizeof handles its own operand, unlike most
2147 of the other unary operator functions. This is because we
2148 have to throw away the code we generate. */
2149 gen_sizeof (exp, pc, ax, value,
2150 builtin_type (ax->gdbarch)->builtin_int);
2153 case STRUCTOP_STRUCT:
2156 int length = (*pc)[1].longconst;
2157 char *name = &(*pc)[2].string;
2159 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2160 gen_expr (exp, pc, ax, value);
2161 if (op == STRUCTOP_STRUCT)
2162 gen_struct_ref (ax, value, name, ".", "structure or union");
2163 else if (op == STRUCTOP_PTR)
2164 gen_struct_ref (ax, value, name, "->",
2165 "pointer to a structure or union");
2167 /* If this `if' chain doesn't handle it, then the case list
2168 shouldn't mention it, and we shouldn't be here. */
2169 internal_error (__FILE__, __LINE__,
2170 _("gen_expr: unhandled struct case"));
2176 struct symbol *sym, *func;
2177 const struct block *b;
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).symbol;
2186 error (_("no `%s' found"), lang->la_name_of_this);
2188 gen_var_ref (ax->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 (ax, value, type, name, "?", "??");
2207 error (_("There is no field named %s"), name);
2208 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2215 error (_("Attempt to use a type name as an expression."));
2218 error (_("Unsupported operator %s (%d) in expression."),
2219 op_name (exp, op), op);
2223 /* This handles the middle-to-right-side of code generation for binary
2224 expressions, which is shared between regular binary operations and
2225 assign-modify (+= and friends) expressions. */
2228 gen_expr_binop_rest (struct expression *exp,
2229 enum exp_opcode op, union exp_element **pc,
2230 struct agent_expr *ax, struct axs_value *value,
2231 struct axs_value *value1, struct axs_value *value2)
2233 struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
2235 gen_expr (exp, pc, ax, value2);
2236 gen_usual_unary (ax, value2);
2237 gen_usual_arithmetic (ax, value1, value2);
2241 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2242 && pointer_type (value2->type))
2244 /* Swap the values and proceed normally. */
2245 ax_simple (ax, aop_swap);
2246 gen_ptradd (ax, value, value2, value1);
2248 else if (pointer_type (value1->type)
2249 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2250 gen_ptradd (ax, value, value1, value2);
2252 gen_binop (ax, value, value1, value2,
2253 aop_add, aop_add, 1, "addition");
2256 if (pointer_type (value1->type)
2257 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2258 gen_ptrsub (ax,value, value1, value2);
2259 else if (pointer_type (value1->type)
2260 && pointer_type (value2->type))
2261 /* FIXME --- result type should be ptrdiff_t */
2262 gen_ptrdiff (ax, value, value1, value2,
2263 builtin_type (ax->gdbarch)->builtin_long);
2265 gen_binop (ax, value, value1, value2,
2266 aop_sub, aop_sub, 1, "subtraction");
2269 gen_binop (ax, value, value1, value2,
2270 aop_mul, aop_mul, 1, "multiplication");
2273 gen_binop (ax, value, value1, value2,
2274 aop_div_signed, aop_div_unsigned, 1, "division");
2277 gen_binop (ax, value, value1, value2,
2278 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2281 gen_binop (ax, value, value1, value2,
2282 aop_lsh, aop_lsh, 1, "left shift");
2285 gen_binop (ax, value, value1, value2,
2286 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2288 case BINOP_SUBSCRIPT:
2292 if (binop_types_user_defined_p (op, value1->type, value2->type))
2294 error (_("cannot subscript requested type: "
2295 "cannot call user defined functions"));
2299 /* If the user attempts to subscript something that is not
2300 an array or pointer type (like a plain int variable for
2301 example), then report this as an error. */
2302 type = check_typedef (value1->type);
2303 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2304 && TYPE_CODE (type) != TYPE_CODE_PTR)
2306 if (TYPE_NAME (type))
2307 error (_("cannot subscript something of type `%s'"),
2310 error (_("cannot subscript requested type"));
2314 if (!is_integral_type (value2->type))
2315 error (_("Argument to arithmetic operation "
2316 "not a number or boolean."));
2318 gen_ptradd (ax, value, value1, value2);
2322 case BINOP_BITWISE_AND:
2323 gen_binop (ax, value, value1, value2,
2324 aop_bit_and, aop_bit_and, 0, "bitwise and");
2327 case BINOP_BITWISE_IOR:
2328 gen_binop (ax, value, value1, value2,
2329 aop_bit_or, aop_bit_or, 0, "bitwise or");
2332 case BINOP_BITWISE_XOR:
2333 gen_binop (ax, value, value1, value2,
2334 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2338 gen_equal (ax, value, value1, value2, int_type);
2341 case BINOP_NOTEQUAL:
2342 gen_equal (ax, value, value1, value2, int_type);
2343 gen_logical_not (ax, value, int_type);
2347 gen_less (ax, value, value1, value2, int_type);
2351 ax_simple (ax, aop_swap);
2352 gen_less (ax, value, value1, value2, int_type);
2356 ax_simple (ax, aop_swap);
2357 gen_less (ax, value, value1, value2, int_type);
2358 gen_logical_not (ax, value, int_type);
2362 gen_less (ax, value, value1, value2, int_type);
2363 gen_logical_not (ax, value, int_type);
2367 /* We should only list operators in the outer case statement
2368 that we actually handle in the inner case statement. */
2369 internal_error (__FILE__, __LINE__,
2370 _("gen_expr: op case sets don't match"));
2375 /* Given a single variable and a scope, generate bytecodes to trace
2376 its value. This is for use in situations where we have only a
2377 variable's name, and no parsed expression; for instance, when the
2378 name comes from a list of local variables of a function. */
2381 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2382 struct symbol *var, int trace_string)
2384 agent_expr_up ax (new agent_expr (gdbarch, scope));
2385 struct axs_value value;
2388 ax->trace_string = trace_string;
2389 gen_var_ref (gdbarch, ax.get (), &value, var);
2391 /* If there is no actual variable to trace, flag it by returning
2392 an empty agent expression. */
2393 if (value.optimized_out)
2394 return agent_expr_up ();
2396 /* Make sure we record the final object, and get rid of it. */
2397 gen_traced_pop (gdbarch, ax.get (), &value);
2399 /* Oh, and terminate. */
2400 ax_simple (ax.get (), aop_end);
2405 /* Generating bytecode from GDB expressions: driver */
2407 /* Given a GDB expression EXPR, return bytecode to trace its value.
2408 The result will use the `trace' and `trace_quick' bytecodes to
2409 record the value of all memory touched by the expression. The
2410 caller can then use the ax_reqs function to discover which
2411 registers it relies upon. */
2414 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
2417 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2418 union exp_element *pc;
2419 struct axs_value value;
2423 ax->trace_string = trace_string;
2424 value.optimized_out = 0;
2425 gen_expr (expr, &pc, ax.get (), &value);
2427 /* Make sure we record the final object, and get rid of it. */
2428 gen_traced_pop (expr->gdbarch, ax.get (), &value);
2430 /* Oh, and terminate. */
2431 ax_simple (ax.get (), aop_end);
2436 /* Given a GDB expression EXPR, return a bytecode sequence that will
2437 evaluate and return a result. The bytecodes will do a direct
2438 evaluation, using the current data on the target, rather than
2439 recording blocks of memory and registers for later use, as
2440 gen_trace_for_expr does. The generated bytecode sequence leaves
2441 the result of expression evaluation on the top of the stack. */
2444 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2446 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2447 union exp_element *pc;
2448 struct axs_value value;
2452 value.optimized_out = 0;
2453 gen_expr (expr, &pc, ax.get (), &value);
2455 require_rvalue (ax.get (), &value);
2457 /* Oh, and terminate. */
2458 ax_simple (ax.get (), aop_end);
2464 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
2467 agent_expr_up ax (new agent_expr (gdbarch, scope));
2468 struct axs_value value;
2471 ax->trace_string = trace_string;
2473 gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope);
2475 /* Make sure we record the final object, and get rid of it. */
2476 gen_traced_pop (gdbarch, ax.get (), &value);
2478 /* Oh, and terminate. */
2479 ax_simple (ax.get (), aop_end);
2484 /* Given a collection of printf-style arguments, generate code to
2485 evaluate the arguments and pass everything to a special
2489 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2490 CORE_ADDR function, LONGEST channel,
2491 const char *format, int fmtlen,
2492 struct format_piece *frags,
2493 int nargs, struct expression **exprs)
2495 agent_expr_up ax (new agent_expr (gdbarch, scope));
2496 union exp_element *pc;
2497 struct axs_value value;
2500 /* We're computing values, not doing side effects. */
2503 /* Evaluate and push the args on the stack in reverse order,
2504 for simplicity of collecting them on the target side. */
2505 for (tem = nargs - 1; tem >= 0; --tem)
2507 pc = exprs[tem]->elts;
2508 value.optimized_out = 0;
2509 gen_expr (exprs[tem], &pc, ax.get (), &value);
2510 require_rvalue (ax.get (), &value);
2513 /* Push function and channel. */
2514 ax_const_l (ax.get (), channel);
2515 ax_const_l (ax.get (), function);
2517 /* Issue the printf bytecode proper. */
2518 ax_simple (ax.get (), aop_printf);
2519 ax_raw_byte (ax.get (), nargs);
2520 ax_string (ax.get (), format, fmtlen);
2522 /* And terminate. */
2523 ax_simple (ax.get (), aop_end);
2529 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
2532 int trace_string = 0;
2537 exp = decode_agent_options (exp, &trace_string);
2540 agent_expr_up agent;
2543 if (!eval && strcmp (arg, "$_ret") == 0)
2545 agent = gen_trace_for_return_address (pc, get_current_arch (),
2550 expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
2554 gdb_assert (trace_string == 0);
2555 agent = gen_eval_for_expr (pc, expr.get ());
2558 agent = gen_trace_for_expr (pc, expr.get (), trace_string);
2561 ax_reqs (agent.get ());
2562 ax_print (gdb_stdout, agent.get ());
2564 /* It would be nice to call ax_reqs here to gather some general info
2565 about the expression, and then print out the result. */
2571 agent_command_1 (char *exp, int eval)
2573 /* We don't deal with overlay debugging at the moment. We need to
2574 think more carefully about this. If you copy this code into
2575 another command, change the error message; the user shouldn't
2576 have to know anything about agent expressions. */
2577 if (overlay_debugging)
2578 error (_("GDB can't do agent expression translation with overlays."));
2581 error_no_arg (_("expression to translate"));
2583 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2585 struct linespec_result canonical;
2587 struct linespec_sals *iter;
2589 exp = skip_spaces (exp);
2591 event_location_up location = new_linespec_location (&exp);
2592 decode_line_full (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
2593 (struct symtab *) NULL, 0, &canonical,
2595 exp = skip_spaces (exp);
2599 exp = skip_spaces (exp);
2601 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
2605 for (i = 0; i < iter->sals.nelts; i++)
2606 agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
2610 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
2616 agent_command (char *exp, int from_tty)
2618 agent_command_1 (exp, 0);
2621 /* Parse the given expression, compile it into an agent expression
2622 that does direct evaluation, and display the resulting
2626 agent_eval_command (char *exp, int from_tty)
2628 agent_command_1 (exp, 1);
2631 /* Parse the given expression, compile it into an agent expression
2632 that does a printf, and display the resulting expression. */
2635 maint_agent_printf_command (char *exp, int from_tty)
2637 struct cleanup *old_chain = 0;
2638 struct expression *argvec[100];
2639 struct frame_info *fi = get_current_frame (); /* need current scope */
2640 const char *cmdrest;
2641 const char *format_start, *format_end;
2642 struct format_piece *fpieces;
2645 /* We don't deal with overlay debugging at the moment. We need to
2646 think more carefully about this. If you copy this code into
2647 another command, change the error message; the user shouldn't
2648 have to know anything about agent expressions. */
2649 if (overlay_debugging)
2650 error (_("GDB can't do agent expression translation with overlays."));
2653 error_no_arg (_("expression to translate"));
2657 cmdrest = skip_spaces_const (cmdrest);
2659 if (*cmdrest++ != '"')
2660 error (_("Must start with a format string."));
2662 format_start = cmdrest;
2664 fpieces = parse_format_string (&cmdrest);
2666 old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2668 format_end = cmdrest;
2670 if (*cmdrest++ != '"')
2671 error (_("Bad format string, non-terminated '\"'."));
2673 cmdrest = skip_spaces_const (cmdrest);
2675 if (*cmdrest != ',' && *cmdrest != 0)
2676 error (_("Invalid argument syntax"));
2678 if (*cmdrest == ',')
2680 cmdrest = skip_spaces_const (cmdrest);
2683 while (*cmdrest != '\0')
2688 expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2689 argvec[nargs] = expr.release ();
2692 if (*cmdrest == ',')
2694 /* else complain? */
2698 agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (),
2700 format_start, format_end - format_start,
2701 fpieces, nargs, argvec);
2702 ax_reqs (agent.get ());
2703 ax_print (gdb_stdout, agent.get ());
2705 /* It would be nice to call ax_reqs here to gather some general info
2706 about the expression, and then print out the result. */
2708 do_cleanups (old_chain);
2713 /* Initialization code. */
2715 void _initialize_ax_gdb (void);
2717 _initialize_ax_gdb (void)
2719 add_cmd ("agent", class_maintenance, agent_command,
2721 Translate an expression into remote agent bytecode for tracing.\n\
2722 Usage: maint agent [-at location,] EXPRESSION\n\
2723 If -at is given, generate remote agent bytecode for this location.\n\
2724 If not, generate remote agent bytecode for current frame pc address."),
2727 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2729 Translate an expression into remote agent bytecode for evaluation.\n\
2730 Usage: maint agent-eval [-at location,] EXPRESSION\n\
2731 If -at is given, generate remote agent bytecode for this location.\n\
2732 If not, generate remote agent bytecode for current frame pc address."),
2735 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2736 _("Translate an expression into remote "
2737 "agent bytecode for evaluation and display the bytecodes."),