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 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,
133 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
134 struct type *result_type);
135 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
136 static void gen_deref (struct agent_expr *, struct axs_value *);
137 static void gen_address_of (struct agent_expr *, struct axs_value *);
138 static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
139 struct axs_value *value,
140 struct type *type, int start, int end);
141 static void gen_primitive_field (struct expression *exp,
142 struct agent_expr *ax,
143 struct axs_value *value,
144 int offset, int fieldno, struct type *type);
145 static int gen_struct_ref_recursive (struct expression *exp,
146 struct agent_expr *ax,
147 struct axs_value *value,
148 const char *field, int offset,
150 static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
151 struct axs_value *value,
153 const char *operator_name,
154 const char *operand_name);
155 static void gen_static_field (struct gdbarch *gdbarch,
156 struct agent_expr *ax, struct axs_value *value,
157 struct type *type, int fieldno);
158 static void gen_repeat (struct expression *exp, union exp_element **pc,
159 struct agent_expr *ax, struct axs_value *value);
160 static void gen_sizeof (struct expression *exp, union exp_element **pc,
161 struct agent_expr *ax, struct axs_value *value,
162 struct type *size_type);
163 static void gen_expr_binop_rest (struct expression *exp,
164 enum exp_opcode op, union exp_element **pc,
165 struct agent_expr *ax,
166 struct axs_value *value,
167 struct axs_value *value1,
168 struct axs_value *value2);
170 static void agent_command (char *exp, int from_tty);
173 /* Detecting constant expressions. */
175 /* If the variable reference at *PC is a constant, return its value.
176 Otherwise, return zero.
178 Hey, Wally! How can a variable reference be a constant?
180 Well, Beav, this function really handles the OP_VAR_VALUE operator,
181 not specifically variable references. GDB uses OP_VAR_VALUE to
182 refer to any kind of symbolic reference: function names, enum
183 elements, and goto labels are all handled through the OP_VAR_VALUE
184 operator, even though they're constants. It makes sense given the
187 Gee, Wally, don'cha wonder sometimes if data representations that
188 subvert commonly accepted definitions of terms in favor of heavily
189 context-specific interpretations are really just a tool of the
190 programming hegemony to preserve their power and exclude the
193 static struct value *
194 const_var_ref (struct symbol *var)
196 struct type *type = SYMBOL_TYPE (var);
198 switch (SYMBOL_CLASS (var))
201 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
204 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
212 /* If the expression starting at *PC has a constant value, return it.
213 Otherwise, return zero. If we return a value, then *PC will be
214 advanced to the end of it. If we return zero, *PC could be
216 static struct value *
217 const_expr (union exp_element **pc)
219 enum exp_opcode op = (*pc)->opcode;
226 struct type *type = (*pc)[1].type;
227 LONGEST k = (*pc)[2].longconst;
230 return value_from_longest (type, k);
235 struct value *v = const_var_ref ((*pc)[2].symbol);
241 /* We could add more operators in here. */
245 v1 = const_expr (pc);
247 return value_neg (v1);
257 /* Like const_expr, but guarantee also that *PC is undisturbed if the
258 expression is not constant. */
259 static struct value *
260 maybe_const_expr (union exp_element **pc)
262 union exp_element *tentative_pc = *pc;
263 struct value *v = const_expr (&tentative_pc);
265 /* If we got a value, then update the real PC. */
273 /* Generating bytecode from GDB expressions: general assumptions */
275 /* Here are a few general assumptions made throughout the code; if you
276 want to make a change that contradicts one of these, then you'd
277 better scan things pretty thoroughly.
279 - We assume that all values occupy one stack element. For example,
280 sometimes we'll swap to get at the left argument to a binary
281 operator. If we decide that void values should occupy no stack
282 elements, or that synthetic arrays (whose size is determined at
283 run time, created by the `@' operator) should occupy two stack
284 elements (address and length), then this will cause trouble.
286 - We assume the stack elements are infinitely wide, and that we
287 don't have to worry what happens if the user requests an
288 operation that is wider than the actual interpreter's stack.
289 That is, it's up to the interpreter to handle directly all the
290 integer widths the user has access to. (Woe betide the language
293 - We don't support side effects. Thus, we don't have to worry about
294 GCC's generalized lvalues, function calls, etc.
296 - We don't support floating point. Many places where we switch on
297 some type don't bother to include cases for floating point; there
298 may be even more subtle ways this assumption exists. For
299 example, the arguments to % must be integers.
301 - We assume all subexpressions have a static, unchanging type. If
302 we tried to support convenience variables, this would be a
305 - All values on the stack should always be fully zero- or
308 (I wasn't sure whether to choose this or its opposite --- that
309 only addresses are assumed extended --- but it turns out that
310 neither convention completely eliminates spurious extend
311 operations (if everything is always extended, then you have to
312 extend after add, because it could overflow; if nothing is
313 extended, then you end up producing extends whenever you change
314 sizes), and this is simpler.) */
317 /* Scan for all static fields in the given class, including any base
318 classes, and generate tracing bytecodes for each. */
321 gen_trace_static_fields (struct gdbarch *gdbarch,
322 struct agent_expr *ax,
325 int i, nbases = TYPE_N_BASECLASSES (type);
326 struct axs_value value;
328 type = check_typedef (type);
330 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
332 if (field_is_static (&TYPE_FIELD (type, i)))
334 gen_static_field (gdbarch, ax, &value, type, i);
335 if (value.optimized_out)
339 case axs_lvalue_memory:
341 /* Initialize the TYPE_LENGTH if it is a typedef. */
342 check_typedef (value.type);
343 ax_const_l (ax, TYPE_LENGTH (value.type));
344 ax_simple (ax, aop_trace);
348 case axs_lvalue_register:
349 /* We don't actually need the register's value to be pushed,
350 just note that we need it to be collected. */
351 ax_reg_mask (ax, value.u.reg);
359 /* Now scan through base classes recursively. */
360 for (i = 0; i < nbases; i++)
362 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
364 gen_trace_static_fields (gdbarch, ax, basetype);
368 /* Trace the lvalue on the stack, if it needs it. In either case, pop
369 the value. Useful on the left side of a comma, and at the end of
370 an expression being used for tracing. */
372 gen_traced_pop (struct gdbarch *gdbarch,
373 struct agent_expr *ax, struct axs_value *value)
375 int string_trace = 0;
377 && TYPE_CODE (value->type) == TYPE_CODE_PTR
378 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
388 ax_const_l (ax, ax->trace_string);
389 ax_simple (ax, aop_tracenz);
392 /* We don't trace rvalues, just the lvalues necessary to
393 produce them. So just dispose of this value. */
394 ax_simple (ax, aop_pop);
397 case axs_lvalue_memory:
399 /* Initialize the TYPE_LENGTH if it is a typedef. */
400 check_typedef (value->type);
404 gen_fetch (ax, value->type);
405 ax_const_l (ax, ax->trace_string);
406 ax_simple (ax, aop_tracenz);
410 /* There's no point in trying to use a trace_quick bytecode
411 here, since "trace_quick SIZE pop" is three bytes, whereas
412 "const8 SIZE trace" is also three bytes, does the same
413 thing, and the simplest code which generates that will also
414 work correctly for objects with large sizes. */
415 ax_const_l (ax, TYPE_LENGTH (value->type));
416 ax_simple (ax, aop_trace);
421 case axs_lvalue_register:
422 /* We don't actually need the register's value to be on the
423 stack, and the target will get heartburn if the register is
424 larger than will fit in a stack, so just mark it for
425 collection and be done with it. */
426 ax_reg_mask (ax, value->u.reg);
428 /* But if the register points to a string, assume the value
429 will fit on the stack and push it anyway. */
432 ax_reg (ax, value->u.reg);
433 ax_const_l (ax, ax->trace_string);
434 ax_simple (ax, aop_tracenz);
439 /* If we're not tracing, just pop the value. */
440 ax_simple (ax, aop_pop);
442 /* To trace C++ classes with static fields stored elsewhere. */
444 && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
445 || TYPE_CODE (value->type) == TYPE_CODE_UNION))
446 gen_trace_static_fields (gdbarch, ax, value->type);
451 /* Generating bytecode from GDB expressions: helper functions */
453 /* Assume that the lower bits of the top of the stack is a value of
454 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
456 gen_sign_extend (struct agent_expr *ax, struct type *type)
458 /* Do we need to sign-extend this? */
459 if (!TYPE_UNSIGNED (type))
460 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
464 /* Assume the lower bits of the top of the stack hold a value of type
465 TYPE, and the upper bits are garbage. Sign-extend or truncate as
468 gen_extend (struct agent_expr *ax, struct type *type)
470 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
473 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
477 /* Assume that the top of the stack contains a value of type "pointer
478 to TYPE"; generate code to fetch its value. Note that TYPE is the
479 target type, not the pointer type. */
481 gen_fetch (struct agent_expr *ax, struct type *type)
485 /* Record the area of memory we're about to fetch. */
486 ax_trace_quick (ax, TYPE_LENGTH (type));
489 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
490 type = TYPE_TARGET_TYPE (type);
492 switch (TYPE_CODE (type))
496 case TYPE_CODE_RVALUE_REF:
501 /* It's a scalar value, so we know how to dereference it. How
502 many bytes long is it? */
503 switch (TYPE_LENGTH (type))
505 case 8 / TARGET_CHAR_BIT:
506 ax_simple (ax, aop_ref8);
508 case 16 / TARGET_CHAR_BIT:
509 ax_simple (ax, aop_ref16);
511 case 32 / TARGET_CHAR_BIT:
512 ax_simple (ax, aop_ref32);
514 case 64 / TARGET_CHAR_BIT:
515 ax_simple (ax, aop_ref64);
518 /* Either our caller shouldn't have asked us to dereference
519 that pointer (other code's fault), or we're not
520 implementing something we should be (this code's fault).
521 In any case, it's a bug the user shouldn't see. */
523 internal_error (__FILE__, __LINE__,
524 _("gen_fetch: strange size"));
527 gen_sign_extend (ax, type);
531 /* Our caller requested us to dereference a pointer from an unsupported
532 type. Error out and give callers a chance to handle the failure
534 error (_("gen_fetch: Unsupported type code `%s'."),
540 /* Generate code to left shift the top of the stack by DISTANCE bits, or
541 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
542 unsigned (logical) right shifts. */
544 gen_left_shift (struct agent_expr *ax, int distance)
548 ax_const_l (ax, distance);
549 ax_simple (ax, aop_lsh);
551 else if (distance < 0)
553 ax_const_l (ax, -distance);
554 ax_simple (ax, aop_rsh_unsigned);
560 /* Generating bytecode from GDB expressions: symbol references */
562 /* Generate code to push the base address of the argument portion of
563 the top stack frame. */
565 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
568 LONGEST frame_offset;
570 gdbarch_virtual_frame_pointer (gdbarch,
571 ax->scope, &frame_reg, &frame_offset);
572 ax_reg (ax, frame_reg);
573 gen_offset (ax, frame_offset);
577 /* Generate code to push the base address of the locals portion of the
580 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
583 LONGEST frame_offset;
585 gdbarch_virtual_frame_pointer (gdbarch,
586 ax->scope, &frame_reg, &frame_offset);
587 ax_reg (ax, frame_reg);
588 gen_offset (ax, frame_offset);
592 /* Generate code to add OFFSET to the top of the stack. Try to
593 generate short and readable code. We use this for getting to
594 variables on the stack, and structure members. If we were
595 programming in ML, it would be clearer why these are the same
598 gen_offset (struct agent_expr *ax, int offset)
600 /* It would suffice to simply push the offset and add it, but this
601 makes it easier to read positive and negative offsets in the
605 ax_const_l (ax, offset);
606 ax_simple (ax, aop_add);
610 ax_const_l (ax, -offset);
611 ax_simple (ax, aop_sub);
616 /* In many cases, a symbol's value is the offset from some other
617 address (stack frame, base register, etc.) Generate code to add
618 VAR's value to the top of the stack. */
620 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
622 gen_offset (ax, SYMBOL_VALUE (var));
626 /* Generate code for a variable reference to AX. The variable is the
627 symbol VAR. Set VALUE to describe the result. */
630 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
631 struct axs_value *value, struct symbol *var)
633 /* Dereference any typedefs. */
634 value->type = check_typedef (SYMBOL_TYPE (var));
635 value->optimized_out = 0;
637 if (SYMBOL_COMPUTED_OPS (var) != NULL)
639 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
643 /* I'm imitating the code in read_var_value. */
644 switch (SYMBOL_CLASS (var))
646 case LOC_CONST: /* A constant, like an enum value. */
647 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
648 value->kind = axs_rvalue;
651 case LOC_LABEL: /* A goto label, being used as a value. */
652 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
653 value->kind = axs_rvalue;
656 case LOC_CONST_BYTES:
657 internal_error (__FILE__, __LINE__,
658 _("gen_var_ref: LOC_CONST_BYTES "
659 "symbols are not supported"));
661 /* Variable at a fixed location in memory. Easy. */
663 /* Push the address of the variable. */
664 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
665 value->kind = axs_lvalue_memory;
668 case LOC_ARG: /* var lives in argument area of frame */
669 gen_frame_args_address (gdbarch, ax);
670 gen_sym_offset (ax, var);
671 value->kind = axs_lvalue_memory;
674 case LOC_REF_ARG: /* As above, but the frame slot really
675 holds the address of the variable. */
676 gen_frame_args_address (gdbarch, ax);
677 gen_sym_offset (ax, var);
678 /* Don't assume any particular pointer size. */
679 gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
680 value->kind = axs_lvalue_memory;
683 case LOC_LOCAL: /* var lives in locals area of frame */
684 gen_frame_locals_address (gdbarch, ax);
685 gen_sym_offset (ax, var);
686 value->kind = axs_lvalue_memory;
690 error (_("Cannot compute value of typedef `%s'."),
691 SYMBOL_PRINT_NAME (var));
695 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
696 value->kind = axs_rvalue;
700 /* Don't generate any code at all; in the process of treating
701 this as an lvalue or rvalue, the caller will generate the
703 value->kind = axs_lvalue_register;
704 value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
707 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
708 register, not on the stack. Simpler than LOC_REGISTER
709 because it's just like any other case where the thing
710 has a real address. */
711 case LOC_REGPARM_ADDR:
712 ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
713 value->kind = axs_lvalue_memory;
718 struct bound_minimal_symbol msym
719 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
722 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
724 /* Push the address of the variable. */
725 ax_const_l (ax, BMSYMBOL_VALUE_ADDRESS (msym));
726 value->kind = axs_lvalue_memory;
731 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
733 case LOC_OPTIMIZED_OUT:
734 /* Flag this, but don't say anything; leave it up to callers to
736 value->optimized_out = 1;
740 error (_("Cannot find value of botched symbol `%s'."),
741 SYMBOL_PRINT_NAME (var));
748 /* Generating bytecode from GDB expressions: literals */
751 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
755 value->kind = axs_rvalue;
756 value->type = check_typedef (type);
761 /* Generating bytecode from GDB expressions: unary conversions, casts */
763 /* Take what's on the top of the stack (as described by VALUE), and
764 try to make an rvalue out of it. Signal an error if we can't do
767 require_rvalue (struct agent_expr *ax, struct axs_value *value)
769 /* Only deal with scalars, structs and such may be too large
770 to fit in a stack entry. */
771 value->type = check_typedef (value->type);
772 if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
773 || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
774 || TYPE_CODE (value->type) == TYPE_CODE_UNION
775 || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
776 error (_("Value not scalar: cannot be an rvalue."));
781 /* It's already an rvalue. */
784 case axs_lvalue_memory:
785 /* The top of stack is the address of the object. Dereference. */
786 gen_fetch (ax, value->type);
789 case axs_lvalue_register:
790 /* There's nothing on the stack, but value->u.reg is the
791 register number containing the value.
793 When we add floating-point support, this is going to have to
794 change. What about SPARC register pairs, for example? */
795 ax_reg (ax, value->u.reg);
796 gen_extend (ax, value->type);
800 value->kind = axs_rvalue;
804 /* Assume the top of the stack is described by VALUE, and perform the
805 usual unary conversions. This is motivated by ANSI 6.2.2, but of
806 course GDB expressions are not ANSI; they're the mishmash union of
807 a bunch of languages. Rah.
809 NOTE! This function promises to produce an rvalue only when the
810 incoming value is of an appropriate type. In other words, the
811 consumer of the value this function produces may assume the value
812 is an rvalue only after checking its type.
814 The immediate issue is that if the user tries to use a structure or
815 union as an operand of, say, the `+' operator, we don't want to try
816 to convert that structure to an rvalue; require_rvalue will bomb on
817 structs and unions. Rather, we want to simply pass the struct
818 lvalue through unchanged, and let `+' raise an error. */
821 gen_usual_unary (struct expression *exp, struct agent_expr *ax,
822 struct axs_value *value)
824 /* We don't have to generate any code for the usual integral
825 conversions, since values are always represented as full-width on
826 the stack. Should we tweak the type? */
828 /* Some types require special handling. */
829 switch (TYPE_CODE (value->type))
831 /* Functions get converted to a pointer to the function. */
833 value->type = lookup_pointer_type (value->type);
834 value->kind = axs_rvalue; /* Should always be true, but just in case. */
837 /* Arrays get converted to a pointer to their first element, and
838 are no longer an lvalue. */
839 case TYPE_CODE_ARRAY:
841 struct type *elements = TYPE_TARGET_TYPE (value->type);
843 value->type = lookup_pointer_type (elements);
844 value->kind = axs_rvalue;
845 /* We don't need to generate any code; the address of the array
846 is also the address of its first element. */
850 /* Don't try to convert structures and unions to rvalues. Let the
851 consumer signal an error. */
852 case TYPE_CODE_STRUCT:
853 case TYPE_CODE_UNION:
857 /* If the value is an lvalue, dereference it. */
858 require_rvalue (ax, value);
862 /* Return non-zero iff the type TYPE1 is considered "wider" than the
863 type TYPE2, according to the rules described in gen_usual_arithmetic. */
865 type_wider_than (struct type *type1, struct type *type2)
867 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
868 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
869 && TYPE_UNSIGNED (type1)
870 && !TYPE_UNSIGNED (type2)));
874 /* Return the "wider" of the two types TYPE1 and TYPE2. */
876 max_type (struct type *type1, struct type *type2)
878 return type_wider_than (type1, type2) ? type1 : type2;
882 /* Generate code to convert a scalar value of type FROM to type TO. */
884 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
886 /* Perhaps there is a more graceful way to state these rules. */
888 /* If we're converting to a narrower type, then we need to clear out
890 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
893 /* If the two values have equal width, but different signednesses,
894 then we need to extend. */
895 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
897 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
901 /* If we're converting to a wider type, and becoming unsigned, then
902 we need to zero out any possible sign bits. */
903 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
905 if (TYPE_UNSIGNED (to))
911 /* Return non-zero iff the type FROM will require any bytecodes to be
912 emitted to be converted to the type TO. */
914 is_nontrivial_conversion (struct type *from, struct type *to)
916 agent_expr_up ax (new agent_expr (NULL, 0));
919 /* Actually generate the code, and see if anything came out. At the
920 moment, it would be trivial to replicate the code in
921 gen_conversion here, but in the future, when we're supporting
922 floating point and the like, it may not be. Doing things this
923 way allows this function to be independent of the logic in
925 gen_conversion (ax.get (), from, to);
926 nontrivial = ax->len > 0;
931 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
932 6.2.1.5) for the two operands of an arithmetic operator. This
933 effectively finds a "least upper bound" type for the two arguments,
934 and promotes each argument to that type. *VALUE1 and *VALUE2
935 describe the values as they are passed in, and as they are left. */
937 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
938 struct axs_value *value1, struct axs_value *value2)
940 /* Do the usual binary conversions. */
941 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
942 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
944 /* The ANSI integral promotions seem to work this way: Order the
945 integer types by size, and then by signedness: an n-bit
946 unsigned type is considered "wider" than an n-bit signed
947 type. Promote to the "wider" of the two types, and always
948 promote at least to int. */
949 struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
950 max_type (value1->type, value2->type));
952 /* Deal with value2, on the top of the stack. */
953 gen_conversion (ax, value2->type, target);
955 /* Deal with value1, not on the top of the stack. Don't
956 generate the `swap' instructions if we're not actually going
958 if (is_nontrivial_conversion (value1->type, target))
960 ax_simple (ax, aop_swap);
961 gen_conversion (ax, value1->type, target);
962 ax_simple (ax, aop_swap);
965 value1->type = value2->type = check_typedef (target);
970 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
971 the value on the top of the stack, as described by VALUE. Assume
972 the value has integral type. */
974 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
975 struct axs_value *value)
977 const struct builtin_type *builtin = builtin_type (exp->gdbarch);
979 if (!type_wider_than (value->type, builtin->builtin_int))
981 gen_conversion (ax, value->type, builtin->builtin_int);
982 value->type = builtin->builtin_int;
984 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
986 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
987 value->type = builtin->builtin_unsigned_int;
992 /* Generate code for a cast to TYPE. */
994 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
996 /* GCC does allow casts to yield lvalues, so this should be fixed
997 before merging these changes into the trunk. */
998 require_rvalue (ax, value);
999 /* Dereference typedefs. */
1000 type = check_typedef (type);
1002 switch (TYPE_CODE (type))
1006 case TYPE_CODE_RVALUE_REF:
1007 /* It's implementation-defined, and I'll bet this is what GCC
1011 case TYPE_CODE_ARRAY:
1012 case TYPE_CODE_STRUCT:
1013 case TYPE_CODE_UNION:
1014 case TYPE_CODE_FUNC:
1015 error (_("Invalid type cast: intended type must be scalar."));
1017 case TYPE_CODE_ENUM:
1018 case TYPE_CODE_BOOL:
1019 /* We don't have to worry about the size of the value, because
1020 all our integral values are fully sign-extended, and when
1021 casting pointers we can do anything we like. Is there any
1022 way for us to know what GCC actually does with a cast like
1027 gen_conversion (ax, value->type, type);
1030 case TYPE_CODE_VOID:
1031 /* We could pop the value, and rely on everyone else to check
1032 the type and notice that this value doesn't occupy a stack
1033 slot. But for now, leave the value on the stack, and
1034 preserve the "value == stack element" assumption. */
1038 error (_("Casts to requested type are not yet implemented."));
1046 /* Generating bytecode from GDB expressions: arithmetic */
1048 /* Scale the integer on the top of the stack by the size of the target
1049 of the pointer type TYPE. */
1051 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1053 struct type *element = TYPE_TARGET_TYPE (type);
1055 if (TYPE_LENGTH (element) != 1)
1057 ax_const_l (ax, TYPE_LENGTH (element));
1063 /* Generate code for pointer arithmetic PTR + INT. */
1065 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1066 struct axs_value *value1, struct axs_value *value2)
1068 gdb_assert (pointer_type (value1->type));
1069 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1071 gen_scale (ax, aop_mul, value1->type);
1072 ax_simple (ax, aop_add);
1073 gen_extend (ax, value1->type); /* Catch overflow. */
1074 value->type = value1->type;
1075 value->kind = axs_rvalue;
1079 /* Generate code for pointer arithmetic PTR - INT. */
1081 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1082 struct axs_value *value1, struct axs_value *value2)
1084 gdb_assert (pointer_type (value1->type));
1085 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1087 gen_scale (ax, aop_mul, value1->type);
1088 ax_simple (ax, aop_sub);
1089 gen_extend (ax, value1->type); /* Catch overflow. */
1090 value->type = value1->type;
1091 value->kind = axs_rvalue;
1095 /* Generate code for pointer arithmetic PTR - PTR. */
1097 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1098 struct axs_value *value1, struct axs_value *value2,
1099 struct type *result_type)
1101 gdb_assert (pointer_type (value1->type));
1102 gdb_assert (pointer_type (value2->type));
1104 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1105 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1107 First argument of `-' is a pointer, but second argument is neither\n\
1108 an integer nor a pointer of the same type."));
1110 ax_simple (ax, aop_sub);
1111 gen_scale (ax, aop_div_unsigned, value1->type);
1112 value->type = result_type;
1113 value->kind = axs_rvalue;
1117 gen_equal (struct agent_expr *ax, struct axs_value *value,
1118 struct axs_value *value1, struct axs_value *value2,
1119 struct type *result_type)
1121 if (pointer_type (value1->type) || pointer_type (value2->type))
1122 ax_simple (ax, aop_equal);
1124 gen_binop (ax, value, value1, value2,
1125 aop_equal, aop_equal, 0, "equal");
1126 value->type = result_type;
1127 value->kind = axs_rvalue;
1131 gen_less (struct agent_expr *ax, struct axs_value *value,
1132 struct axs_value *value1, struct axs_value *value2,
1133 struct type *result_type)
1135 if (pointer_type (value1->type) || pointer_type (value2->type))
1136 ax_simple (ax, aop_less_unsigned);
1138 gen_binop (ax, value, value1, value2,
1139 aop_less_signed, aop_less_unsigned, 0, "less than");
1140 value->type = result_type;
1141 value->kind = axs_rvalue;
1144 /* Generate code for a binary operator that doesn't do pointer magic.
1145 We set VALUE to describe the result value; we assume VALUE1 and
1146 VALUE2 describe the two operands, and that they've undergone the
1147 usual binary conversions. MAY_CARRY should be non-zero iff the
1148 result needs to be extended. NAME is the English name of the
1149 operator, used in error messages */
1151 gen_binop (struct agent_expr *ax, struct axs_value *value,
1152 struct axs_value *value1, struct axs_value *value2,
1153 enum agent_op op, enum agent_op op_unsigned,
1154 int may_carry, const char *name)
1156 /* We only handle INT op INT. */
1157 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1158 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1159 error (_("Invalid combination of types in %s."), name);
1162 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1164 gen_extend (ax, value1->type); /* catch overflow */
1165 value->type = value1->type;
1166 value->kind = axs_rvalue;
1171 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1172 struct type *result_type)
1174 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1175 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1176 error (_("Invalid type of operand to `!'."));
1178 ax_simple (ax, aop_log_not);
1179 value->type = result_type;
1184 gen_complement (struct agent_expr *ax, struct axs_value *value)
1186 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1187 error (_("Invalid type of operand to `~'."));
1189 ax_simple (ax, aop_bit_not);
1190 gen_extend (ax, value->type);
1195 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1197 /* Dereference the value on the top of the stack. */
1199 gen_deref (struct agent_expr *ax, struct axs_value *value)
1201 /* The caller should check the type, because several operators use
1202 this, and we don't know what error message to generate. */
1203 if (!pointer_type (value->type))
1204 internal_error (__FILE__, __LINE__,
1205 _("gen_deref: expected a pointer"));
1207 /* We've got an rvalue now, which is a pointer. We want to yield an
1208 lvalue, whose address is exactly that pointer. So we don't
1209 actually emit any code; we just change the type from "Pointer to
1210 T" to "T", and mark the value as an lvalue in memory. Leave it
1211 to the consumer to actually dereference it. */
1212 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1213 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1214 error (_("Attempt to dereference a generic pointer."));
1215 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1216 ? axs_rvalue : axs_lvalue_memory);
1220 /* Produce the address of the lvalue on the top of the stack. */
1222 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1224 /* Special case for taking the address of a function. The ANSI
1225 standard describes this as a special case, too, so this
1226 arrangement is not without motivation. */
1227 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1228 /* The value's already an rvalue on the stack, so we just need to
1230 value->type = lookup_pointer_type (value->type);
1232 switch (value->kind)
1235 error (_("Operand of `&' is an rvalue, which has no address."));
1237 case axs_lvalue_register:
1238 error (_("Operand of `&' is in a register, and has no address."));
1240 case axs_lvalue_memory:
1241 value->kind = axs_rvalue;
1242 value->type = lookup_pointer_type (value->type);
1247 /* Generate code to push the value of a bitfield of a structure whose
1248 address is on the top of the stack. START and END give the
1249 starting and one-past-ending *bit* numbers of the field within the
1252 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1253 struct axs_value *value, struct type *type,
1256 /* Note that ops[i] fetches 8 << i bits. */
1257 static enum agent_op ops[]
1258 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1259 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1261 /* We don't want to touch any byte that the bitfield doesn't
1262 actually occupy; we shouldn't make any accesses we're not
1263 explicitly permitted to. We rely here on the fact that the
1264 bytecode `ref' operators work on unaligned addresses.
1266 It takes some fancy footwork to get the stack to work the way
1267 we'd like. Say we're retrieving a bitfield that requires three
1268 fetches. Initially, the stack just contains the address:
1270 For the first fetch, we duplicate the address
1272 then add the byte offset, do the fetch, and shift and mask as
1273 needed, yielding a fragment of the value, properly aligned for
1274 the final bitwise or:
1276 then we swap, and repeat the process:
1277 frag1 addr --- address on top
1278 frag1 addr addr --- duplicate it
1279 frag1 addr frag2 --- get second fragment
1280 frag1 frag2 addr --- swap again
1281 frag1 frag2 frag3 --- get third fragment
1282 Notice that, since the third fragment is the last one, we don't
1283 bother duplicating the address this time. Now we have all the
1284 fragments on the stack, and we can simply `or' them together,
1285 yielding the final value of the bitfield. */
1287 /* The first and one-after-last bits in the field, but rounded down
1288 and up to byte boundaries. */
1289 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1290 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1294 /* current bit offset within the structure */
1297 /* The index in ops of the opcode we're considering. */
1300 /* The number of fragments we generated in the process. Probably
1301 equal to the number of `one' bits in bytesize, but who cares? */
1304 /* Dereference any typedefs. */
1305 type = check_typedef (type);
1307 /* Can we fetch the number of bits requested at all? */
1308 if ((end - start) > ((1 << num_ops) * 8))
1309 internal_error (__FILE__, __LINE__,
1310 _("gen_bitfield_ref: bitfield too wide"));
1312 /* Note that we know here that we only need to try each opcode once.
1313 That may not be true on machines with weird byte sizes. */
1314 offset = bound_start;
1316 for (op = num_ops - 1; op >= 0; op--)
1318 /* number of bits that ops[op] would fetch */
1319 int op_size = 8 << op;
1321 /* The stack at this point, from bottom to top, contains zero or
1322 more fragments, then the address. */
1324 /* Does this fetch fit within the bitfield? */
1325 if (offset + op_size <= bound_end)
1327 /* Is this the last fragment? */
1328 int last_frag = (offset + op_size == bound_end);
1331 ax_simple (ax, aop_dup); /* keep a copy of the address */
1333 /* Add the offset. */
1334 gen_offset (ax, offset / TARGET_CHAR_BIT);
1338 /* Record the area of memory we're about to fetch. */
1339 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1342 /* Perform the fetch. */
1343 ax_simple (ax, ops[op]);
1345 /* Shift the bits we have to their proper position.
1346 gen_left_shift will generate right shifts when the operand
1349 A big-endian field diagram to ponder:
1350 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1351 +------++------++------++------++------++------++------++------+
1352 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1354 bit number 16 32 48 53
1355 These are bit numbers as supplied by GDB. Note that the
1356 bit numbers run from right to left once you've fetched the
1359 A little-endian field diagram to ponder:
1360 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1361 +------++------++------++------++------++------++------++------+
1362 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1364 bit number 48 32 16 4 0
1366 In both cases, the most significant end is on the left
1367 (i.e. normal numeric writing order), which means that you
1368 don't go crazy thinking about `left' and `right' shifts.
1370 We don't have to worry about masking yet:
1371 - If they contain garbage off the least significant end, then we
1372 must be looking at the low end of the field, and the right
1373 shift will wipe them out.
1374 - If they contain garbage off the most significant end, then we
1375 must be looking at the most significant end of the word, and
1376 the sign/zero extension will wipe them out.
1377 - If we're in the interior of the word, then there is no garbage
1378 on either end, because the ref operators zero-extend. */
1379 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1380 gen_left_shift (ax, end - (offset + op_size));
1382 gen_left_shift (ax, offset - start);
1385 /* Bring the copy of the address up to the top. */
1386 ax_simple (ax, aop_swap);
1393 /* Generate enough bitwise `or' operations to combine all the
1394 fragments we left on the stack. */
1395 while (fragment_count-- > 1)
1396 ax_simple (ax, aop_bit_or);
1398 /* Sign- or zero-extend the value as appropriate. */
1399 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1401 /* This is *not* an lvalue. Ugh. */
1402 value->kind = axs_rvalue;
1406 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1407 is an accumulated offset (in bytes), will be nonzero for objects
1408 embedded in other objects, like C++ base classes. Behavior should
1409 generally follow value_primitive_field. */
1412 gen_primitive_field (struct expression *exp,
1413 struct agent_expr *ax, struct axs_value *value,
1414 int offset, int fieldno, struct type *type)
1416 /* Is this a bitfield? */
1417 if (TYPE_FIELD_PACKED (type, fieldno))
1418 gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1419 (offset * TARGET_CHAR_BIT
1420 + TYPE_FIELD_BITPOS (type, fieldno)),
1421 (offset * TARGET_CHAR_BIT
1422 + TYPE_FIELD_BITPOS (type, fieldno)
1423 + TYPE_FIELD_BITSIZE (type, fieldno)));
1426 gen_offset (ax, offset
1427 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1428 value->kind = axs_lvalue_memory;
1429 value->type = TYPE_FIELD_TYPE (type, fieldno);
1433 /* Search for the given field in either the given type or one of its
1434 base classes. Return 1 if found, 0 if not. */
1437 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1438 struct axs_value *value,
1439 const char *field, int offset, struct type *type)
1442 int nbases = TYPE_N_BASECLASSES (type);
1444 type = check_typedef (type);
1446 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1448 const char *this_name = TYPE_FIELD_NAME (type, i);
1452 if (strcmp (field, this_name) == 0)
1454 /* Note that bytecodes for the struct's base (aka
1455 "this") will have been generated already, which will
1456 be unnecessary but not harmful if the static field is
1457 being handled as a global. */
1458 if (field_is_static (&TYPE_FIELD (type, i)))
1460 gen_static_field (exp->gdbarch, ax, value, type, i);
1461 if (value->optimized_out)
1462 error (_("static field `%s' has been "
1463 "optimized out, cannot use"),
1468 gen_primitive_field (exp, ax, value, offset, i, type);
1471 #if 0 /* is this right? */
1472 if (this_name[0] == '\0')
1473 internal_error (__FILE__, __LINE__,
1474 _("find_field: anonymous unions not supported"));
1479 /* Now scan through base classes recursively. */
1480 for (i = 0; i < nbases; i++)
1482 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1484 rslt = gen_struct_ref_recursive (exp, ax, value, field,
1485 offset + TYPE_BASECLASS_BITPOS (type, i)
1492 /* Not found anywhere, flag so caller can complain. */
1496 /* Generate code to reference the member named FIELD of a structure or
1497 union. The top of the stack, as described by VALUE, should have
1498 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1499 the operator being compiled, and OPERAND_NAME is the kind of thing
1500 it operates on; we use them in error messages. */
1502 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1503 struct axs_value *value, const char *field,
1504 const char *operator_name, const char *operand_name)
1509 /* Follow pointers until we reach a non-pointer. These aren't the C
1510 semantics, but they're what the normal GDB evaluator does, so we
1511 should at least be consistent. */
1512 while (pointer_type (value->type))
1514 require_rvalue (ax, value);
1515 gen_deref (ax, value);
1517 type = check_typedef (value->type);
1519 /* This must yield a structure or a union. */
1520 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1521 && TYPE_CODE (type) != TYPE_CODE_UNION)
1522 error (_("The left operand of `%s' is not a %s."),
1523 operator_name, operand_name);
1525 /* And it must be in memory; we don't deal with structure rvalues,
1526 or structures living in registers. */
1527 if (value->kind != axs_lvalue_memory)
1528 error (_("Structure does not live in memory."));
1530 /* Search through fields and base classes recursively. */
1531 found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1534 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1535 field, TYPE_TAG_NAME (type));
1539 gen_namespace_elt (struct expression *exp,
1540 struct agent_expr *ax, struct axs_value *value,
1541 const struct type *curtype, char *name);
1543 gen_maybe_namespace_elt (struct expression *exp,
1544 struct agent_expr *ax, struct axs_value *value,
1545 const struct type *curtype, char *name);
1548 gen_static_field (struct gdbarch *gdbarch,
1549 struct agent_expr *ax, struct axs_value *value,
1550 struct type *type, int fieldno)
1552 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1554 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1555 value->kind = axs_lvalue_memory;
1556 value->type = TYPE_FIELD_TYPE (type, fieldno);
1557 value->optimized_out = 0;
1561 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1562 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0).symbol;
1566 gen_var_ref (gdbarch, ax, value, sym);
1568 /* Don't error if the value was optimized out, we may be
1569 scanning all static fields and just want to pass over this
1570 and continue with the rest. */
1574 /* Silently assume this was optimized out; class printing
1575 will let the user know why the data is missing. */
1576 value->optimized_out = 1;
1582 gen_struct_elt_for_reference (struct expression *exp,
1583 struct agent_expr *ax, struct axs_value *value,
1584 struct type *type, char *fieldname)
1586 struct type *t = type;
1589 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1590 && TYPE_CODE (t) != TYPE_CODE_UNION)
1591 internal_error (__FILE__, __LINE__,
1592 _("non-aggregate type to gen_struct_elt_for_reference"));
1594 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1596 const char *t_field_name = TYPE_FIELD_NAME (t, i);
1598 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1600 if (field_is_static (&TYPE_FIELD (t, i)))
1602 gen_static_field (exp->gdbarch, ax, value, t, i);
1603 if (value->optimized_out)
1604 error (_("static field `%s' has been "
1605 "optimized out, cannot use"),
1609 if (TYPE_FIELD_PACKED (t, i))
1610 error (_("pointers to bitfield members not allowed"));
1612 /* FIXME we need a way to do "want_address" equivalent */
1614 error (_("Cannot reference non-static field \"%s\""), fieldname);
1618 /* FIXME add other scoped-reference cases here */
1620 /* Do a last-ditch lookup. */
1621 return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1624 /* C++: Return the member NAME of the namespace given by the type
1628 gen_namespace_elt (struct expression *exp,
1629 struct agent_expr *ax, struct axs_value *value,
1630 const struct type *curtype, char *name)
1632 int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1635 error (_("No symbol \"%s\" in namespace \"%s\"."),
1636 name, TYPE_TAG_NAME (curtype));
1641 /* A helper function used by value_namespace_elt and
1642 value_struct_elt_for_reference. It looks up NAME inside the
1643 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1644 is a class and NAME refers to a type in CURTYPE itself (as opposed
1645 to, say, some base class of CURTYPE). */
1648 gen_maybe_namespace_elt (struct expression *exp,
1649 struct agent_expr *ax, struct axs_value *value,
1650 const struct type *curtype, char *name)
1652 const char *namespace_name = TYPE_TAG_NAME (curtype);
1653 struct block_symbol sym;
1655 sym = cp_lookup_symbol_namespace (namespace_name, name,
1656 block_for_pc (ax->scope),
1659 if (sym.symbol == NULL)
1662 gen_var_ref (exp->gdbarch, ax, value, sym.symbol);
1664 if (value->optimized_out)
1665 error (_("`%s' has been optimized out, cannot use"),
1666 SYMBOL_PRINT_NAME (sym.symbol));
1673 gen_aggregate_elt_ref (struct expression *exp,
1674 struct agent_expr *ax, struct axs_value *value,
1675 struct type *type, char *field,
1676 const char *operator_name,
1677 const char *operand_name)
1679 switch (TYPE_CODE (type))
1681 case TYPE_CODE_STRUCT:
1682 case TYPE_CODE_UNION:
1683 return gen_struct_elt_for_reference (exp, ax, value, type, field);
1685 case TYPE_CODE_NAMESPACE:
1686 return gen_namespace_elt (exp, ax, value, type, field);
1689 internal_error (__FILE__, __LINE__,
1690 _("non-aggregate type in gen_aggregate_elt_ref"));
1696 /* Generate code for GDB's magical `repeat' operator.
1697 LVALUE @ INT creates an array INT elements long, and whose elements
1698 have the same type as LVALUE, located in memory so that LVALUE is
1699 its first element. For example, argv[0]@argc gives you the array
1700 of command-line arguments.
1702 Unfortunately, because we have to know the types before we actually
1703 have a value for the expression, we can't implement this perfectly
1704 without changing the type system, having values that occupy two
1705 stack slots, doing weird things with sizeof, etc. So we require
1706 the right operand to be a constant expression. */
1708 gen_repeat (struct expression *exp, union exp_element **pc,
1709 struct agent_expr *ax, struct axs_value *value)
1711 struct axs_value value1;
1713 /* We don't want to turn this into an rvalue, so no conversions
1715 gen_expr (exp, pc, ax, &value1);
1716 if (value1.kind != axs_lvalue_memory)
1717 error (_("Left operand of `@' must be an object in memory."));
1719 /* Evaluate the length; it had better be a constant. */
1721 struct value *v = const_expr (pc);
1725 error (_("Right operand of `@' must be a "
1726 "constant, in agent expressions."));
1727 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1728 error (_("Right operand of `@' must be an integer."));
1729 length = value_as_long (v);
1731 error (_("Right operand of `@' must be positive."));
1733 /* The top of the stack is already the address of the object, so
1734 all we need to do is frob the type of the lvalue. */
1736 /* FIXME-type-allocation: need a way to free this type when we are
1739 = lookup_array_range_type (value1.type, 0, length - 1);
1741 value->kind = axs_lvalue_memory;
1742 value->type = array;
1748 /* Emit code for the `sizeof' operator.
1749 *PC should point at the start of the operand expression; we advance it
1750 to the first instruction after the operand. */
1752 gen_sizeof (struct expression *exp, union exp_element **pc,
1753 struct agent_expr *ax, struct axs_value *value,
1754 struct type *size_type)
1756 /* We don't care about the value of the operand expression; we only
1757 care about its type. However, in the current arrangement, the
1758 only way to find an expression's type is to generate code for it.
1759 So we generate code for the operand, and then throw it away,
1760 replacing it with code that simply pushes its size. */
1761 int start = ax->len;
1763 gen_expr (exp, pc, ax, value);
1765 /* Throw away the code we just generated. */
1768 ax_const_l (ax, TYPE_LENGTH (value->type));
1769 value->kind = axs_rvalue;
1770 value->type = size_type;
1774 /* Generating bytecode from GDB expressions: general recursive thingy */
1777 /* A gen_expr function written by a Gen-X'er guy.
1778 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1780 gen_expr (struct expression *exp, union exp_element **pc,
1781 struct agent_expr *ax, struct axs_value *value)
1783 /* Used to hold the descriptions of operand expressions. */
1784 struct axs_value value1, value2, value3;
1785 enum exp_opcode op = (*pc)[0].opcode, op2;
1786 int if1, go1, if2, go2, end;
1787 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
1789 /* If we're looking at a constant expression, just push its value. */
1791 struct value *v = maybe_const_expr (pc);
1795 ax_const_l (ax, value_as_long (v));
1796 value->kind = axs_rvalue;
1797 value->type = check_typedef (value_type (v));
1802 /* Otherwise, go ahead and generate code for it. */
1805 /* Binary arithmetic operators. */
1813 case BINOP_SUBSCRIPT:
1814 case BINOP_BITWISE_AND:
1815 case BINOP_BITWISE_IOR:
1816 case BINOP_BITWISE_XOR:
1818 case BINOP_NOTEQUAL:
1824 gen_expr (exp, pc, ax, &value1);
1825 gen_usual_unary (exp, ax, &value1);
1826 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1829 case BINOP_LOGICAL_AND:
1831 /* Generate the obvious sequence of tests and jumps. */
1832 gen_expr (exp, pc, ax, &value1);
1833 gen_usual_unary (exp, ax, &value1);
1834 if1 = ax_goto (ax, aop_if_goto);
1835 go1 = ax_goto (ax, aop_goto);
1836 ax_label (ax, if1, ax->len);
1837 gen_expr (exp, pc, ax, &value2);
1838 gen_usual_unary (exp, ax, &value2);
1839 if2 = ax_goto (ax, aop_if_goto);
1840 go2 = ax_goto (ax, aop_goto);
1841 ax_label (ax, if2, ax->len);
1843 end = ax_goto (ax, aop_goto);
1844 ax_label (ax, go1, ax->len);
1845 ax_label (ax, go2, ax->len);
1847 ax_label (ax, end, ax->len);
1848 value->kind = axs_rvalue;
1849 value->type = int_type;
1852 case BINOP_LOGICAL_OR:
1854 /* Generate the obvious sequence of tests and jumps. */
1855 gen_expr (exp, pc, ax, &value1);
1856 gen_usual_unary (exp, ax, &value1);
1857 if1 = ax_goto (ax, aop_if_goto);
1858 gen_expr (exp, pc, ax, &value2);
1859 gen_usual_unary (exp, ax, &value2);
1860 if2 = ax_goto (ax, aop_if_goto);
1862 end = ax_goto (ax, aop_goto);
1863 ax_label (ax, if1, ax->len);
1864 ax_label (ax, if2, ax->len);
1866 ax_label (ax, end, ax->len);
1867 value->kind = axs_rvalue;
1868 value->type = int_type;
1873 gen_expr (exp, pc, ax, &value1);
1874 gen_usual_unary (exp, ax, &value1);
1875 /* For (A ? B : C), it's easiest to generate subexpression
1876 bytecodes in order, but if_goto jumps on true, so we invert
1877 the sense of A. Then we can do B by dropping through, and
1879 gen_logical_not (ax, &value1, int_type);
1880 if1 = ax_goto (ax, aop_if_goto);
1881 gen_expr (exp, pc, ax, &value2);
1882 gen_usual_unary (exp, ax, &value2);
1883 end = ax_goto (ax, aop_goto);
1884 ax_label (ax, if1, ax->len);
1885 gen_expr (exp, pc, ax, &value3);
1886 gen_usual_unary (exp, ax, &value3);
1887 ax_label (ax, end, ax->len);
1888 /* This is arbitary - what if B and C are incompatible types? */
1889 value->type = value2.type;
1890 value->kind = value2.kind;
1895 if ((*pc)[0].opcode == OP_INTERNALVAR)
1897 char *name = internalvar_name ((*pc)[1].internalvar);
1898 struct trace_state_variable *tsv;
1901 gen_expr (exp, pc, ax, value);
1902 tsv = find_trace_state_variable (name);
1905 ax_tsv (ax, aop_setv, tsv->number);
1907 ax_tsv (ax, aop_tracev, tsv->number);
1910 error (_("$%s is not a trace state variable, "
1911 "may not assign to it"), name);
1914 error (_("May only assign to trace state variables"));
1917 case BINOP_ASSIGN_MODIFY:
1919 op2 = (*pc)[0].opcode;
1922 if ((*pc)[0].opcode == OP_INTERNALVAR)
1924 char *name = internalvar_name ((*pc)[1].internalvar);
1925 struct trace_state_variable *tsv;
1928 tsv = find_trace_state_variable (name);
1931 /* The tsv will be the left half of the binary operation. */
1932 ax_tsv (ax, aop_getv, tsv->number);
1934 ax_tsv (ax, aop_tracev, tsv->number);
1935 /* Trace state variables are always 64-bit integers. */
1936 value1.kind = axs_rvalue;
1937 value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1938 /* Now do right half of expression. */
1939 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1940 /* We have a result of the binary op, set the tsv. */
1941 ax_tsv (ax, aop_setv, tsv->number);
1943 ax_tsv (ax, aop_tracev, tsv->number);
1946 error (_("$%s is not a trace state variable, "
1947 "may not assign to it"), name);
1950 error (_("May only assign to trace state variables"));
1953 /* Note that we need to be a little subtle about generating code
1954 for comma. In C, we can do some optimizations here because
1955 we know the left operand is only being evaluated for effect.
1956 However, if the tracing kludge is in effect, then we always
1957 need to evaluate the left hand side fully, so that all the
1958 variables it mentions get traced. */
1961 gen_expr (exp, pc, ax, &value1);
1962 /* Don't just dispose of the left operand. We might be tracing,
1963 in which case we want to emit code to trace it if it's an
1965 gen_traced_pop (exp->gdbarch, ax, &value1);
1966 gen_expr (exp, pc, ax, value);
1967 /* It's the consumer's responsibility to trace the right operand. */
1970 case OP_LONG: /* some integer constant */
1972 struct type *type = (*pc)[1].type;
1973 LONGEST k = (*pc)[2].longconst;
1976 gen_int_literal (ax, value, k, type);
1981 gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
1983 if (value->optimized_out)
1984 error (_("`%s' has been optimized out, cannot use"),
1985 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
1992 const char *name = &(*pc)[2].string;
1995 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1996 reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
1998 internal_error (__FILE__, __LINE__,
1999 _("Register $%s not available"), name);
2000 /* No support for tracing user registers yet. */
2001 if (reg >= gdbarch_num_regs (exp->gdbarch)
2002 + gdbarch_num_pseudo_regs (exp->gdbarch))
2003 error (_("'%s' is a user-register; "
2004 "GDB cannot yet trace user-register contents."),
2006 value->kind = axs_lvalue_register;
2008 value->type = register_type (exp->gdbarch, reg);
2012 case OP_INTERNALVAR:
2014 struct internalvar *var = (*pc)[1].internalvar;
2015 const char *name = internalvar_name (var);
2016 struct trace_state_variable *tsv;
2019 tsv = find_trace_state_variable (name);
2022 ax_tsv (ax, aop_getv, tsv->number);
2024 ax_tsv (ax, aop_tracev, tsv->number);
2025 /* Trace state variables are always 64-bit integers. */
2026 value->kind = axs_rvalue;
2027 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2029 else if (! compile_internalvar_to_ax (var, ax, value))
2030 error (_("$%s is not a trace state variable; GDB agent "
2031 "expressions cannot use convenience variables."), name);
2035 /* Weirdo operator: see comments for gen_repeat for details. */
2037 /* Note that gen_repeat handles its own argument evaluation. */
2039 gen_repeat (exp, pc, ax, value);
2044 struct type *type = (*pc)[1].type;
2047 gen_expr (exp, pc, ax, value);
2048 gen_cast (ax, value, type);
2052 case UNOP_CAST_TYPE:
2059 offset = *pc - exp->elts;
2060 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2061 type = value_type (val);
2062 *pc = &exp->elts[offset];
2064 gen_expr (exp, pc, ax, value);
2065 gen_cast (ax, value, type);
2071 struct type *type = check_typedef ((*pc)[1].type);
2074 gen_expr (exp, pc, ax, value);
2076 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2077 already have the right value on the stack. For
2078 axs_lvalue_register, we must convert. */
2079 if (value->kind == axs_lvalue_register)
2080 require_rvalue (ax, value);
2083 value->kind = axs_lvalue_memory;
2087 case UNOP_MEMVAL_TYPE:
2094 offset = *pc - exp->elts;
2095 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2096 type = value_type (val);
2097 *pc = &exp->elts[offset];
2099 gen_expr (exp, pc, ax, value);
2101 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2102 already have the right value on the stack. For
2103 axs_lvalue_register, we must convert. */
2104 if (value->kind == axs_lvalue_register)
2105 require_rvalue (ax, value);
2108 value->kind = axs_lvalue_memory;
2114 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2115 gen_expr (exp, pc, ax, value);
2116 gen_usual_unary (exp, ax, value);
2121 /* -FOO is equivalent to 0 - FOO. */
2122 gen_int_literal (ax, &value1, 0,
2123 builtin_type (exp->gdbarch)->builtin_int);
2124 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2125 gen_expr (exp, pc, ax, &value2);
2126 gen_usual_unary (exp, ax, &value2);
2127 gen_usual_arithmetic (exp, ax, &value1, &value2);
2128 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2131 case UNOP_LOGICAL_NOT:
2133 gen_expr (exp, pc, ax, value);
2134 gen_usual_unary (exp, ax, value);
2135 gen_logical_not (ax, value, int_type);
2138 case UNOP_COMPLEMENT:
2140 gen_expr (exp, pc, ax, value);
2141 gen_usual_unary (exp, ax, value);
2142 gen_integral_promotions (exp, ax, value);
2143 gen_complement (ax, value);
2148 gen_expr (exp, pc, ax, value);
2149 gen_usual_unary (exp, ax, value);
2150 if (!pointer_type (value->type))
2151 error (_("Argument of unary `*' is not a pointer."));
2152 gen_deref (ax, value);
2157 gen_expr (exp, pc, ax, value);
2158 gen_address_of (ax, value);
2163 /* Notice that gen_sizeof handles its own operand, unlike most
2164 of the other unary operator functions. This is because we
2165 have to throw away the code we generate. */
2166 gen_sizeof (exp, pc, ax, value,
2167 builtin_type (exp->gdbarch)->builtin_int);
2170 case STRUCTOP_STRUCT:
2173 int length = (*pc)[1].longconst;
2174 char *name = &(*pc)[2].string;
2176 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2177 gen_expr (exp, pc, ax, value);
2178 if (op == STRUCTOP_STRUCT)
2179 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2180 else if (op == STRUCTOP_PTR)
2181 gen_struct_ref (exp, ax, value, name, "->",
2182 "pointer to a structure or union");
2184 /* If this `if' chain doesn't handle it, then the case list
2185 shouldn't mention it, and we shouldn't be here. */
2186 internal_error (__FILE__, __LINE__,
2187 _("gen_expr: unhandled struct case"));
2193 struct symbol *sym, *func;
2194 const struct block *b;
2195 const struct language_defn *lang;
2197 b = block_for_pc (ax->scope);
2198 func = block_linkage_function (b);
2199 lang = language_def (SYMBOL_LANGUAGE (func));
2201 sym = lookup_language_this (lang, b).symbol;
2203 error (_("no `%s' found"), lang->la_name_of_this);
2205 gen_var_ref (exp->gdbarch, ax, value, sym);
2207 if (value->optimized_out)
2208 error (_("`%s' has been optimized out, cannot use"),
2209 SYMBOL_PRINT_NAME (sym));
2217 struct type *type = (*pc)[1].type;
2218 int length = longest_to_int ((*pc)[2].longconst);
2219 char *name = &(*pc)[3].string;
2222 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2225 error (_("There is no field named %s"), name);
2226 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2233 error (_("Attempt to use a type name as an expression."));
2236 error (_("Unsupported operator %s (%d) in expression."),
2237 op_name (exp, op), op);
2241 /* This handles the middle-to-right-side of code generation for binary
2242 expressions, which is shared between regular binary operations and
2243 assign-modify (+= and friends) expressions. */
2246 gen_expr_binop_rest (struct expression *exp,
2247 enum exp_opcode op, union exp_element **pc,
2248 struct agent_expr *ax, struct axs_value *value,
2249 struct axs_value *value1, struct axs_value *value2)
2251 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2253 gen_expr (exp, pc, ax, value2);
2254 gen_usual_unary (exp, ax, value2);
2255 gen_usual_arithmetic (exp, ax, value1, value2);
2259 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2260 && pointer_type (value2->type))
2262 /* Swap the values and proceed normally. */
2263 ax_simple (ax, aop_swap);
2264 gen_ptradd (ax, value, value2, value1);
2266 else if (pointer_type (value1->type)
2267 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2268 gen_ptradd (ax, value, value1, value2);
2270 gen_binop (ax, value, value1, value2,
2271 aop_add, aop_add, 1, "addition");
2274 if (pointer_type (value1->type)
2275 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2276 gen_ptrsub (ax,value, value1, value2);
2277 else if (pointer_type (value1->type)
2278 && pointer_type (value2->type))
2279 /* FIXME --- result type should be ptrdiff_t */
2280 gen_ptrdiff (ax, value, value1, value2,
2281 builtin_type (exp->gdbarch)->builtin_long);
2283 gen_binop (ax, value, value1, value2,
2284 aop_sub, aop_sub, 1, "subtraction");
2287 gen_binop (ax, value, value1, value2,
2288 aop_mul, aop_mul, 1, "multiplication");
2291 gen_binop (ax, value, value1, value2,
2292 aop_div_signed, aop_div_unsigned, 1, "division");
2295 gen_binop (ax, value, value1, value2,
2296 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2299 gen_binop (ax, value, value1, value2,
2300 aop_lsh, aop_lsh, 1, "left shift");
2303 gen_binop (ax, value, value1, value2,
2304 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2306 case BINOP_SUBSCRIPT:
2310 if (binop_types_user_defined_p (op, value1->type, value2->type))
2312 error (_("cannot subscript requested type: "
2313 "cannot call user defined functions"));
2317 /* If the user attempts to subscript something that is not
2318 an array or pointer type (like a plain int variable for
2319 example), then report this as an error. */
2320 type = check_typedef (value1->type);
2321 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2322 && TYPE_CODE (type) != TYPE_CODE_PTR)
2324 if (TYPE_NAME (type))
2325 error (_("cannot subscript something of type `%s'"),
2328 error (_("cannot subscript requested type"));
2332 if (!is_integral_type (value2->type))
2333 error (_("Argument to arithmetic operation "
2334 "not a number or boolean."));
2336 gen_ptradd (ax, value, value1, value2);
2337 gen_deref (ax, value);
2340 case BINOP_BITWISE_AND:
2341 gen_binop (ax, value, value1, value2,
2342 aop_bit_and, aop_bit_and, 0, "bitwise and");
2345 case BINOP_BITWISE_IOR:
2346 gen_binop (ax, value, value1, value2,
2347 aop_bit_or, aop_bit_or, 0, "bitwise or");
2350 case BINOP_BITWISE_XOR:
2351 gen_binop (ax, value, value1, value2,
2352 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2356 gen_equal (ax, value, value1, value2, int_type);
2359 case BINOP_NOTEQUAL:
2360 gen_equal (ax, value, value1, value2, int_type);
2361 gen_logical_not (ax, value, int_type);
2365 gen_less (ax, value, value1, value2, int_type);
2369 ax_simple (ax, aop_swap);
2370 gen_less (ax, value, value1, value2, int_type);
2374 ax_simple (ax, aop_swap);
2375 gen_less (ax, value, value1, value2, int_type);
2376 gen_logical_not (ax, value, int_type);
2380 gen_less (ax, value, value1, value2, int_type);
2381 gen_logical_not (ax, value, int_type);
2385 /* We should only list operators in the outer case statement
2386 that we actually handle in the inner case statement. */
2387 internal_error (__FILE__, __LINE__,
2388 _("gen_expr: op case sets don't match"));
2393 /* Given a single variable and a scope, generate bytecodes to trace
2394 its value. This is for use in situations where we have only a
2395 variable's name, and no parsed expression; for instance, when the
2396 name comes from a list of local variables of a function. */
2399 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2400 struct symbol *var, int trace_string)
2402 agent_expr_up ax (new agent_expr (gdbarch, scope));
2403 struct axs_value value;
2406 ax->trace_string = trace_string;
2407 gen_var_ref (gdbarch, ax.get (), &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)
2412 return agent_expr_up ();
2414 /* Make sure we record the final object, and get rid of it. */
2415 gen_traced_pop (gdbarch, ax.get (), &value);
2417 /* Oh, and terminate. */
2418 ax_simple (ax.get (), aop_end);
2423 /* Generating bytecode from GDB expressions: driver */
2425 /* Given a GDB expression EXPR, return bytecode to trace its value.
2426 The result will use the `trace' and `trace_quick' bytecodes to
2427 record the value of all memory touched by the expression. The
2428 caller can then use the ax_reqs function to discover which
2429 registers it relies upon. */
2432 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
2435 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2436 union exp_element *pc;
2437 struct axs_value value;
2441 ax->trace_string = trace_string;
2442 value.optimized_out = 0;
2443 gen_expr (expr, &pc, ax.get (), &value);
2445 /* Make sure we record the final object, and get rid of it. */
2446 gen_traced_pop (expr->gdbarch, ax.get (), &value);
2448 /* Oh, and terminate. */
2449 ax_simple (ax.get (), aop_end);
2454 /* Given a GDB expression EXPR, return a bytecode sequence that will
2455 evaluate and return a result. The bytecodes will do a direct
2456 evaluation, using the current data on the target, rather than
2457 recording blocks of memory and registers for later use, as
2458 gen_trace_for_expr does. The generated bytecode sequence leaves
2459 the result of expression evaluation on the top of the stack. */
2462 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2464 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2465 union exp_element *pc;
2466 struct axs_value value;
2470 value.optimized_out = 0;
2471 gen_expr (expr, &pc, ax.get (), &value);
2473 require_rvalue (ax.get (), &value);
2475 /* Oh, and terminate. */
2476 ax_simple (ax.get (), aop_end);
2482 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
2485 agent_expr_up ax (new agent_expr (gdbarch, scope));
2486 struct axs_value value;
2489 ax->trace_string = trace_string;
2491 gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope);
2493 /* Make sure we record the final object, and get rid of it. */
2494 gen_traced_pop (gdbarch, ax.get (), &value);
2496 /* Oh, and terminate. */
2497 ax_simple (ax.get (), aop_end);
2502 /* Given a collection of printf-style arguments, generate code to
2503 evaluate the arguments and pass everything to a special
2507 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2508 CORE_ADDR function, LONGEST channel,
2509 const char *format, int fmtlen,
2510 struct format_piece *frags,
2511 int nargs, struct expression **exprs)
2513 agent_expr_up ax (new agent_expr (gdbarch, scope));
2514 union exp_element *pc;
2515 struct axs_value value;
2518 /* We're computing values, not doing side effects. */
2521 /* Evaluate and push the args on the stack in reverse order,
2522 for simplicity of collecting them on the target side. */
2523 for (tem = nargs - 1; tem >= 0; --tem)
2525 pc = exprs[tem]->elts;
2526 value.optimized_out = 0;
2527 gen_expr (exprs[tem], &pc, ax.get (), &value);
2528 require_rvalue (ax.get (), &value);
2531 /* Push function and channel. */
2532 ax_const_l (ax.get (), channel);
2533 ax_const_l (ax.get (), function);
2535 /* Issue the printf bytecode proper. */
2536 ax_simple (ax.get (), aop_printf);
2537 ax_raw_byte (ax.get (), nargs);
2538 ax_string (ax.get (), format, fmtlen);
2540 /* And terminate. */
2541 ax_simple (ax.get (), aop_end);
2547 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
2550 int trace_string = 0;
2555 exp = decode_agent_options (exp, &trace_string);
2558 agent_expr_up agent;
2561 if (!eval && strcmp (arg, "$_ret") == 0)
2563 agent = gen_trace_for_return_address (pc, get_current_arch (),
2568 expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
2572 gdb_assert (trace_string == 0);
2573 agent = gen_eval_for_expr (pc, expr.get ());
2576 agent = gen_trace_for_expr (pc, expr.get (), trace_string);
2579 ax_reqs (agent.get ());
2580 ax_print (gdb_stdout, agent.get ());
2582 /* It would be nice to call ax_reqs here to gather some general info
2583 about the expression, and then print out the result. */
2589 agent_command_1 (char *exp, int eval)
2591 /* We don't deal with overlay debugging at the moment. We need to
2592 think more carefully about this. If you copy this code into
2593 another command, change the error message; the user shouldn't
2594 have to know anything about agent expressions. */
2595 if (overlay_debugging)
2596 error (_("GDB can't do agent expression translation with overlays."));
2599 error_no_arg (_("expression to translate"));
2601 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2603 struct linespec_result canonical;
2605 struct linespec_sals *iter;
2606 struct cleanup *old_chain;
2607 struct event_location *location;
2609 exp = skip_spaces (exp);
2610 init_linespec_result (&canonical);
2611 location = new_linespec_location (&exp);
2612 old_chain = make_cleanup_delete_event_location (location);
2613 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
2614 (struct symtab *) NULL, 0, &canonical,
2616 make_cleanup_destroy_linespec_result (&canonical);
2617 exp = skip_spaces (exp);
2621 exp = skip_spaces (exp);
2623 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
2627 for (i = 0; i < iter->sals.nelts; i++)
2628 agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
2630 do_cleanups (old_chain);
2633 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
2639 agent_command (char *exp, int from_tty)
2641 agent_command_1 (exp, 0);
2644 /* Parse the given expression, compile it into an agent expression
2645 that does direct evaluation, and display the resulting
2649 agent_eval_command (char *exp, int from_tty)
2651 agent_command_1 (exp, 1);
2654 /* Parse the given expression, compile it into an agent expression
2655 that does a printf, and display the resulting expression. */
2658 maint_agent_printf_command (char *exp, int from_tty)
2660 struct cleanup *old_chain = 0;
2661 struct expression *argvec[100];
2662 struct frame_info *fi = get_current_frame (); /* need current scope */
2663 const char *cmdrest;
2664 const char *format_start, *format_end;
2665 struct format_piece *fpieces;
2668 /* We don't deal with overlay debugging at the moment. We need to
2669 think more carefully about this. If you copy this code into
2670 another command, change the error message; the user shouldn't
2671 have to know anything about agent expressions. */
2672 if (overlay_debugging)
2673 error (_("GDB can't do agent expression translation with overlays."));
2676 error_no_arg (_("expression to translate"));
2680 cmdrest = skip_spaces_const (cmdrest);
2682 if (*cmdrest++ != '"')
2683 error (_("Must start with a format string."));
2685 format_start = cmdrest;
2687 fpieces = parse_format_string (&cmdrest);
2689 old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2691 format_end = cmdrest;
2693 if (*cmdrest++ != '"')
2694 error (_("Bad format string, non-terminated '\"'."));
2696 cmdrest = skip_spaces_const (cmdrest);
2698 if (*cmdrest != ',' && *cmdrest != 0)
2699 error (_("Invalid argument syntax"));
2701 if (*cmdrest == ',')
2703 cmdrest = skip_spaces_const (cmdrest);
2706 while (*cmdrest != '\0')
2711 expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2712 argvec[nargs] = expr.release ();
2715 if (*cmdrest == ',')
2717 /* else complain? */
2721 agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (),
2723 format_start, format_end - format_start,
2724 fpieces, nargs, argvec);
2725 ax_reqs (agent.get ());
2726 ax_print (gdb_stdout, agent.get ());
2728 /* It would be nice to call ax_reqs here to gather some general info
2729 about the expression, and then print out the result. */
2731 do_cleanups (old_chain);
2736 /* Initialization code. */
2738 void _initialize_ax_gdb (void);
2740 _initialize_ax_gdb (void)
2742 add_cmd ("agent", class_maintenance, agent_command,
2744 Translate an expression into remote agent bytecode for tracing.\n\
2745 Usage: maint agent [-at location,] EXPRESSION\n\
2746 If -at is given, generate remote agent bytecode for this location.\n\
2747 If not, generate remote agent bytecode for current frame pc address."),
2750 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2752 Translate an expression into remote agent bytecode for evaluation.\n\
2753 Usage: maint agent-eval [-at location,] EXPRESSION\n\
2754 If -at is given, generate remote agent bytecode for this location.\n\
2755 If not, generate remote agent bytecode for current frame pc address."),
2758 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2759 _("Translate an expression into remote "
2760 "agent bytecode for evaluation and display the bytecodes."),