1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998-2001, 2003, 2007-2012 Free Software Foundation,
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "expression.h"
34 #include "gdb_string.h"
37 #include "user-regs.h"
39 #include "dictionary.h"
40 #include "breakpoint.h"
41 #include "tracepoint.h"
42 #include "cp-support.h"
43 #include "arch-utils.h"
44 #include "cli/cli-utils.h"
51 /* To make sense of this file, you should read doc/agentexpr.texi.
52 Then look at the types and enums in ax-gdb.h. For the code itself,
53 look at gen_expr, towards the bottom; that's the main function that
54 looks at the GDB expressions and calls everything else to generate
57 I'm beginning to wonder whether it wouldn't be nicer to internally
58 generate trees, with types, and then spit out the bytecode in
59 linear form afterwards; we could generate fewer `swap', `ext', and
60 `zero_ext' bytecodes that way; it would make good constant folding
61 easier, too. But at the moment, I think we should be willing to
62 pay for the simplicity of this code with less-than-optimal bytecode
65 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
69 /* Prototypes for local functions. */
71 /* There's a standard order to the arguments of these functions:
72 union exp_element ** --- pointer into expression
73 struct agent_expr * --- agent expression buffer to generate code into
74 struct axs_value * --- describes value left on top of stack */
76 static struct value *const_var_ref (struct symbol *var);
77 static struct value *const_expr (union exp_element **pc);
78 static struct value *maybe_const_expr (union exp_element **pc);
80 static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
83 static void gen_sign_extend (struct agent_expr *, struct type *);
84 static void gen_extend (struct agent_expr *, struct type *);
85 static void gen_fetch (struct agent_expr *, struct type *);
86 static void gen_left_shift (struct agent_expr *, int);
89 static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
90 static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
91 static void gen_offset (struct agent_expr *ax, int offset);
92 static void gen_sym_offset (struct agent_expr *, struct symbol *);
93 static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
94 struct axs_value *value, struct symbol *var);
97 static void gen_int_literal (struct agent_expr *ax,
98 struct axs_value *value,
99 LONGEST k, struct type *type);
101 static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
102 struct axs_value *value);
103 static int type_wider_than (struct type *type1, struct type *type2);
104 static struct type *max_type (struct type *type1, struct type *type2);
105 static void gen_conversion (struct agent_expr *ax,
106 struct type *from, struct type *to);
107 static int is_nontrivial_conversion (struct type *from, struct type *to);
108 static void gen_usual_arithmetic (struct expression *exp,
109 struct agent_expr *ax,
110 struct axs_value *value1,
111 struct axs_value *value2);
112 static void gen_integral_promotions (struct expression *exp,
113 struct agent_expr *ax,
114 struct axs_value *value);
115 static void gen_cast (struct agent_expr *ax,
116 struct axs_value *value, struct type *type);
117 static void gen_scale (struct agent_expr *ax,
118 enum agent_op op, struct type *type);
119 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
120 struct axs_value *value1, struct axs_value *value2);
121 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
122 struct axs_value *value1, struct axs_value *value2);
123 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
124 struct axs_value *value1, struct axs_value *value2,
125 struct type *result_type);
126 static void gen_binop (struct agent_expr *ax,
127 struct axs_value *value,
128 struct axs_value *value1,
129 struct axs_value *value2,
131 enum agent_op op_unsigned, int may_carry, char *name);
132 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
133 struct type *result_type);
134 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
135 static void gen_deref (struct agent_expr *, struct axs_value *);
136 static void gen_address_of (struct agent_expr *, struct axs_value *);
137 static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
138 struct axs_value *value,
139 struct type *type, int start, int end);
140 static void gen_primitive_field (struct expression *exp,
141 struct agent_expr *ax,
142 struct axs_value *value,
143 int offset, int fieldno, struct type *type);
144 static int gen_struct_ref_recursive (struct expression *exp,
145 struct agent_expr *ax,
146 struct axs_value *value,
147 char *field, int offset,
149 static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
150 struct axs_value *value,
152 char *operator_name, char *operand_name);
153 static void gen_static_field (struct gdbarch *gdbarch,
154 struct agent_expr *ax, struct axs_value *value,
155 struct type *type, int fieldno);
156 static void gen_repeat (struct expression *exp, union exp_element **pc,
157 struct agent_expr *ax, struct axs_value *value);
158 static void gen_sizeof (struct expression *exp, union exp_element **pc,
159 struct agent_expr *ax, struct axs_value *value,
160 struct type *size_type);
161 static void gen_expr_binop_rest (struct expression *exp,
162 enum exp_opcode op, union exp_element **pc,
163 struct agent_expr *ax,
164 struct axs_value *value,
165 struct axs_value *value1,
166 struct axs_value *value2);
168 static void agent_command (char *exp, int from_tty);
171 /* Detecting constant expressions. */
173 /* If the variable reference at *PC is a constant, return its value.
174 Otherwise, return zero.
176 Hey, Wally! How can a variable reference be a constant?
178 Well, Beav, this function really handles the OP_VAR_VALUE operator,
179 not specifically variable references. GDB uses OP_VAR_VALUE to
180 refer to any kind of symbolic reference: function names, enum
181 elements, and goto labels are all handled through the OP_VAR_VALUE
182 operator, even though they're constants. It makes sense given the
185 Gee, Wally, don'cha wonder sometimes if data representations that
186 subvert commonly accepted definitions of terms in favor of heavily
187 context-specific interpretations are really just a tool of the
188 programming hegemony to preserve their power and exclude the
191 static struct value *
192 const_var_ref (struct symbol *var)
194 struct type *type = SYMBOL_TYPE (var);
196 switch (SYMBOL_CLASS (var))
199 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
202 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
210 /* If the expression starting at *PC has a constant value, return it.
211 Otherwise, return zero. If we return a value, then *PC will be
212 advanced to the end of it. If we return zero, *PC could be
214 static struct value *
215 const_expr (union exp_element **pc)
217 enum exp_opcode op = (*pc)->opcode;
224 struct type *type = (*pc)[1].type;
225 LONGEST k = (*pc)[2].longconst;
228 return value_from_longest (type, k);
233 struct value *v = const_var_ref ((*pc)[2].symbol);
239 /* We could add more operators in here. */
243 v1 = const_expr (pc);
245 return value_neg (v1);
255 /* Like const_expr, but guarantee also that *PC is undisturbed if the
256 expression is not constant. */
257 static struct value *
258 maybe_const_expr (union exp_element **pc)
260 union exp_element *tentative_pc = *pc;
261 struct value *v = const_expr (&tentative_pc);
263 /* If we got a value, then update the real PC. */
271 /* Generating bytecode from GDB expressions: general assumptions */
273 /* Here are a few general assumptions made throughout the code; if you
274 want to make a change that contradicts one of these, then you'd
275 better scan things pretty thoroughly.
277 - We assume that all values occupy one stack element. For example,
278 sometimes we'll swap to get at the left argument to a binary
279 operator. If we decide that void values should occupy no stack
280 elements, or that synthetic arrays (whose size is determined at
281 run time, created by the `@' operator) should occupy two stack
282 elements (address and length), then this will cause trouble.
284 - We assume the stack elements are infinitely wide, and that we
285 don't have to worry what happens if the user requests an
286 operation that is wider than the actual interpreter's stack.
287 That is, it's up to the interpreter to handle directly all the
288 integer widths the user has access to. (Woe betide the language
291 - We don't support side effects. Thus, we don't have to worry about
292 GCC's generalized lvalues, function calls, etc.
294 - We don't support floating point. Many places where we switch on
295 some type don't bother to include cases for floating point; there
296 may be even more subtle ways this assumption exists. For
297 example, the arguments to % must be integers.
299 - We assume all subexpressions have a static, unchanging type. If
300 we tried to support convenience variables, this would be a
303 - All values on the stack should always be fully zero- or
306 (I wasn't sure whether to choose this or its opposite --- that
307 only addresses are assumed extended --- but it turns out that
308 neither convention completely eliminates spurious extend
309 operations (if everything is always extended, then you have to
310 extend after add, because it could overflow; if nothing is
311 extended, then you end up producing extends whenever you change
312 sizes), and this is simpler.) */
315 /* Generating bytecode from GDB expressions: the `trace' kludge */
317 /* The compiler in this file is a general-purpose mechanism for
318 translating GDB expressions into bytecode. One ought to be able to
319 find a million and one uses for it.
321 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
322 of expediency. Let he who is without sin cast the first stone.
324 For the data tracing facility, we need to insert `trace' bytecodes
325 before each data fetch; this records all the memory that the
326 expression touches in the course of evaluation, so that memory will
327 be available when the user later tries to evaluate the expression
330 This should be done (I think) in a post-processing pass, that walks
331 an arbitrary agent expression and inserts `trace' operations at the
332 appropriate points. But it's much faster to just hack them
333 directly into the code. And since we're in a crunch, that's what
336 Setting the flag trace_kludge to non-zero enables the code that
337 emits the trace bytecodes at the appropriate points. */
340 /* Inspired by trace_kludge, this indicates that pointers to chars
341 should get an added tracenz bytecode to record nonzero bytes, up to
342 a length that is the value of trace_string_kludge. */
343 int trace_string_kludge;
345 /* Scan for all static fields in the given class, including any base
346 classes, and generate tracing bytecodes for each. */
349 gen_trace_static_fields (struct gdbarch *gdbarch,
350 struct agent_expr *ax,
353 int i, nbases = TYPE_N_BASECLASSES (type);
354 struct axs_value value;
356 CHECK_TYPEDEF (type);
358 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
360 if (field_is_static (&TYPE_FIELD (type, i)))
362 gen_static_field (gdbarch, ax, &value, type, i);
363 if (value.optimized_out)
367 case axs_lvalue_memory:
369 int length = TYPE_LENGTH (check_typedef (value.type));
371 ax_const_l (ax, length);
372 ax_simple (ax, aop_trace);
376 case axs_lvalue_register:
377 /* We don't actually need the register's value to be pushed,
378 just note that we need it to be collected. */
379 ax_reg_mask (ax, value.u.reg);
387 /* Now scan through base classes recursively. */
388 for (i = 0; i < nbases; i++)
390 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
392 gen_trace_static_fields (gdbarch, ax, basetype);
396 /* Trace the lvalue on the stack, if it needs it. In either case, pop
397 the value. Useful on the left side of a comma, and at the end of
398 an expression being used for tracing. */
400 gen_traced_pop (struct gdbarch *gdbarch,
401 struct agent_expr *ax, struct axs_value *value)
403 int string_trace = 0;
404 if (trace_string_kludge
405 && TYPE_CODE (value->type) == TYPE_CODE_PTR
406 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
416 ax_const_l (ax, trace_string_kludge);
417 ax_simple (ax, aop_tracenz);
420 /* We don't trace rvalues, just the lvalues necessary to
421 produce them. So just dispose of this value. */
422 ax_simple (ax, aop_pop);
425 case axs_lvalue_memory:
427 int length = TYPE_LENGTH (check_typedef (value->type));
430 ax_simple (ax, aop_dup);
432 /* There's no point in trying to use a trace_quick bytecode
433 here, since "trace_quick SIZE pop" is three bytes, whereas
434 "const8 SIZE trace" is also three bytes, does the same
435 thing, and the simplest code which generates that will also
436 work correctly for objects with large sizes. */
437 ax_const_l (ax, length);
438 ax_simple (ax, aop_trace);
442 ax_simple (ax, aop_ref32);
443 ax_const_l (ax, trace_string_kludge);
444 ax_simple (ax, aop_tracenz);
449 case axs_lvalue_register:
450 /* We don't actually need the register's value to be on the
451 stack, and the target will get heartburn if the register is
452 larger than will fit in a stack, so just mark it for
453 collection and be done with it. */
454 ax_reg_mask (ax, value->u.reg);
456 /* But if the register points to a string, assume the value
457 will fit on the stack and push it anyway. */
460 ax_reg (ax, value->u.reg);
461 ax_const_l (ax, trace_string_kludge);
462 ax_simple (ax, aop_tracenz);
467 /* If we're not tracing, just pop the value. */
468 ax_simple (ax, aop_pop);
470 /* To trace C++ classes with static fields stored elsewhere. */
472 && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
473 || TYPE_CODE (value->type) == TYPE_CODE_UNION))
474 gen_trace_static_fields (gdbarch, ax, value->type);
479 /* Generating bytecode from GDB expressions: helper functions */
481 /* Assume that the lower bits of the top of the stack is a value of
482 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
484 gen_sign_extend (struct agent_expr *ax, struct type *type)
486 /* Do we need to sign-extend this? */
487 if (!TYPE_UNSIGNED (type))
488 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
492 /* Assume the lower bits of the top of the stack hold a value of type
493 TYPE, and the upper bits are garbage. Sign-extend or truncate as
496 gen_extend (struct agent_expr *ax, struct type *type)
498 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
501 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
505 /* Assume that the top of the stack contains a value of type "pointer
506 to TYPE"; generate code to fetch its value. Note that TYPE is the
507 target type, not the pointer type. */
509 gen_fetch (struct agent_expr *ax, struct type *type)
513 /* Record the area of memory we're about to fetch. */
514 ax_trace_quick (ax, TYPE_LENGTH (type));
517 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
518 type = TYPE_TARGET_TYPE (type);
520 switch (TYPE_CODE (type))
528 /* It's a scalar value, so we know how to dereference it. How
529 many bytes long is it? */
530 switch (TYPE_LENGTH (type))
532 case 8 / TARGET_CHAR_BIT:
533 ax_simple (ax, aop_ref8);
535 case 16 / TARGET_CHAR_BIT:
536 ax_simple (ax, aop_ref16);
538 case 32 / TARGET_CHAR_BIT:
539 ax_simple (ax, aop_ref32);
541 case 64 / TARGET_CHAR_BIT:
542 ax_simple (ax, aop_ref64);
545 /* Either our caller shouldn't have asked us to dereference
546 that pointer (other code's fault), or we're not
547 implementing something we should be (this code's fault).
548 In any case, it's a bug the user shouldn't see. */
550 internal_error (__FILE__, __LINE__,
551 _("gen_fetch: strange size"));
554 gen_sign_extend (ax, type);
558 /* Our caller requested us to dereference a pointer from an unsupported
559 type. Error out and give callers a chance to handle the failure
561 error (_("gen_fetch: Unsupported type code `%s'."),
567 /* Generate code to left shift the top of the stack by DISTANCE bits, or
568 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
569 unsigned (logical) right shifts. */
571 gen_left_shift (struct agent_expr *ax, int distance)
575 ax_const_l (ax, distance);
576 ax_simple (ax, aop_lsh);
578 else if (distance < 0)
580 ax_const_l (ax, -distance);
581 ax_simple (ax, aop_rsh_unsigned);
587 /* Generating bytecode from GDB expressions: symbol references */
589 /* Generate code to push the base address of the argument portion of
590 the top stack frame. */
592 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
595 LONGEST frame_offset;
597 gdbarch_virtual_frame_pointer (gdbarch,
598 ax->scope, &frame_reg, &frame_offset);
599 ax_reg (ax, frame_reg);
600 gen_offset (ax, frame_offset);
604 /* Generate code to push the base address of the locals portion of the
607 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
610 LONGEST frame_offset;
612 gdbarch_virtual_frame_pointer (gdbarch,
613 ax->scope, &frame_reg, &frame_offset);
614 ax_reg (ax, frame_reg);
615 gen_offset (ax, frame_offset);
619 /* Generate code to add OFFSET to the top of the stack. Try to
620 generate short and readable code. We use this for getting to
621 variables on the stack, and structure members. If we were
622 programming in ML, it would be clearer why these are the same
625 gen_offset (struct agent_expr *ax, int offset)
627 /* It would suffice to simply push the offset and add it, but this
628 makes it easier to read positive and negative offsets in the
632 ax_const_l (ax, offset);
633 ax_simple (ax, aop_add);
637 ax_const_l (ax, -offset);
638 ax_simple (ax, aop_sub);
643 /* In many cases, a symbol's value is the offset from some other
644 address (stack frame, base register, etc.) Generate code to add
645 VAR's value to the top of the stack. */
647 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
649 gen_offset (ax, SYMBOL_VALUE (var));
653 /* Generate code for a variable reference to AX. The variable is the
654 symbol VAR. Set VALUE to describe the result. */
657 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
658 struct axs_value *value, struct symbol *var)
660 /* Dereference any typedefs. */
661 value->type = check_typedef (SYMBOL_TYPE (var));
662 value->optimized_out = 0;
664 /* I'm imitating the code in read_var_value. */
665 switch (SYMBOL_CLASS (var))
667 case LOC_CONST: /* A constant, like an enum value. */
668 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
669 value->kind = axs_rvalue;
672 case LOC_LABEL: /* A goto label, being used as a value. */
673 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
674 value->kind = axs_rvalue;
677 case LOC_CONST_BYTES:
678 internal_error (__FILE__, __LINE__,
679 _("gen_var_ref: LOC_CONST_BYTES "
680 "symbols are not supported"));
682 /* Variable at a fixed location in memory. Easy. */
684 /* Push the address of the variable. */
685 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
686 value->kind = axs_lvalue_memory;
689 case LOC_ARG: /* var lives in argument area of frame */
690 gen_frame_args_address (gdbarch, ax);
691 gen_sym_offset (ax, var);
692 value->kind = axs_lvalue_memory;
695 case LOC_REF_ARG: /* As above, but the frame slot really
696 holds the address of the variable. */
697 gen_frame_args_address (gdbarch, ax);
698 gen_sym_offset (ax, var);
699 /* Don't assume any particular pointer size. */
700 gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
701 value->kind = axs_lvalue_memory;
704 case LOC_LOCAL: /* var lives in locals area of frame */
705 gen_frame_locals_address (gdbarch, ax);
706 gen_sym_offset (ax, var);
707 value->kind = axs_lvalue_memory;
711 error (_("Cannot compute value of typedef `%s'."),
712 SYMBOL_PRINT_NAME (var));
716 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
717 value->kind = axs_rvalue;
721 /* Don't generate any code at all; in the process of treating
722 this as an lvalue or rvalue, the caller will generate the
724 value->kind = axs_lvalue_register;
725 value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
728 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
729 register, not on the stack. Simpler than LOC_REGISTER
730 because it's just like any other case where the thing
731 has a real address. */
732 case LOC_REGPARM_ADDR:
733 ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
734 value->kind = axs_lvalue_memory;
739 struct minimal_symbol *msym
740 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
743 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
745 /* Push the address of the variable. */
746 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
747 value->kind = axs_lvalue_memory;
752 /* FIXME: cagney/2004-01-26: It should be possible to
753 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
754 Unfortunately DWARF 2 stores the frame-base (instead of the
755 function) location in a function's symbol. Oops! For the
756 moment enable this when/where applicable. */
757 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
760 case LOC_OPTIMIZED_OUT:
761 /* Flag this, but don't say anything; leave it up to callers to
763 value->optimized_out = 1;
767 error (_("Cannot find value of botched symbol `%s'."),
768 SYMBOL_PRINT_NAME (var));
775 /* Generating bytecode from GDB expressions: literals */
778 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
782 value->kind = axs_rvalue;
783 value->type = check_typedef (type);
788 /* Generating bytecode from GDB expressions: unary conversions, casts */
790 /* Take what's on the top of the stack (as described by VALUE), and
791 try to make an rvalue out of it. Signal an error if we can't do
794 require_rvalue (struct agent_expr *ax, struct axs_value *value)
796 /* Only deal with scalars, structs and such may be too large
797 to fit in a stack entry. */
798 value->type = check_typedef (value->type);
799 if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
800 || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
801 || TYPE_CODE (value->type) == TYPE_CODE_UNION
802 || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
803 error (_("Value not scalar: cannot be an rvalue."));
808 /* It's already an rvalue. */
811 case axs_lvalue_memory:
812 /* The top of stack is the address of the object. Dereference. */
813 gen_fetch (ax, value->type);
816 case axs_lvalue_register:
817 /* There's nothing on the stack, but value->u.reg is the
818 register number containing the value.
820 When we add floating-point support, this is going to have to
821 change. What about SPARC register pairs, for example? */
822 ax_reg (ax, value->u.reg);
823 gen_extend (ax, value->type);
827 value->kind = axs_rvalue;
831 /* Assume the top of the stack is described by VALUE, and perform the
832 usual unary conversions. This is motivated by ANSI 6.2.2, but of
833 course GDB expressions are not ANSI; they're the mishmash union of
834 a bunch of languages. Rah.
836 NOTE! This function promises to produce an rvalue only when the
837 incoming value is of an appropriate type. In other words, the
838 consumer of the value this function produces may assume the value
839 is an rvalue only after checking its type.
841 The immediate issue is that if the user tries to use a structure or
842 union as an operand of, say, the `+' operator, we don't want to try
843 to convert that structure to an rvalue; require_rvalue will bomb on
844 structs and unions. Rather, we want to simply pass the struct
845 lvalue through unchanged, and let `+' raise an error. */
848 gen_usual_unary (struct expression *exp, struct agent_expr *ax,
849 struct axs_value *value)
851 /* We don't have to generate any code for the usual integral
852 conversions, since values are always represented as full-width on
853 the stack. Should we tweak the type? */
855 /* Some types require special handling. */
856 switch (TYPE_CODE (value->type))
858 /* Functions get converted to a pointer to the function. */
860 value->type = lookup_pointer_type (value->type);
861 value->kind = axs_rvalue; /* Should always be true, but just in case. */
864 /* Arrays get converted to a pointer to their first element, and
865 are no longer an lvalue. */
866 case TYPE_CODE_ARRAY:
868 struct type *elements = TYPE_TARGET_TYPE (value->type);
870 value->type = lookup_pointer_type (elements);
871 value->kind = axs_rvalue;
872 /* We don't need to generate any code; the address of the array
873 is also the address of its first element. */
877 /* Don't try to convert structures and unions to rvalues. Let the
878 consumer signal an error. */
879 case TYPE_CODE_STRUCT:
880 case TYPE_CODE_UNION:
884 /* If the value is an lvalue, dereference it. */
885 require_rvalue (ax, value);
889 /* Return non-zero iff the type TYPE1 is considered "wider" than the
890 type TYPE2, according to the rules described in gen_usual_arithmetic. */
892 type_wider_than (struct type *type1, struct type *type2)
894 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
895 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
896 && TYPE_UNSIGNED (type1)
897 && !TYPE_UNSIGNED (type2)));
901 /* Return the "wider" of the two types TYPE1 and TYPE2. */
903 max_type (struct type *type1, struct type *type2)
905 return type_wider_than (type1, type2) ? type1 : type2;
909 /* Generate code to convert a scalar value of type FROM to type TO. */
911 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
913 /* Perhaps there is a more graceful way to state these rules. */
915 /* If we're converting to a narrower type, then we need to clear out
917 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
918 gen_extend (ax, from);
920 /* If the two values have equal width, but different signednesses,
921 then we need to extend. */
922 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
924 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
928 /* If we're converting to a wider type, and becoming unsigned, then
929 we need to zero out any possible sign bits. */
930 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
932 if (TYPE_UNSIGNED (to))
938 /* Return non-zero iff the type FROM will require any bytecodes to be
939 emitted to be converted to the type TO. */
941 is_nontrivial_conversion (struct type *from, struct type *to)
943 struct agent_expr *ax = new_agent_expr (NULL, 0);
946 /* Actually generate the code, and see if anything came out. At the
947 moment, it would be trivial to replicate the code in
948 gen_conversion here, but in the future, when we're supporting
949 floating point and the like, it may not be. Doing things this
950 way allows this function to be independent of the logic in
952 gen_conversion (ax, from, to);
953 nontrivial = ax->len > 0;
954 free_agent_expr (ax);
959 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
960 6.2.1.5) for the two operands of an arithmetic operator. This
961 effectively finds a "least upper bound" type for the two arguments,
962 and promotes each argument to that type. *VALUE1 and *VALUE2
963 describe the values as they are passed in, and as they are left. */
965 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
966 struct axs_value *value1, struct axs_value *value2)
968 /* Do the usual binary conversions. */
969 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
970 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
972 /* The ANSI integral promotions seem to work this way: Order the
973 integer types by size, and then by signedness: an n-bit
974 unsigned type is considered "wider" than an n-bit signed
975 type. Promote to the "wider" of the two types, and always
976 promote at least to int. */
977 struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
978 max_type (value1->type, value2->type));
980 /* Deal with value2, on the top of the stack. */
981 gen_conversion (ax, value2->type, target);
983 /* Deal with value1, not on the top of the stack. Don't
984 generate the `swap' instructions if we're not actually going
986 if (is_nontrivial_conversion (value1->type, target))
988 ax_simple (ax, aop_swap);
989 gen_conversion (ax, value1->type, target);
990 ax_simple (ax, aop_swap);
993 value1->type = value2->type = check_typedef (target);
998 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
999 the value on the top of the stack, as described by VALUE. Assume
1000 the value has integral type. */
1002 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
1003 struct axs_value *value)
1005 const struct builtin_type *builtin = builtin_type (exp->gdbarch);
1007 if (!type_wider_than (value->type, builtin->builtin_int))
1009 gen_conversion (ax, value->type, builtin->builtin_int);
1010 value->type = builtin->builtin_int;
1012 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
1014 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
1015 value->type = builtin->builtin_unsigned_int;
1020 /* Generate code for a cast to TYPE. */
1022 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
1024 /* GCC does allow casts to yield lvalues, so this should be fixed
1025 before merging these changes into the trunk. */
1026 require_rvalue (ax, value);
1027 /* Dereference typedefs. */
1028 type = check_typedef (type);
1030 switch (TYPE_CODE (type))
1034 /* It's implementation-defined, and I'll bet this is what GCC
1038 case TYPE_CODE_ARRAY:
1039 case TYPE_CODE_STRUCT:
1040 case TYPE_CODE_UNION:
1041 case TYPE_CODE_FUNC:
1042 error (_("Invalid type cast: intended type must be scalar."));
1044 case TYPE_CODE_ENUM:
1045 case TYPE_CODE_BOOL:
1046 /* We don't have to worry about the size of the value, because
1047 all our integral values are fully sign-extended, and when
1048 casting pointers we can do anything we like. Is there any
1049 way for us to know what GCC actually does with a cast like
1054 gen_conversion (ax, value->type, type);
1057 case TYPE_CODE_VOID:
1058 /* We could pop the value, and rely on everyone else to check
1059 the type and notice that this value doesn't occupy a stack
1060 slot. But for now, leave the value on the stack, and
1061 preserve the "value == stack element" assumption. */
1065 error (_("Casts to requested type are not yet implemented."));
1073 /* Generating bytecode from GDB expressions: arithmetic */
1075 /* Scale the integer on the top of the stack by the size of the target
1076 of the pointer type TYPE. */
1078 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1080 struct type *element = TYPE_TARGET_TYPE (type);
1082 if (TYPE_LENGTH (element) != 1)
1084 ax_const_l (ax, TYPE_LENGTH (element));
1090 /* Generate code for pointer arithmetic PTR + INT. */
1092 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1093 struct axs_value *value1, struct axs_value *value2)
1095 gdb_assert (pointer_type (value1->type));
1096 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1098 gen_scale (ax, aop_mul, value1->type);
1099 ax_simple (ax, aop_add);
1100 gen_extend (ax, value1->type); /* Catch overflow. */
1101 value->type = value1->type;
1102 value->kind = axs_rvalue;
1106 /* Generate code for pointer arithmetic PTR - INT. */
1108 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1109 struct axs_value *value1, struct axs_value *value2)
1111 gdb_assert (pointer_type (value1->type));
1112 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1114 gen_scale (ax, aop_mul, value1->type);
1115 ax_simple (ax, aop_sub);
1116 gen_extend (ax, value1->type); /* Catch overflow. */
1117 value->type = value1->type;
1118 value->kind = axs_rvalue;
1122 /* Generate code for pointer arithmetic PTR - PTR. */
1124 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1125 struct axs_value *value1, struct axs_value *value2,
1126 struct type *result_type)
1128 gdb_assert (pointer_type (value1->type));
1129 gdb_assert (pointer_type (value2->type));
1131 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1132 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1134 First argument of `-' is a pointer, but second argument is neither\n\
1135 an integer nor a pointer of the same type."));
1137 ax_simple (ax, aop_sub);
1138 gen_scale (ax, aop_div_unsigned, value1->type);
1139 value->type = result_type;
1140 value->kind = axs_rvalue;
1144 gen_equal (struct agent_expr *ax, struct axs_value *value,
1145 struct axs_value *value1, struct axs_value *value2,
1146 struct type *result_type)
1148 if (pointer_type (value1->type) || pointer_type (value2->type))
1149 ax_simple (ax, aop_equal);
1151 gen_binop (ax, value, value1, value2,
1152 aop_equal, aop_equal, 0, "equal");
1153 value->type = result_type;
1154 value->kind = axs_rvalue;
1158 gen_less (struct agent_expr *ax, struct axs_value *value,
1159 struct axs_value *value1, struct axs_value *value2,
1160 struct type *result_type)
1162 if (pointer_type (value1->type) || pointer_type (value2->type))
1163 ax_simple (ax, aop_less_unsigned);
1165 gen_binop (ax, value, value1, value2,
1166 aop_less_signed, aop_less_unsigned, 0, "less than");
1167 value->type = result_type;
1168 value->kind = axs_rvalue;
1171 /* Generate code for a binary operator that doesn't do pointer magic.
1172 We set VALUE to describe the result value; we assume VALUE1 and
1173 VALUE2 describe the two operands, and that they've undergone the
1174 usual binary conversions. MAY_CARRY should be non-zero iff the
1175 result needs to be extended. NAME is the English name of the
1176 operator, used in error messages */
1178 gen_binop (struct agent_expr *ax, struct axs_value *value,
1179 struct axs_value *value1, struct axs_value *value2,
1180 enum agent_op op, enum agent_op op_unsigned,
1181 int may_carry, char *name)
1183 /* We only handle INT op INT. */
1184 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1185 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1186 error (_("Invalid combination of types in %s."), name);
1189 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1191 gen_extend (ax, value1->type); /* catch overflow */
1192 value->type = value1->type;
1193 value->kind = axs_rvalue;
1198 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1199 struct type *result_type)
1201 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1202 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1203 error (_("Invalid type of operand to `!'."));
1205 ax_simple (ax, aop_log_not);
1206 value->type = result_type;
1211 gen_complement (struct agent_expr *ax, struct axs_value *value)
1213 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1214 error (_("Invalid type of operand to `~'."));
1216 ax_simple (ax, aop_bit_not);
1217 gen_extend (ax, value->type);
1222 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1224 /* Dereference the value on the top of the stack. */
1226 gen_deref (struct agent_expr *ax, struct axs_value *value)
1228 /* The caller should check the type, because several operators use
1229 this, and we don't know what error message to generate. */
1230 if (!pointer_type (value->type))
1231 internal_error (__FILE__, __LINE__,
1232 _("gen_deref: expected a pointer"));
1234 /* We've got an rvalue now, which is a pointer. We want to yield an
1235 lvalue, whose address is exactly that pointer. So we don't
1236 actually emit any code; we just change the type from "Pointer to
1237 T" to "T", and mark the value as an lvalue in memory. Leave it
1238 to the consumer to actually dereference it. */
1239 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1240 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1241 error (_("Attempt to dereference a generic pointer."));
1242 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1243 ? axs_rvalue : axs_lvalue_memory);
1247 /* Produce the address of the lvalue on the top of the stack. */
1249 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1251 /* Special case for taking the address of a function. The ANSI
1252 standard describes this as a special case, too, so this
1253 arrangement is not without motivation. */
1254 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1255 /* The value's already an rvalue on the stack, so we just need to
1257 value->type = lookup_pointer_type (value->type);
1259 switch (value->kind)
1262 error (_("Operand of `&' is an rvalue, which has no address."));
1264 case axs_lvalue_register:
1265 error (_("Operand of `&' is in a register, and has no address."));
1267 case axs_lvalue_memory:
1268 value->kind = axs_rvalue;
1269 value->type = lookup_pointer_type (value->type);
1274 /* Generate code to push the value of a bitfield of a structure whose
1275 address is on the top of the stack. START and END give the
1276 starting and one-past-ending *bit* numbers of the field within the
1279 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1280 struct axs_value *value, struct type *type,
1283 /* Note that ops[i] fetches 8 << i bits. */
1284 static enum agent_op ops[]
1285 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1286 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1288 /* We don't want to touch any byte that the bitfield doesn't
1289 actually occupy; we shouldn't make any accesses we're not
1290 explicitly permitted to. We rely here on the fact that the
1291 bytecode `ref' operators work on unaligned addresses.
1293 It takes some fancy footwork to get the stack to work the way
1294 we'd like. Say we're retrieving a bitfield that requires three
1295 fetches. Initially, the stack just contains the address:
1297 For the first fetch, we duplicate the address
1299 then add the byte offset, do the fetch, and shift and mask as
1300 needed, yielding a fragment of the value, properly aligned for
1301 the final bitwise or:
1303 then we swap, and repeat the process:
1304 frag1 addr --- address on top
1305 frag1 addr addr --- duplicate it
1306 frag1 addr frag2 --- get second fragment
1307 frag1 frag2 addr --- swap again
1308 frag1 frag2 frag3 --- get third fragment
1309 Notice that, since the third fragment is the last one, we don't
1310 bother duplicating the address this time. Now we have all the
1311 fragments on the stack, and we can simply `or' them together,
1312 yielding the final value of the bitfield. */
1314 /* The first and one-after-last bits in the field, but rounded down
1315 and up to byte boundaries. */
1316 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1317 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1321 /* current bit offset within the structure */
1324 /* The index in ops of the opcode we're considering. */
1327 /* The number of fragments we generated in the process. Probably
1328 equal to the number of `one' bits in bytesize, but who cares? */
1331 /* Dereference any typedefs. */
1332 type = check_typedef (type);
1334 /* Can we fetch the number of bits requested at all? */
1335 if ((end - start) > ((1 << num_ops) * 8))
1336 internal_error (__FILE__, __LINE__,
1337 _("gen_bitfield_ref: bitfield too wide"));
1339 /* Note that we know here that we only need to try each opcode once.
1340 That may not be true on machines with weird byte sizes. */
1341 offset = bound_start;
1343 for (op = num_ops - 1; op >= 0; op--)
1345 /* number of bits that ops[op] would fetch */
1346 int op_size = 8 << op;
1348 /* The stack at this point, from bottom to top, contains zero or
1349 more fragments, then the address. */
1351 /* Does this fetch fit within the bitfield? */
1352 if (offset + op_size <= bound_end)
1354 /* Is this the last fragment? */
1355 int last_frag = (offset + op_size == bound_end);
1358 ax_simple (ax, aop_dup); /* keep a copy of the address */
1360 /* Add the offset. */
1361 gen_offset (ax, offset / TARGET_CHAR_BIT);
1365 /* Record the area of memory we're about to fetch. */
1366 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1369 /* Perform the fetch. */
1370 ax_simple (ax, ops[op]);
1372 /* Shift the bits we have to their proper position.
1373 gen_left_shift will generate right shifts when the operand
1376 A big-endian field diagram to ponder:
1377 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1378 +------++------++------++------++------++------++------++------+
1379 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1381 bit number 16 32 48 53
1382 These are bit numbers as supplied by GDB. Note that the
1383 bit numbers run from right to left once you've fetched the
1386 A little-endian field diagram to ponder:
1387 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1388 +------++------++------++------++------++------++------++------+
1389 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1391 bit number 48 32 16 4 0
1393 In both cases, the most significant end is on the left
1394 (i.e. normal numeric writing order), which means that you
1395 don't go crazy thinking about `left' and `right' shifts.
1397 We don't have to worry about masking yet:
1398 - If they contain garbage off the least significant end, then we
1399 must be looking at the low end of the field, and the right
1400 shift will wipe them out.
1401 - If they contain garbage off the most significant end, then we
1402 must be looking at the most significant end of the word, and
1403 the sign/zero extension will wipe them out.
1404 - If we're in the interior of the word, then there is no garbage
1405 on either end, because the ref operators zero-extend. */
1406 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1407 gen_left_shift (ax, end - (offset + op_size));
1409 gen_left_shift (ax, offset - start);
1412 /* Bring the copy of the address up to the top. */
1413 ax_simple (ax, aop_swap);
1420 /* Generate enough bitwise `or' operations to combine all the
1421 fragments we left on the stack. */
1422 while (fragment_count-- > 1)
1423 ax_simple (ax, aop_bit_or);
1425 /* Sign- or zero-extend the value as appropriate. */
1426 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1428 /* This is *not* an lvalue. Ugh. */
1429 value->kind = axs_rvalue;
1433 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1434 is an accumulated offset (in bytes), will be nonzero for objects
1435 embedded in other objects, like C++ base classes. Behavior should
1436 generally follow value_primitive_field. */
1439 gen_primitive_field (struct expression *exp,
1440 struct agent_expr *ax, struct axs_value *value,
1441 int offset, int fieldno, struct type *type)
1443 /* Is this a bitfield? */
1444 if (TYPE_FIELD_PACKED (type, fieldno))
1445 gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1446 (offset * TARGET_CHAR_BIT
1447 + TYPE_FIELD_BITPOS (type, fieldno)),
1448 (offset * TARGET_CHAR_BIT
1449 + TYPE_FIELD_BITPOS (type, fieldno)
1450 + TYPE_FIELD_BITSIZE (type, fieldno)));
1453 gen_offset (ax, offset
1454 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1455 value->kind = axs_lvalue_memory;
1456 value->type = TYPE_FIELD_TYPE (type, fieldno);
1460 /* Search for the given field in either the given type or one of its
1461 base classes. Return 1 if found, 0 if not. */
1464 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1465 struct axs_value *value,
1466 char *field, int offset, struct type *type)
1469 int nbases = TYPE_N_BASECLASSES (type);
1471 CHECK_TYPEDEF (type);
1473 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1475 const char *this_name = TYPE_FIELD_NAME (type, i);
1479 if (strcmp (field, this_name) == 0)
1481 /* Note that bytecodes for the struct's base (aka
1482 "this") will have been generated already, which will
1483 be unnecessary but not harmful if the static field is
1484 being handled as a global. */
1485 if (field_is_static (&TYPE_FIELD (type, i)))
1487 gen_static_field (exp->gdbarch, ax, value, type, i);
1488 if (value->optimized_out)
1489 error (_("static field `%s' has been "
1490 "optimized out, cannot use"),
1495 gen_primitive_field (exp, ax, value, offset, i, type);
1498 #if 0 /* is this right? */
1499 if (this_name[0] == '\0')
1500 internal_error (__FILE__, __LINE__,
1501 _("find_field: anonymous unions not supported"));
1506 /* Now scan through base classes recursively. */
1507 for (i = 0; i < nbases; i++)
1509 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1511 rslt = gen_struct_ref_recursive (exp, ax, value, field,
1512 offset + TYPE_BASECLASS_BITPOS (type, i)
1519 /* Not found anywhere, flag so caller can complain. */
1523 /* Generate code to reference the member named FIELD of a structure or
1524 union. The top of the stack, as described by VALUE, should have
1525 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1526 the operator being compiled, and OPERAND_NAME is the kind of thing
1527 it operates on; we use them in error messages. */
1529 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1530 struct axs_value *value, char *field,
1531 char *operator_name, char *operand_name)
1536 /* Follow pointers until we reach a non-pointer. These aren't the C
1537 semantics, but they're what the normal GDB evaluator does, so we
1538 should at least be consistent. */
1539 while (pointer_type (value->type))
1541 require_rvalue (ax, value);
1542 gen_deref (ax, value);
1544 type = check_typedef (value->type);
1546 /* This must yield a structure or a union. */
1547 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1548 && TYPE_CODE (type) != TYPE_CODE_UNION)
1549 error (_("The left operand of `%s' is not a %s."),
1550 operator_name, operand_name);
1552 /* And it must be in memory; we don't deal with structure rvalues,
1553 or structures living in registers. */
1554 if (value->kind != axs_lvalue_memory)
1555 error (_("Structure does not live in memory."));
1557 /* Search through fields and base classes recursively. */
1558 found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1561 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1562 field, TYPE_TAG_NAME (type));
1566 gen_namespace_elt (struct expression *exp,
1567 struct agent_expr *ax, struct axs_value *value,
1568 const struct type *curtype, char *name);
1570 gen_maybe_namespace_elt (struct expression *exp,
1571 struct agent_expr *ax, struct axs_value *value,
1572 const struct type *curtype, char *name);
1575 gen_static_field (struct gdbarch *gdbarch,
1576 struct agent_expr *ax, struct axs_value *value,
1577 struct type *type, int fieldno)
1579 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1581 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1582 value->kind = axs_lvalue_memory;
1583 value->type = TYPE_FIELD_TYPE (type, fieldno);
1584 value->optimized_out = 0;
1588 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1589 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
1593 gen_var_ref (gdbarch, ax, value, sym);
1595 /* Don't error if the value was optimized out, we may be
1596 scanning all static fields and just want to pass over this
1597 and continue with the rest. */
1601 /* Silently assume this was optimized out; class printing
1602 will let the user know why the data is missing. */
1603 value->optimized_out = 1;
1609 gen_struct_elt_for_reference (struct expression *exp,
1610 struct agent_expr *ax, struct axs_value *value,
1611 struct type *type, char *fieldname)
1613 struct type *t = type;
1616 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1617 && TYPE_CODE (t) != TYPE_CODE_UNION)
1618 internal_error (__FILE__, __LINE__,
1619 _("non-aggregate type to gen_struct_elt_for_reference"));
1621 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1623 const char *t_field_name = TYPE_FIELD_NAME (t, i);
1625 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1627 if (field_is_static (&TYPE_FIELD (t, i)))
1629 gen_static_field (exp->gdbarch, ax, value, t, i);
1630 if (value->optimized_out)
1631 error (_("static field `%s' has been "
1632 "optimized out, cannot use"),
1636 if (TYPE_FIELD_PACKED (t, i))
1637 error (_("pointers to bitfield members not allowed"));
1639 /* FIXME we need a way to do "want_address" equivalent */
1641 error (_("Cannot reference non-static field \"%s\""), fieldname);
1645 /* FIXME add other scoped-reference cases here */
1647 /* Do a last-ditch lookup. */
1648 return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1651 /* C++: Return the member NAME of the namespace given by the type
1655 gen_namespace_elt (struct expression *exp,
1656 struct agent_expr *ax, struct axs_value *value,
1657 const struct type *curtype, char *name)
1659 int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1662 error (_("No symbol \"%s\" in namespace \"%s\"."),
1663 name, TYPE_TAG_NAME (curtype));
1668 /* A helper function used by value_namespace_elt and
1669 value_struct_elt_for_reference. It looks up NAME inside the
1670 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1671 is a class and NAME refers to a type in CURTYPE itself (as opposed
1672 to, say, some base class of CURTYPE). */
1675 gen_maybe_namespace_elt (struct expression *exp,
1676 struct agent_expr *ax, struct axs_value *value,
1677 const struct type *curtype, char *name)
1679 const char *namespace_name = TYPE_TAG_NAME (curtype);
1682 sym = cp_lookup_symbol_namespace (namespace_name, name,
1683 block_for_pc (ax->scope),
1689 gen_var_ref (exp->gdbarch, ax, value, sym);
1691 if (value->optimized_out)
1692 error (_("`%s' has been optimized out, cannot use"),
1693 SYMBOL_PRINT_NAME (sym));
1700 gen_aggregate_elt_ref (struct expression *exp,
1701 struct agent_expr *ax, struct axs_value *value,
1702 struct type *type, char *field,
1703 char *operator_name, char *operand_name)
1705 switch (TYPE_CODE (type))
1707 case TYPE_CODE_STRUCT:
1708 case TYPE_CODE_UNION:
1709 return gen_struct_elt_for_reference (exp, ax, value, type, field);
1711 case TYPE_CODE_NAMESPACE:
1712 return gen_namespace_elt (exp, ax, value, type, field);
1715 internal_error (__FILE__, __LINE__,
1716 _("non-aggregate type in gen_aggregate_elt_ref"));
1722 /* Generate code for GDB's magical `repeat' operator.
1723 LVALUE @ INT creates an array INT elements long, and whose elements
1724 have the same type as LVALUE, located in memory so that LVALUE is
1725 its first element. For example, argv[0]@argc gives you the array
1726 of command-line arguments.
1728 Unfortunately, because we have to know the types before we actually
1729 have a value for the expression, we can't implement this perfectly
1730 without changing the type system, having values that occupy two
1731 stack slots, doing weird things with sizeof, etc. So we require
1732 the right operand to be a constant expression. */
1734 gen_repeat (struct expression *exp, union exp_element **pc,
1735 struct agent_expr *ax, struct axs_value *value)
1737 struct axs_value value1;
1739 /* We don't want to turn this into an rvalue, so no conversions
1741 gen_expr (exp, pc, ax, &value1);
1742 if (value1.kind != axs_lvalue_memory)
1743 error (_("Left operand of `@' must be an object in memory."));
1745 /* Evaluate the length; it had better be a constant. */
1747 struct value *v = const_expr (pc);
1751 error (_("Right operand of `@' must be a "
1752 "constant, in agent expressions."));
1753 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1754 error (_("Right operand of `@' must be an integer."));
1755 length = value_as_long (v);
1757 error (_("Right operand of `@' must be positive."));
1759 /* The top of the stack is already the address of the object, so
1760 all we need to do is frob the type of the lvalue. */
1762 /* FIXME-type-allocation: need a way to free this type when we are
1765 = lookup_array_range_type (value1.type, 0, length - 1);
1767 value->kind = axs_lvalue_memory;
1768 value->type = array;
1774 /* Emit code for the `sizeof' operator.
1775 *PC should point at the start of the operand expression; we advance it
1776 to the first instruction after the operand. */
1778 gen_sizeof (struct expression *exp, union exp_element **pc,
1779 struct agent_expr *ax, struct axs_value *value,
1780 struct type *size_type)
1782 /* We don't care about the value of the operand expression; we only
1783 care about its type. However, in the current arrangement, the
1784 only way to find an expression's type is to generate code for it.
1785 So we generate code for the operand, and then throw it away,
1786 replacing it with code that simply pushes its size. */
1787 int start = ax->len;
1789 gen_expr (exp, pc, ax, value);
1791 /* Throw away the code we just generated. */
1794 ax_const_l (ax, TYPE_LENGTH (value->type));
1795 value->kind = axs_rvalue;
1796 value->type = size_type;
1800 /* Generating bytecode from GDB expressions: general recursive thingy */
1803 /* A gen_expr function written by a Gen-X'er guy.
1804 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1806 gen_expr (struct expression *exp, union exp_element **pc,
1807 struct agent_expr *ax, struct axs_value *value)
1809 /* Used to hold the descriptions of operand expressions. */
1810 struct axs_value value1, value2, value3;
1811 enum exp_opcode op = (*pc)[0].opcode, op2;
1812 int if1, go1, if2, go2, end;
1813 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
1815 /* If we're looking at a constant expression, just push its value. */
1817 struct value *v = maybe_const_expr (pc);
1821 ax_const_l (ax, value_as_long (v));
1822 value->kind = axs_rvalue;
1823 value->type = check_typedef (value_type (v));
1828 /* Otherwise, go ahead and generate code for it. */
1831 /* Binary arithmetic operators. */
1839 case BINOP_SUBSCRIPT:
1840 case BINOP_BITWISE_AND:
1841 case BINOP_BITWISE_IOR:
1842 case BINOP_BITWISE_XOR:
1844 case BINOP_NOTEQUAL:
1850 gen_expr (exp, pc, ax, &value1);
1851 gen_usual_unary (exp, ax, &value1);
1852 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1855 case BINOP_LOGICAL_AND:
1857 /* Generate the obvious sequence of tests and jumps. */
1858 gen_expr (exp, pc, ax, &value1);
1859 gen_usual_unary (exp, ax, &value1);
1860 if1 = ax_goto (ax, aop_if_goto);
1861 go1 = ax_goto (ax, aop_goto);
1862 ax_label (ax, if1, ax->len);
1863 gen_expr (exp, pc, ax, &value2);
1864 gen_usual_unary (exp, ax, &value2);
1865 if2 = ax_goto (ax, aop_if_goto);
1866 go2 = ax_goto (ax, aop_goto);
1867 ax_label (ax, if2, ax->len);
1869 end = ax_goto (ax, aop_goto);
1870 ax_label (ax, go1, ax->len);
1871 ax_label (ax, go2, ax->len);
1873 ax_label (ax, end, ax->len);
1874 value->kind = axs_rvalue;
1875 value->type = int_type;
1878 case BINOP_LOGICAL_OR:
1880 /* Generate the obvious sequence of tests and jumps. */
1881 gen_expr (exp, pc, ax, &value1);
1882 gen_usual_unary (exp, ax, &value1);
1883 if1 = ax_goto (ax, aop_if_goto);
1884 gen_expr (exp, pc, ax, &value2);
1885 gen_usual_unary (exp, ax, &value2);
1886 if2 = ax_goto (ax, aop_if_goto);
1888 end = ax_goto (ax, aop_goto);
1889 ax_label (ax, if1, ax->len);
1890 ax_label (ax, if2, ax->len);
1892 ax_label (ax, end, ax->len);
1893 value->kind = axs_rvalue;
1894 value->type = int_type;
1899 gen_expr (exp, pc, ax, &value1);
1900 gen_usual_unary (exp, ax, &value1);
1901 /* For (A ? B : C), it's easiest to generate subexpression
1902 bytecodes in order, but if_goto jumps on true, so we invert
1903 the sense of A. Then we can do B by dropping through, and
1905 gen_logical_not (ax, &value1, int_type);
1906 if1 = ax_goto (ax, aop_if_goto);
1907 gen_expr (exp, pc, ax, &value2);
1908 gen_usual_unary (exp, ax, &value2);
1909 end = ax_goto (ax, aop_goto);
1910 ax_label (ax, if1, ax->len);
1911 gen_expr (exp, pc, ax, &value3);
1912 gen_usual_unary (exp, ax, &value3);
1913 ax_label (ax, end, ax->len);
1914 /* This is arbitary - what if B and C are incompatible types? */
1915 value->type = value2.type;
1916 value->kind = value2.kind;
1921 if ((*pc)[0].opcode == OP_INTERNALVAR)
1923 char *name = internalvar_name ((*pc)[1].internalvar);
1924 struct trace_state_variable *tsv;
1927 gen_expr (exp, pc, ax, value);
1928 tsv = find_trace_state_variable (name);
1931 ax_tsv (ax, aop_setv, tsv->number);
1933 ax_tsv (ax, aop_tracev, tsv->number);
1936 error (_("$%s is not a trace state variable, "
1937 "may not assign to it"), name);
1940 error (_("May only assign to trace state variables"));
1943 case BINOP_ASSIGN_MODIFY:
1945 op2 = (*pc)[0].opcode;
1948 if ((*pc)[0].opcode == OP_INTERNALVAR)
1950 char *name = internalvar_name ((*pc)[1].internalvar);
1951 struct trace_state_variable *tsv;
1954 tsv = find_trace_state_variable (name);
1957 /* The tsv will be the left half of the binary operation. */
1958 ax_tsv (ax, aop_getv, tsv->number);
1960 ax_tsv (ax, aop_tracev, tsv->number);
1961 /* Trace state variables are always 64-bit integers. */
1962 value1.kind = axs_rvalue;
1963 value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1964 /* Now do right half of expression. */
1965 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1966 /* We have a result of the binary op, set the tsv. */
1967 ax_tsv (ax, aop_setv, tsv->number);
1969 ax_tsv (ax, aop_tracev, tsv->number);
1972 error (_("$%s is not a trace state variable, "
1973 "may not assign to it"), name);
1976 error (_("May only assign to trace state variables"));
1979 /* Note that we need to be a little subtle about generating code
1980 for comma. In C, we can do some optimizations here because
1981 we know the left operand is only being evaluated for effect.
1982 However, if the tracing kludge is in effect, then we always
1983 need to evaluate the left hand side fully, so that all the
1984 variables it mentions get traced. */
1987 gen_expr (exp, pc, ax, &value1);
1988 /* Don't just dispose of the left operand. We might be tracing,
1989 in which case we want to emit code to trace it if it's an
1991 gen_traced_pop (exp->gdbarch, ax, &value1);
1992 gen_expr (exp, pc, ax, value);
1993 /* It's the consumer's responsibility to trace the right operand. */
1996 case OP_LONG: /* some integer constant */
1998 struct type *type = (*pc)[1].type;
1999 LONGEST k = (*pc)[2].longconst;
2002 gen_int_literal (ax, value, k, type);
2007 gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
2009 if (value->optimized_out)
2010 error (_("`%s' has been optimized out, cannot use"),
2011 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
2018 const char *name = &(*pc)[2].string;
2021 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
2022 reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
2024 internal_error (__FILE__, __LINE__,
2025 _("Register $%s not available"), name);
2026 /* No support for tracing user registers yet. */
2027 if (reg >= gdbarch_num_regs (exp->gdbarch)
2028 + gdbarch_num_pseudo_regs (exp->gdbarch))
2029 error (_("'%s' is a user-register; "
2030 "GDB cannot yet trace user-register contents."),
2032 value->kind = axs_lvalue_register;
2034 value->type = register_type (exp->gdbarch, reg);
2038 case OP_INTERNALVAR:
2040 struct internalvar *var = (*pc)[1].internalvar;
2041 const char *name = internalvar_name (var);
2042 struct trace_state_variable *tsv;
2045 tsv = find_trace_state_variable (name);
2048 ax_tsv (ax, aop_getv, tsv->number);
2050 ax_tsv (ax, aop_tracev, tsv->number);
2051 /* Trace state variables are always 64-bit integers. */
2052 value->kind = axs_rvalue;
2053 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2055 else if (! compile_internalvar_to_ax (var, ax, value))
2056 error (_("$%s is not a trace state variable; GDB agent "
2057 "expressions cannot use convenience variables."), name);
2061 /* Weirdo operator: see comments for gen_repeat for details. */
2063 /* Note that gen_repeat handles its own argument evaluation. */
2065 gen_repeat (exp, pc, ax, value);
2070 struct type *type = (*pc)[1].type;
2073 gen_expr (exp, pc, ax, value);
2074 gen_cast (ax, value, type);
2080 struct type *type = check_typedef ((*pc)[1].type);
2083 gen_expr (exp, pc, ax, value);
2085 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2086 already have the right value on the stack. For
2087 axs_lvalue_register, we must convert. */
2088 if (value->kind == axs_lvalue_register)
2089 require_rvalue (ax, value);
2092 value->kind = axs_lvalue_memory;
2098 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2099 gen_expr (exp, pc, ax, value);
2100 gen_usual_unary (exp, ax, value);
2105 /* -FOO is equivalent to 0 - FOO. */
2106 gen_int_literal (ax, &value1, 0,
2107 builtin_type (exp->gdbarch)->builtin_int);
2108 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2109 gen_expr (exp, pc, ax, &value2);
2110 gen_usual_unary (exp, ax, &value2);
2111 gen_usual_arithmetic (exp, ax, &value1, &value2);
2112 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2115 case UNOP_LOGICAL_NOT:
2117 gen_expr (exp, pc, ax, value);
2118 gen_usual_unary (exp, ax, value);
2119 gen_logical_not (ax, value, int_type);
2122 case UNOP_COMPLEMENT:
2124 gen_expr (exp, pc, ax, value);
2125 gen_usual_unary (exp, ax, value);
2126 gen_integral_promotions (exp, ax, value);
2127 gen_complement (ax, value);
2132 gen_expr (exp, pc, ax, value);
2133 gen_usual_unary (exp, ax, value);
2134 if (!pointer_type (value->type))
2135 error (_("Argument of unary `*' is not a pointer."));
2136 gen_deref (ax, value);
2141 gen_expr (exp, pc, ax, value);
2142 gen_address_of (ax, value);
2147 /* Notice that gen_sizeof handles its own operand, unlike most
2148 of the other unary operator functions. This is because we
2149 have to throw away the code we generate. */
2150 gen_sizeof (exp, pc, ax, value,
2151 builtin_type (exp->gdbarch)->builtin_int);
2154 case STRUCTOP_STRUCT:
2157 int length = (*pc)[1].longconst;
2158 char *name = &(*pc)[2].string;
2160 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2161 gen_expr (exp, pc, ax, value);
2162 if (op == STRUCTOP_STRUCT)
2163 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2164 else if (op == STRUCTOP_PTR)
2165 gen_struct_ref (exp, ax, value, name, "->",
2166 "pointer to a structure or union");
2168 /* If this `if' chain doesn't handle it, then the case list
2169 shouldn't mention it, and we shouldn't be here. */
2170 internal_error (__FILE__, __LINE__,
2171 _("gen_expr: unhandled struct case"));
2177 struct symbol *sym, *func;
2179 const struct language_defn *lang;
2181 b = block_for_pc (ax->scope);
2182 func = block_linkage_function (b);
2183 lang = language_def (SYMBOL_LANGUAGE (func));
2185 sym = lookup_language_this (lang, b);
2187 error (_("no `%s' found"), lang->la_name_of_this);
2189 gen_var_ref (exp->gdbarch, ax, value, sym);
2191 if (value->optimized_out)
2192 error (_("`%s' has been optimized out, cannot use"),
2193 SYMBOL_PRINT_NAME (sym));
2201 struct type *type = (*pc)[1].type;
2202 int length = longest_to_int ((*pc)[2].longconst);
2203 char *name = &(*pc)[3].string;
2206 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2209 error (_("There is no field named %s"), name);
2210 (*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 (exp->gdbarch)->builtin_int;
2235 gen_expr (exp, pc, ax, value2);
2236 gen_usual_unary (exp, ax, value2);
2237 gen_usual_arithmetic (exp, 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 (exp->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);
2319 gen_deref (ax, value);
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,
2384 struct cleanup *old_chain = 0;
2385 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2386 struct axs_value value;
2388 old_chain = make_cleanup_free_agent_expr (ax);
2391 gen_var_ref (gdbarch, ax, &value, var);
2393 /* If there is no actual variable to trace, flag it by returning
2394 an empty agent expression. */
2395 if (value.optimized_out)
2397 do_cleanups (old_chain);
2401 /* Make sure we record the final object, and get rid of it. */
2402 gen_traced_pop (gdbarch, ax, &value);
2404 /* Oh, and terminate. */
2405 ax_simple (ax, aop_end);
2407 /* We have successfully built the agent expr, so cancel the cleanup
2408 request. If we add more cleanups that we always want done, this
2409 will have to get more complicated. */
2410 discard_cleanups (old_chain);
2414 /* Generating bytecode from GDB expressions: driver */
2416 /* Given a GDB expression EXPR, return bytecode to trace its value.
2417 The result will use the `trace' and `trace_quick' bytecodes to
2418 record the value of all memory touched by the expression. The
2419 caller can then use the ax_reqs function to discover which
2420 registers it relies upon. */
2422 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
2424 struct cleanup *old_chain = 0;
2425 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2426 union exp_element *pc;
2427 struct axs_value value;
2429 old_chain = make_cleanup_free_agent_expr (ax);
2433 value.optimized_out = 0;
2434 gen_expr (expr, &pc, ax, &value);
2436 /* Make sure we record the final object, and get rid of it. */
2437 gen_traced_pop (expr->gdbarch, ax, &value);
2439 /* Oh, and terminate. */
2440 ax_simple (ax, aop_end);
2442 /* We have successfully built the agent expr, so cancel the cleanup
2443 request. If we add more cleanups that we always want done, this
2444 will have to get more complicated. */
2445 discard_cleanups (old_chain);
2449 /* Given a GDB expression EXPR, return a bytecode sequence that will
2450 evaluate and return a result. The bytecodes will do a direct
2451 evaluation, using the current data on the target, rather than
2452 recording blocks of memory and registers for later use, as
2453 gen_trace_for_expr does. The generated bytecode sequence leaves
2454 the result of expression evaluation on the top of the stack. */
2457 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2459 struct cleanup *old_chain = 0;
2460 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2461 union exp_element *pc;
2462 struct axs_value value;
2464 old_chain = make_cleanup_free_agent_expr (ax);
2468 value.optimized_out = 0;
2469 gen_expr (expr, &pc, ax, &value);
2471 require_rvalue (ax, &value);
2473 /* Oh, and terminate. */
2474 ax_simple (ax, aop_end);
2476 /* We have successfully built the agent expr, so cancel the cleanup
2477 request. If we add more cleanups that we always want done, this
2478 will have to get more complicated. */
2479 discard_cleanups (old_chain);
2484 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch)
2486 struct cleanup *old_chain = 0;
2487 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2488 struct axs_value value;
2490 old_chain = make_cleanup_free_agent_expr (ax);
2494 gdbarch_gen_return_address (gdbarch, ax, &value, scope);
2496 /* Make sure we record the final object, and get rid of it. */
2497 gen_traced_pop (gdbarch, ax, &value);
2499 /* Oh, and terminate. */
2500 ax_simple (ax, aop_end);
2502 /* We have successfully built the agent expr, so cancel the cleanup
2503 request. If we add more cleanups that we always want done, this
2504 will have to get more complicated. */
2505 discard_cleanups (old_chain);
2509 /* Given a collection of printf-style arguments, generate code to
2510 evaluate the arguments and pass everything to a special
2514 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2515 CORE_ADDR function, LONGEST channel,
2516 char *format, int fmtlen,
2517 struct format_piece *frags,
2518 int nargs, struct expression **exprs)
2520 struct expression *expr;
2521 struct cleanup *old_chain = 0;
2522 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2523 union exp_element *pc;
2524 struct axs_value value;
2525 int i, tem, bot, fr, flen;
2528 old_chain = make_cleanup_free_agent_expr (ax);
2530 /* Evaluate and push the args on the stack in reverse order,
2531 for simplicity of collecting them on the target side. */
2532 for (tem = nargs - 1; tem >= 0; --tem)
2534 pc = exprs[tem]->elts;
2535 /* We're computing values, not doing side effects. */
2537 value.optimized_out = 0;
2538 gen_expr (exprs[tem], &pc, ax, &value);
2539 require_rvalue (ax, &value);
2542 /* Push function and channel. */
2543 ax_const_l (ax, channel);
2544 ax_const_l (ax, function);
2546 /* Issue the printf bytecode proper. */
2547 ax_simple (ax, aop_printf);
2548 ax_simple (ax, nargs);
2549 ax_string (ax, format, fmtlen);
2551 /* And terminate. */
2552 ax_simple (ax, aop_end);
2554 /* We have successfully built the agent expr, so cancel the cleanup
2555 request. If we add more cleanups that we always want done, this
2556 will have to get more complicated. */
2557 discard_cleanups (old_chain);
2563 agent_command (char *exp, int from_tty)
2565 struct cleanup *old_chain = 0;
2566 struct expression *expr;
2567 struct agent_expr *agent;
2568 struct frame_info *fi = get_current_frame (); /* need current scope */
2570 /* We don't deal with overlay debugging at the moment. We need to
2571 think more carefully about this. If you copy this code into
2572 another command, change the error message; the user shouldn't
2573 have to know anything about agent expressions. */
2574 if (overlay_debugging)
2575 error (_("GDB can't do agent expression translation with overlays."));
2578 error_no_arg (_("expression to translate"));
2580 trace_string_kludge = 0;
2582 exp = decode_agent_options (exp);
2584 /* Recognize the return address collection directive specially. Note
2585 that it is not really an expression of any sort. */
2586 if (strcmp (exp, "$_ret") == 0)
2588 agent = gen_trace_for_return_address (get_frame_pc (fi),
2589 get_current_arch ());
2590 old_chain = make_cleanup_free_agent_expr (agent);
2594 expr = parse_expression (exp);
2595 old_chain = make_cleanup (free_current_contents, &expr);
2596 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
2597 make_cleanup_free_agent_expr (agent);
2601 ax_print (gdb_stdout, agent);
2603 /* It would be nice to call ax_reqs here to gather some general info
2604 about the expression, and then print out the result. */
2606 do_cleanups (old_chain);
2610 /* Parse the given expression, compile it into an agent expression
2611 that does direct evaluation, and display the resulting
2615 agent_eval_command (char *exp, int from_tty)
2617 struct cleanup *old_chain = 0;
2618 struct expression *expr;
2619 struct agent_expr *agent;
2620 struct frame_info *fi = get_current_frame (); /* need current scope */
2622 /* We don't deal with overlay debugging at the moment. We need to
2623 think more carefully about this. If you copy this code into
2624 another command, change the error message; the user shouldn't
2625 have to know anything about agent expressions. */
2626 if (overlay_debugging)
2627 error (_("GDB can't do agent expression translation with overlays."));
2630 error_no_arg (_("expression to translate"));
2632 expr = parse_expression (exp);
2633 old_chain = make_cleanup (free_current_contents, &expr);
2634 agent = gen_eval_for_expr (get_frame_pc (fi), expr);
2635 make_cleanup_free_agent_expr (agent);
2637 ax_print (gdb_stdout, agent);
2639 /* It would be nice to call ax_reqs here to gather some general info
2640 about the expression, and then print out the result. */
2642 do_cleanups (old_chain);
2645 /* Parse the given expression, compile it into an agent expression
2646 that does a printf, and display the resulting expression. */
2649 maint_agent_printf_command (char *exp, int from_tty)
2651 struct cleanup *old_chain = 0;
2652 struct expression *expr;
2653 struct expression *argvec[100];
2654 struct agent_expr *agent;
2655 struct frame_info *fi = get_current_frame (); /* need current scope */
2657 char *format_start, *format_end;
2658 struct format_piece *fpieces;
2661 /* We don't deal with overlay debugging at the moment. We need to
2662 think more carefully about this. If you copy this code into
2663 another command, change the error message; the user shouldn't
2664 have to know anything about agent expressions. */
2665 if (overlay_debugging)
2666 error (_("GDB can't do agent expression translation with overlays."));
2669 error_no_arg (_("expression to translate"));
2673 cmdrest = skip_spaces (cmdrest);
2675 if (*cmdrest++ != '"')
2676 error (_("Must start with a format string."));
2678 format_start = cmdrest;
2680 fpieces = parse_format_string (&cmdrest);
2682 old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2684 format_end = cmdrest;
2686 if (*cmdrest++ != '"')
2687 error (_("Bad format string, non-terminated '\"'."));
2689 cmdrest = skip_spaces (cmdrest);
2691 if (*cmdrest != ',' && *cmdrest != 0)
2692 error (_("Invalid argument syntax"));
2694 if (*cmdrest == ',')
2696 cmdrest = skip_spaces (cmdrest);
2699 while (*cmdrest != '\0')
2704 expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2705 argvec[nargs] = expr;
2708 if (*cmdrest == ',')
2710 /* else complain? */
2714 agent = gen_printf (get_frame_pc (fi), get_current_arch (), 0, 0,
2715 format_start, format_end - format_start,
2716 fpieces, nargs, argvec);
2717 make_cleanup_free_agent_expr (agent);
2719 ax_print (gdb_stdout, agent);
2721 /* It would be nice to call ax_reqs here to gather some general info
2722 about the expression, and then print out the result. */
2724 do_cleanups (old_chain);
2729 /* Initialization code. */
2731 void _initialize_ax_gdb (void);
2733 _initialize_ax_gdb (void)
2735 add_cmd ("agent", class_maintenance, agent_command,
2736 _("Translate an expression into "
2737 "remote agent bytecode for tracing."),
2740 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2741 _("Translate an expression into remote "
2742 "agent bytecode for evaluation."),
2745 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2746 _("Translate an expression into remote "
2747 "agent bytecode for evaluation and display the bytecodes."),