1 /* GDB-specific functions for operating on agent expressions
2 Copyright 1998, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include "expression.h"
34 /* To make sense of this file, you should read doc/agentexpr.texi.
35 Then look at the types and enums in ax-gdb.h. For the code itself,
36 look at gen_expr, towards the bottom; that's the main function that
37 looks at the GDB expressions and calls everything else to generate
40 I'm beginning to wonder whether it wouldn't be nicer to internally
41 generate trees, with types, and then spit out the bytecode in
42 linear form afterwards; we could generate fewer `swap', `ext', and
43 `zero_ext' bytecodes that way; it would make good constant folding
44 easier, too. But at the moment, I think we should be willing to
45 pay for the simplicity of this code with less-than-optimal bytecode
48 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
52 /* Prototypes for local functions. */
54 /* There's a standard order to the arguments of these functions:
55 union exp_element ** --- pointer into expression
56 struct agent_expr * --- agent expression buffer to generate code into
57 struct axs_value * --- describes value left on top of stack */
59 static struct value *const_var_ref (struct symbol *var);
60 static struct value *const_expr (union exp_element **pc);
61 static struct value *maybe_const_expr (union exp_element **pc);
63 static void gen_traced_pop (struct agent_expr *, struct axs_value *);
65 static void gen_sign_extend (struct agent_expr *, struct type *);
66 static void gen_extend (struct agent_expr *, struct type *);
67 static void gen_fetch (struct agent_expr *, struct type *);
68 static void gen_left_shift (struct agent_expr *, int);
71 static void gen_frame_args_address (struct agent_expr *);
72 static void gen_frame_locals_address (struct agent_expr *);
73 static void gen_offset (struct agent_expr *ax, int offset);
74 static void gen_sym_offset (struct agent_expr *, struct symbol *);
75 static void gen_var_ref (struct agent_expr *ax,
76 struct axs_value *value, struct symbol *var);
79 static void gen_int_literal (struct agent_expr *ax,
80 struct axs_value *value,
81 LONGEST k, struct type *type);
84 static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
85 static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
86 static int type_wider_than (struct type *type1, struct type *type2);
87 static struct type *max_type (struct type *type1, struct type *type2);
88 static void gen_conversion (struct agent_expr *ax,
89 struct type *from, struct type *to);
90 static int is_nontrivial_conversion (struct type *from, struct type *to);
91 static void gen_usual_arithmetic (struct agent_expr *ax,
92 struct axs_value *value1,
93 struct axs_value *value2);
94 static void gen_integral_promotions (struct agent_expr *ax,
95 struct axs_value *value);
96 static void gen_cast (struct agent_expr *ax,
97 struct axs_value *value, struct type *type);
98 static void gen_scale (struct agent_expr *ax,
99 enum agent_op op, struct type *type);
100 static void gen_add (struct agent_expr *ax,
101 struct axs_value *value,
102 struct axs_value *value1,
103 struct axs_value *value2, char *name);
104 static void gen_sub (struct agent_expr *ax,
105 struct axs_value *value,
106 struct axs_value *value1, struct axs_value *value2);
107 static void gen_binop (struct agent_expr *ax,
108 struct axs_value *value,
109 struct axs_value *value1,
110 struct axs_value *value2,
112 enum agent_op op_unsigned, int may_carry, char *name);
113 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value);
114 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
115 static void gen_deref (struct agent_expr *, struct axs_value *);
116 static void gen_address_of (struct agent_expr *, struct axs_value *);
117 static int find_field (struct type *type, char *name);
118 static void gen_bitfield_ref (struct agent_expr *ax,
119 struct axs_value *value,
120 struct type *type, int start, int end);
121 static void gen_struct_ref (struct agent_expr *ax,
122 struct axs_value *value,
124 char *operator_name, char *operand_name);
125 static void gen_repeat (union exp_element **pc,
126 struct agent_expr *ax, struct axs_value *value);
127 static void gen_sizeof (union exp_element **pc,
128 struct agent_expr *ax, struct axs_value *value);
129 static void gen_expr (union exp_element **pc,
130 struct agent_expr *ax, struct axs_value *value);
132 static void print_axs_value (struct ui_file *f, struct axs_value * value);
133 static void agent_command (char *exp, int from_tty);
136 /* Detecting constant expressions. */
138 /* If the variable reference at *PC is a constant, return its value.
139 Otherwise, return zero.
141 Hey, Wally! How can a variable reference be a constant?
143 Well, Beav, this function really handles the OP_VAR_VALUE operator,
144 not specifically variable references. GDB uses OP_VAR_VALUE to
145 refer to any kind of symbolic reference: function names, enum
146 elements, and goto labels are all handled through the OP_VAR_VALUE
147 operator, even though they're constants. It makes sense given the
150 Gee, Wally, don'cha wonder sometimes if data representations that
151 subvert commonly accepted definitions of terms in favor of heavily
152 context-specific interpretations are really just a tool of the
153 programming hegemony to preserve their power and exclude the
156 static struct value *
160 struct type *type = SYMBOL_TYPE (var);
162 switch (SYMBOL_CLASS (var))
165 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
168 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
176 /* If the expression starting at *PC has a constant value, return it.
177 Otherwise, return zero. If we return a value, then *PC will be
178 advanced to the end of it. If we return zero, *PC could be
180 static struct value *
182 union exp_element **pc;
184 enum exp_opcode op = (*pc)->opcode;
191 struct type *type = (*pc)[1].type;
192 LONGEST k = (*pc)[2].longconst;
194 return value_from_longest (type, k);
199 struct value *v = const_var_ref ((*pc)[2].symbol);
204 /* We could add more operators in here. */
208 v1 = const_expr (pc);
210 return value_neg (v1);
220 /* Like const_expr, but guarantee also that *PC is undisturbed if the
221 expression is not constant. */
222 static struct value *
223 maybe_const_expr (pc)
224 union exp_element **pc;
226 union exp_element *tentative_pc = *pc;
227 struct value *v = const_expr (&tentative_pc);
229 /* If we got a value, then update the real PC. */
237 /* Generating bytecode from GDB expressions: general assumptions */
239 /* Here are a few general assumptions made throughout the code; if you
240 want to make a change that contradicts one of these, then you'd
241 better scan things pretty thoroughly.
243 - We assume that all values occupy one stack element. For example,
244 sometimes we'll swap to get at the left argument to a binary
245 operator. If we decide that void values should occupy no stack
246 elements, or that synthetic arrays (whose size is determined at
247 run time, created by the `@' operator) should occupy two stack
248 elements (address and length), then this will cause trouble.
250 - We assume the stack elements are infinitely wide, and that we
251 don't have to worry what happens if the user requests an
252 operation that is wider than the actual interpreter's stack.
253 That is, it's up to the interpreter to handle directly all the
254 integer widths the user has access to. (Woe betide the language
257 - We don't support side effects. Thus, we don't have to worry about
258 GCC's generalized lvalues, function calls, etc.
260 - We don't support floating point. Many places where we switch on
261 some type don't bother to include cases for floating point; there
262 may be even more subtle ways this assumption exists. For
263 example, the arguments to % must be integers.
265 - We assume all subexpressions have a static, unchanging type. If
266 we tried to support convenience variables, this would be a
269 - All values on the stack should always be fully zero- or
272 (I wasn't sure whether to choose this or its opposite --- that
273 only addresses are assumed extended --- but it turns out that
274 neither convention completely eliminates spurious extend
275 operations (if everything is always extended, then you have to
276 extend after add, because it could overflow; if nothing is
277 extended, then you end up producing extends whenever you change
278 sizes), and this is simpler.) */
281 /* Generating bytecode from GDB expressions: the `trace' kludge */
283 /* The compiler in this file is a general-purpose mechanism for
284 translating GDB expressions into bytecode. One ought to be able to
285 find a million and one uses for it.
287 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
288 of expediency. Let he who is without sin cast the first stone.
290 For the data tracing facility, we need to insert `trace' bytecodes
291 before each data fetch; this records all the memory that the
292 expression touches in the course of evaluation, so that memory will
293 be available when the user later tries to evaluate the expression
296 This should be done (I think) in a post-processing pass, that walks
297 an arbitrary agent expression and inserts `trace' operations at the
298 appropriate points. But it's much faster to just hack them
299 directly into the code. And since we're in a crunch, that's what
302 Setting the flag trace_kludge to non-zero enables the code that
303 emits the trace bytecodes at the appropriate points. */
304 static int trace_kludge;
306 /* Trace the lvalue on the stack, if it needs it. In either case, pop
307 the value. Useful on the left side of a comma, and at the end of
308 an expression being used for tracing. */
310 gen_traced_pop (ax, value)
311 struct agent_expr *ax;
312 struct axs_value *value;
318 /* We don't trace rvalues, just the lvalues necessary to
319 produce them. So just dispose of this value. */
320 ax_simple (ax, aop_pop);
323 case axs_lvalue_memory:
325 int length = TYPE_LENGTH (value->type);
327 /* There's no point in trying to use a trace_quick bytecode
328 here, since "trace_quick SIZE pop" is three bytes, whereas
329 "const8 SIZE trace" is also three bytes, does the same
330 thing, and the simplest code which generates that will also
331 work correctly for objects with large sizes. */
332 ax_const_l (ax, length);
333 ax_simple (ax, aop_trace);
337 case axs_lvalue_register:
338 /* We need to mention the register somewhere in the bytecode,
339 so ax_reqs will pick it up and add it to the mask of
341 ax_reg (ax, value->u.reg);
342 ax_simple (ax, aop_pop);
346 /* If we're not tracing, just pop the value. */
347 ax_simple (ax, aop_pop);
352 /* Generating bytecode from GDB expressions: helper functions */
354 /* Assume that the lower bits of the top of the stack is a value of
355 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
357 gen_sign_extend (ax, type)
358 struct agent_expr *ax;
361 /* Do we need to sign-extend this? */
362 if (!TYPE_UNSIGNED (type))
363 ax_ext (ax, type->length * TARGET_CHAR_BIT);
367 /* Assume the lower bits of the top of the stack hold a value of type
368 TYPE, and the upper bits are garbage. Sign-extend or truncate as
371 gen_extend (ax, type)
372 struct agent_expr *ax;
375 int bits = type->length * TARGET_CHAR_BIT;
377 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
381 /* Assume that the top of the stack contains a value of type "pointer
382 to TYPE"; generate code to fetch its value. Note that TYPE is the
383 target type, not the pointer type. */
386 struct agent_expr *ax;
391 /* Record the area of memory we're about to fetch. */
392 ax_trace_quick (ax, TYPE_LENGTH (type));
401 /* It's a scalar value, so we know how to dereference it. How
402 many bytes long is it? */
403 switch (type->length)
405 case 8 / TARGET_CHAR_BIT:
406 ax_simple (ax, aop_ref8);
408 case 16 / TARGET_CHAR_BIT:
409 ax_simple (ax, aop_ref16);
411 case 32 / TARGET_CHAR_BIT:
412 ax_simple (ax, aop_ref32);
414 case 64 / TARGET_CHAR_BIT:
415 ax_simple (ax, aop_ref64);
418 /* Either our caller shouldn't have asked us to dereference
419 that pointer (other code's fault), or we're not
420 implementing something we should be (this code's fault).
421 In any case, it's a bug the user shouldn't see. */
423 internal_error ("ax-gdb.c (gen_fetch): strange size");
426 gen_sign_extend (ax, type);
430 /* Either our caller shouldn't have asked us to dereference that
431 pointer (other code's fault), or we're not implementing
432 something we should be (this code's fault). In any case,
433 it's a bug the user shouldn't see. */
434 internal_error ("ax-gdb.c (gen_fetch): bad type code");
439 /* Generate code to left shift the top of the stack by DISTANCE bits, or
440 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
441 unsigned (logical) right shifts. */
443 gen_left_shift (ax, distance)
444 struct agent_expr *ax;
449 ax_const_l (ax, distance);
450 ax_simple (ax, aop_lsh);
452 else if (distance < 0)
454 ax_const_l (ax, -distance);
455 ax_simple (ax, aop_rsh_unsigned);
461 /* Generating bytecode from GDB expressions: symbol references */
463 /* Generate code to push the base address of the argument portion of
464 the top stack frame. */
466 gen_frame_args_address (ax)
467 struct agent_expr *ax;
469 long frame_reg, frame_offset;
471 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
472 ax_reg (ax, frame_reg);
473 gen_offset (ax, frame_offset);
477 /* Generate code to push the base address of the locals portion of the
480 gen_frame_locals_address (ax)
481 struct agent_expr *ax;
483 long frame_reg, frame_offset;
485 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
486 ax_reg (ax, frame_reg);
487 gen_offset (ax, frame_offset);
491 /* Generate code to add OFFSET to the top of the stack. Try to
492 generate short and readable code. We use this for getting to
493 variables on the stack, and structure members. If we were
494 programming in ML, it would be clearer why these are the same
497 gen_offset (ax, offset)
498 struct agent_expr *ax;
501 /* It would suffice to simply push the offset and add it, but this
502 makes it easier to read positive and negative offsets in the
506 ax_const_l (ax, offset);
507 ax_simple (ax, aop_add);
511 ax_const_l (ax, -offset);
512 ax_simple (ax, aop_sub);
517 /* In many cases, a symbol's value is the offset from some other
518 address (stack frame, base register, etc.) Generate code to add
519 VAR's value to the top of the stack. */
521 gen_sym_offset (ax, var)
522 struct agent_expr *ax;
525 gen_offset (ax, SYMBOL_VALUE (var));
529 /* Generate code for a variable reference to AX. The variable is the
530 symbol VAR. Set VALUE to describe the result. */
533 gen_var_ref (ax, value, var)
534 struct agent_expr *ax;
535 struct axs_value *value;
538 /* Dereference any typedefs. */
539 value->type = check_typedef (SYMBOL_TYPE (var));
541 /* I'm imitating the code in read_var_value. */
542 switch (SYMBOL_CLASS (var))
544 case LOC_CONST: /* A constant, like an enum value. */
545 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
546 value->kind = axs_rvalue;
549 case LOC_LABEL: /* A goto label, being used as a value. */
550 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
551 value->kind = axs_rvalue;
554 case LOC_CONST_BYTES:
555 internal_error ("ax-gdb.c (gen_var_ref): LOC_CONST_BYTES symbols are not supported");
557 /* Variable at a fixed location in memory. Easy. */
559 /* Push the address of the variable. */
560 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
561 value->kind = axs_lvalue_memory;
564 case LOC_ARG: /* var lives in argument area of frame */
565 gen_frame_args_address (ax);
566 gen_sym_offset (ax, var);
567 value->kind = axs_lvalue_memory;
570 case LOC_REF_ARG: /* As above, but the frame slot really
571 holds the address of the variable. */
572 gen_frame_args_address (ax);
573 gen_sym_offset (ax, var);
574 /* Don't assume any particular pointer size. */
575 gen_fetch (ax, lookup_pointer_type (builtin_type_void));
576 value->kind = axs_lvalue_memory;
579 case LOC_LOCAL: /* var lives in locals area of frame */
581 gen_frame_locals_address (ax);
582 gen_sym_offset (ax, var);
583 value->kind = axs_lvalue_memory;
586 case LOC_BASEREG: /* relative to some base register */
587 case LOC_BASEREG_ARG:
588 ax_reg (ax, SYMBOL_BASEREG (var));
589 gen_sym_offset (ax, var);
590 value->kind = axs_lvalue_memory;
594 error ("Cannot compute value of typedef `%s'.",
595 SYMBOL_SOURCE_NAME (var));
599 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
600 value->kind = axs_rvalue;
605 /* Don't generate any code at all; in the process of treating
606 this as an lvalue or rvalue, the caller will generate the
608 value->kind = axs_lvalue_register;
609 value->u.reg = SYMBOL_VALUE (var);
612 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
613 register, not on the stack. Simpler than LOC_REGISTER and
614 LOC_REGPARM, because it's just like any other case where the
615 thing has a real address. */
616 case LOC_REGPARM_ADDR:
617 ax_reg (ax, SYMBOL_VALUE (var));
618 value->kind = axs_lvalue_memory;
623 struct minimal_symbol *msym
624 = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
626 error ("Couldn't resolve symbol `%s'.", SYMBOL_SOURCE_NAME (var));
628 /* Push the address of the variable. */
629 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
630 value->kind = axs_lvalue_memory;
634 case LOC_OPTIMIZED_OUT:
635 error ("The variable `%s' has been optimized out.",
636 SYMBOL_SOURCE_NAME (var));
640 error ("Cannot find value of botched symbol `%s'.",
641 SYMBOL_SOURCE_NAME (var));
648 /* Generating bytecode from GDB expressions: literals */
651 gen_int_literal (ax, value, k, type)
652 struct agent_expr *ax;
653 struct axs_value *value;
658 value->kind = axs_rvalue;
664 /* Generating bytecode from GDB expressions: unary conversions, casts */
666 /* Take what's on the top of the stack (as described by VALUE), and
667 try to make an rvalue out of it. Signal an error if we can't do
670 require_rvalue (ax, value)
671 struct agent_expr *ax;
672 struct axs_value *value;
677 /* It's already an rvalue. */
680 case axs_lvalue_memory:
681 /* The top of stack is the address of the object. Dereference. */
682 gen_fetch (ax, value->type);
685 case axs_lvalue_register:
686 /* There's nothing on the stack, but value->u.reg is the
687 register number containing the value.
689 When we add floating-point support, this is going to have to
690 change. What about SPARC register pairs, for example? */
691 ax_reg (ax, value->u.reg);
692 gen_extend (ax, value->type);
696 value->kind = axs_rvalue;
700 /* Assume the top of the stack is described by VALUE, and perform the
701 usual unary conversions. This is motivated by ANSI 6.2.2, but of
702 course GDB expressions are not ANSI; they're the mishmash union of
703 a bunch of languages. Rah.
705 NOTE! This function promises to produce an rvalue only when the
706 incoming value is of an appropriate type. In other words, the
707 consumer of the value this function produces may assume the value
708 is an rvalue only after checking its type.
710 The immediate issue is that if the user tries to use a structure or
711 union as an operand of, say, the `+' operator, we don't want to try
712 to convert that structure to an rvalue; require_rvalue will bomb on
713 structs and unions. Rather, we want to simply pass the struct
714 lvalue through unchanged, and let `+' raise an error. */
717 gen_usual_unary (ax, value)
718 struct agent_expr *ax;
719 struct axs_value *value;
721 /* We don't have to generate any code for the usual integral
722 conversions, since values are always represented as full-width on
723 the stack. Should we tweak the type? */
725 /* Some types require special handling. */
726 switch (value->type->code)
728 /* Functions get converted to a pointer to the function. */
730 value->type = lookup_pointer_type (value->type);
731 value->kind = axs_rvalue; /* Should always be true, but just in case. */
734 /* Arrays get converted to a pointer to their first element, and
735 are no longer an lvalue. */
736 case TYPE_CODE_ARRAY:
738 struct type *elements = TYPE_TARGET_TYPE (value->type);
739 value->type = lookup_pointer_type (elements);
740 value->kind = axs_rvalue;
741 /* We don't need to generate any code; the address of the array
742 is also the address of its first element. */
746 /* Don't try to convert structures and unions to rvalues. Let the
747 consumer signal an error. */
748 case TYPE_CODE_STRUCT:
749 case TYPE_CODE_UNION:
752 /* If the value is an enum, call it an integer. */
754 value->type = builtin_type_int;
758 /* If the value is an lvalue, dereference it. */
759 require_rvalue (ax, value);
763 /* Return non-zero iff the type TYPE1 is considered "wider" than the
764 type TYPE2, according to the rules described in gen_usual_arithmetic. */
766 type_wider_than (type1, type2)
767 struct type *type1, *type2;
769 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
770 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
771 && TYPE_UNSIGNED (type1)
772 && !TYPE_UNSIGNED (type2)));
776 /* Return the "wider" of the two types TYPE1 and TYPE2. */
778 max_type (type1, type2)
779 struct type *type1, *type2;
781 return type_wider_than (type1, type2) ? type1 : type2;
785 /* Generate code to convert a scalar value of type FROM to type TO. */
787 gen_conversion (ax, from, to)
788 struct agent_expr *ax;
789 struct type *from, *to;
791 /* Perhaps there is a more graceful way to state these rules. */
793 /* If we're converting to a narrower type, then we need to clear out
795 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
796 gen_extend (ax, from);
798 /* If the two values have equal width, but different signednesses,
799 then we need to extend. */
800 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
802 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
806 /* If we're converting to a wider type, and becoming unsigned, then
807 we need to zero out any possible sign bits. */
808 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
810 if (TYPE_UNSIGNED (to))
816 /* Return non-zero iff the type FROM will require any bytecodes to be
817 emitted to be converted to the type TO. */
819 is_nontrivial_conversion (from, to)
820 struct type *from, *to;
822 struct agent_expr *ax = new_agent_expr (0);
825 /* Actually generate the code, and see if anything came out. At the
826 moment, it would be trivial to replicate the code in
827 gen_conversion here, but in the future, when we're supporting
828 floating point and the like, it may not be. Doing things this
829 way allows this function to be independent of the logic in
831 gen_conversion (ax, from, to);
832 nontrivial = ax->len > 0;
833 free_agent_expr (ax);
838 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
839 6.2.1.5) for the two operands of an arithmetic operator. This
840 effectively finds a "least upper bound" type for the two arguments,
841 and promotes each argument to that type. *VALUE1 and *VALUE2
842 describe the values as they are passed in, and as they are left. */
844 gen_usual_arithmetic (ax, value1, value2)
845 struct agent_expr *ax;
846 struct axs_value *value1, *value2;
848 /* Do the usual binary conversions. */
849 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
850 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
852 /* The ANSI integral promotions seem to work this way: Order the
853 integer types by size, and then by signedness: an n-bit
854 unsigned type is considered "wider" than an n-bit signed
855 type. Promote to the "wider" of the two types, and always
856 promote at least to int. */
857 struct type *target = max_type (builtin_type_int,
858 max_type (value1->type, value2->type));
860 /* Deal with value2, on the top of the stack. */
861 gen_conversion (ax, value2->type, target);
863 /* Deal with value1, not on the top of the stack. Don't
864 generate the `swap' instructions if we're not actually going
866 if (is_nontrivial_conversion (value1->type, target))
868 ax_simple (ax, aop_swap);
869 gen_conversion (ax, value1->type, target);
870 ax_simple (ax, aop_swap);
873 value1->type = value2->type = target;
878 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
879 the value on the top of the stack, as described by VALUE. Assume
880 the value has integral type. */
882 gen_integral_promotions (ax, value)
883 struct agent_expr *ax;
884 struct axs_value *value;
886 if (!type_wider_than (value->type, builtin_type_int))
888 gen_conversion (ax, value->type, builtin_type_int);
889 value->type = builtin_type_int;
891 else if (!type_wider_than (value->type, builtin_type_unsigned_int))
893 gen_conversion (ax, value->type, builtin_type_unsigned_int);
894 value->type = builtin_type_unsigned_int;
899 /* Generate code for a cast to TYPE. */
901 gen_cast (ax, value, type)
902 struct agent_expr *ax;
903 struct axs_value *value;
906 /* GCC does allow casts to yield lvalues, so this should be fixed
907 before merging these changes into the trunk. */
908 require_rvalue (ax, value);
909 /* Dereference typedefs. */
910 type = check_typedef (type);
915 /* It's implementation-defined, and I'll bet this is what GCC
919 case TYPE_CODE_ARRAY:
920 case TYPE_CODE_STRUCT:
921 case TYPE_CODE_UNION:
923 error ("Illegal type cast: intended type must be scalar.");
926 /* We don't have to worry about the size of the value, because
927 all our integral values are fully sign-extended, and when
928 casting pointers we can do anything we like. Is there any
929 way for us to actually know what GCC actually does with a
935 gen_conversion (ax, value->type, type);
939 /* We could pop the value, and rely on everyone else to check
940 the type and notice that this value doesn't occupy a stack
941 slot. But for now, leave the value on the stack, and
942 preserve the "value == stack element" assumption. */
946 error ("Casts to requested type are not yet implemented.");
954 /* Generating bytecode from GDB expressions: arithmetic */
956 /* Scale the integer on the top of the stack by the size of the target
957 of the pointer type TYPE. */
959 gen_scale (ax, op, type)
960 struct agent_expr *ax;
964 struct type *element = TYPE_TARGET_TYPE (type);
966 if (element->length != 1)
968 ax_const_l (ax, element->length);
974 /* Generate code for an addition; non-trivial because we deal with
975 pointer arithmetic. We set VALUE to describe the result value; we
976 assume VALUE1 and VALUE2 describe the two operands, and that
977 they've undergone the usual binary conversions. Used by both
978 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
980 gen_add (ax, value, value1, value2, name)
981 struct agent_expr *ax;
982 struct axs_value *value, *value1, *value2;
986 if (value1->type->code == TYPE_CODE_INT
987 && value2->type->code == TYPE_CODE_PTR)
989 /* Swap the values and proceed normally. */
990 ax_simple (ax, aop_swap);
991 gen_scale (ax, aop_mul, value2->type);
992 ax_simple (ax, aop_add);
993 gen_extend (ax, value2->type); /* Catch overflow. */
994 value->type = value2->type;
998 else if (value1->type->code == TYPE_CODE_PTR
999 && value2->type->code == TYPE_CODE_INT)
1001 gen_scale (ax, aop_mul, value1->type);
1002 ax_simple (ax, aop_add);
1003 gen_extend (ax, value1->type); /* Catch overflow. */
1004 value->type = value1->type;
1007 /* Must be number + number; the usual binary conversions will have
1008 brought them both to the same width. */
1009 else if (value1->type->code == TYPE_CODE_INT
1010 && value2->type->code == TYPE_CODE_INT)
1012 ax_simple (ax, aop_add);
1013 gen_extend (ax, value1->type); /* Catch overflow. */
1014 value->type = value1->type;
1018 error ("Illegal combination of types in %s.", name);
1020 value->kind = axs_rvalue;
1024 /* Generate code for an addition; non-trivial because we have to deal
1025 with pointer arithmetic. We set VALUE to describe the result
1026 value; we assume VALUE1 and VALUE2 describe the two operands, and
1027 that they've undergone the usual binary conversions. */
1029 gen_sub (ax, value, value1, value2)
1030 struct agent_expr *ax;
1031 struct axs_value *value, *value1, *value2;
1033 if (value1->type->code == TYPE_CODE_PTR)
1035 /* Is it PTR - INT? */
1036 if (value2->type->code == TYPE_CODE_INT)
1038 gen_scale (ax, aop_mul, value1->type);
1039 ax_simple (ax, aop_sub);
1040 gen_extend (ax, value1->type); /* Catch overflow. */
1041 value->type = value1->type;
1044 /* Is it PTR - PTR? Strictly speaking, the types ought to
1045 match, but this is what the normal GDB expression evaluator
1047 else if (value2->type->code == TYPE_CODE_PTR
1048 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1049 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1051 ax_simple (ax, aop_sub);
1052 gen_scale (ax, aop_div_unsigned, value1->type);
1053 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
1057 First argument of `-' is a pointer, but second argument is neither\n\
1058 an integer nor a pointer of the same type.");
1061 /* Must be number + number. */
1062 else if (value1->type->code == TYPE_CODE_INT
1063 && value2->type->code == TYPE_CODE_INT)
1065 ax_simple (ax, aop_sub);
1066 gen_extend (ax, value1->type); /* Catch overflow. */
1067 value->type = value1->type;
1071 error ("Illegal combination of types in subtraction.");
1073 value->kind = axs_rvalue;
1076 /* Generate code for a binary operator that doesn't do pointer magic.
1077 We set VALUE to describe the result value; we assume VALUE1 and
1078 VALUE2 describe the two operands, and that they've undergone the
1079 usual binary conversions. MAY_CARRY should be non-zero iff the
1080 result needs to be extended. NAME is the English name of the
1081 operator, used in error messages */
1083 gen_binop (ax, value, value1, value2, op, op_unsigned, may_carry, name)
1084 struct agent_expr *ax;
1085 struct axs_value *value, *value1, *value2;
1086 enum agent_op op, op_unsigned;
1090 /* We only handle INT op INT. */
1091 if ((value1->type->code != TYPE_CODE_INT)
1092 || (value2->type->code != TYPE_CODE_INT))
1093 error ("Illegal combination of types in %s.", name);
1096 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1098 gen_extend (ax, value1->type); /* catch overflow */
1099 value->type = value1->type;
1100 value->kind = axs_rvalue;
1105 gen_logical_not (ax, value)
1106 struct agent_expr *ax;
1107 struct axs_value *value;
1109 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1110 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1111 error ("Illegal type of operand to `!'.");
1113 gen_usual_unary (ax, value);
1114 ax_simple (ax, aop_log_not);
1115 value->type = builtin_type_int;
1120 gen_complement (ax, value)
1121 struct agent_expr *ax;
1122 struct axs_value *value;
1124 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1125 error ("Illegal type of operand to `~'.");
1127 gen_usual_unary (ax, value);
1128 gen_integral_promotions (ax, value);
1129 ax_simple (ax, aop_bit_not);
1130 gen_extend (ax, value->type);
1135 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1137 /* Dereference the value on the top of the stack. */
1139 gen_deref (ax, value)
1140 struct agent_expr *ax;
1141 struct axs_value *value;
1143 /* The caller should check the type, because several operators use
1144 this, and we don't know what error message to generate. */
1145 if (value->type->code != TYPE_CODE_PTR)
1146 internal_error ("ax-gdb.c (gen_deref): expected a pointer");
1148 /* We've got an rvalue now, which is a pointer. We want to yield an
1149 lvalue, whose address is exactly that pointer. So we don't
1150 actually emit any code; we just change the type from "Pointer to
1151 T" to "T", and mark the value as an lvalue in memory. Leave it
1152 to the consumer to actually dereference it. */
1153 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1154 value->kind = ((value->type->code == TYPE_CODE_FUNC)
1155 ? axs_rvalue : axs_lvalue_memory);
1159 /* Produce the address of the lvalue on the top of the stack. */
1161 gen_address_of (ax, value)
1162 struct agent_expr *ax;
1163 struct axs_value *value;
1165 /* Special case for taking the address of a function. The ANSI
1166 standard describes this as a special case, too, so this
1167 arrangement is not without motivation. */
1168 if (value->type->code == TYPE_CODE_FUNC)
1169 /* The value's already an rvalue on the stack, so we just need to
1171 value->type = lookup_pointer_type (value->type);
1173 switch (value->kind)
1176 error ("Operand of `&' is an rvalue, which has no address.");
1178 case axs_lvalue_register:
1179 error ("Operand of `&' is in a register, and has no address.");
1181 case axs_lvalue_memory:
1182 value->kind = axs_rvalue;
1183 value->type = lookup_pointer_type (value->type);
1189 /* A lot of this stuff will have to change to support C++. But we're
1190 not going to deal with that at the moment. */
1192 /* Find the field in the structure type TYPE named NAME, and return
1193 its index in TYPE's field array. */
1195 find_field (type, name)
1201 CHECK_TYPEDEF (type);
1203 /* Make sure this isn't C++. */
1204 if (TYPE_N_BASECLASSES (type) != 0)
1205 internal_error ("ax-gdb.c (find_field): derived classes supported");
1207 for (i = 0; i < TYPE_NFIELDS (type); i++)
1209 char *this_name = TYPE_FIELD_NAME (type, i);
1211 if (this_name && STREQ (name, this_name))
1214 if (this_name[0] == '\0')
1215 internal_error ("ax-gdb.c (find_field): anonymous unions not supported");
1218 error ("Couldn't find member named `%s' in struct/union `%s'",
1219 name, type->tag_name);
1225 /* Generate code to push the value of a bitfield of a structure whose
1226 address is on the top of the stack. START and END give the
1227 starting and one-past-ending *bit* numbers of the field within the
1230 gen_bitfield_ref (ax, value, type, start, end)
1231 struct agent_expr *ax;
1232 struct axs_value *value;
1236 /* Note that ops[i] fetches 8 << i bits. */
1237 static enum agent_op ops[]
1239 {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1240 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1242 /* We don't want to touch any byte that the bitfield doesn't
1243 actually occupy; we shouldn't make any accesses we're not
1244 explicitly permitted to. We rely here on the fact that the
1245 bytecode `ref' operators work on unaligned addresses.
1247 It takes some fancy footwork to get the stack to work the way
1248 we'd like. Say we're retrieving a bitfield that requires three
1249 fetches. Initially, the stack just contains the address:
1251 For the first fetch, we duplicate the address
1253 then add the byte offset, do the fetch, and shift and mask as
1254 needed, yielding a fragment of the value, properly aligned for
1255 the final bitwise or:
1257 then we swap, and repeat the process:
1258 frag1 addr --- address on top
1259 frag1 addr addr --- duplicate it
1260 frag1 addr frag2 --- get second fragment
1261 frag1 frag2 addr --- swap again
1262 frag1 frag2 frag3 --- get third fragment
1263 Notice that, since the third fragment is the last one, we don't
1264 bother duplicating the address this time. Now we have all the
1265 fragments on the stack, and we can simply `or' them together,
1266 yielding the final value of the bitfield. */
1268 /* The first and one-after-last bits in the field, but rounded down
1269 and up to byte boundaries. */
1270 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1271 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1275 /* current bit offset within the structure */
1278 /* The index in ops of the opcode we're considering. */
1281 /* The number of fragments we generated in the process. Probably
1282 equal to the number of `one' bits in bytesize, but who cares? */
1285 /* Dereference any typedefs. */
1286 type = check_typedef (type);
1288 /* Can we fetch the number of bits requested at all? */
1289 if ((end - start) > ((1 << num_ops) * 8))
1290 internal_error ("ax-gdb.c (gen_bitfield_ref): bitfield too wide");
1292 /* Note that we know here that we only need to try each opcode once.
1293 That may not be true on machines with weird byte sizes. */
1294 offset = bound_start;
1296 for (op = num_ops - 1; op >= 0; op--)
1298 /* number of bits that ops[op] would fetch */
1299 int op_size = 8 << op;
1301 /* The stack at this point, from bottom to top, contains zero or
1302 more fragments, then the address. */
1304 /* Does this fetch fit within the bitfield? */
1305 if (offset + op_size <= bound_end)
1307 /* Is this the last fragment? */
1308 int last_frag = (offset + op_size == bound_end);
1311 ax_simple (ax, aop_dup); /* keep a copy of the address */
1313 /* Add the offset. */
1314 gen_offset (ax, offset / TARGET_CHAR_BIT);
1318 /* Record the area of memory we're about to fetch. */
1319 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1322 /* Perform the fetch. */
1323 ax_simple (ax, ops[op]);
1325 /* Shift the bits we have to their proper position.
1326 gen_left_shift will generate right shifts when the operand
1329 A big-endian field diagram to ponder:
1330 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1331 +------++------++------++------++------++------++------++------+
1332 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1334 bit number 16 32 48 53
1335 These are bit numbers as supplied by GDB. Note that the
1336 bit numbers run from right to left once you've fetched the
1339 A little-endian field diagram to ponder:
1340 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1341 +------++------++------++------++------++------++------++------+
1342 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1344 bit number 48 32 16 4 0
1346 In both cases, the most significant end is on the left
1347 (i.e. normal numeric writing order), which means that you
1348 don't go crazy thinking about `left' and `right' shifts.
1350 We don't have to worry about masking yet:
1351 - If they contain garbage off the least significant end, then we
1352 must be looking at the low end of the field, and the right
1353 shift will wipe them out.
1354 - If they contain garbage off the most significant end, then we
1355 must be looking at the most significant end of the word, and
1356 the sign/zero extension will wipe them out.
1357 - If we're in the interior of the word, then there is no garbage
1358 on either end, because the ref operators zero-extend. */
1359 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1360 gen_left_shift (ax, end - (offset + op_size));
1362 gen_left_shift (ax, offset - start);
1365 /* Bring the copy of the address up to the top. */
1366 ax_simple (ax, aop_swap);
1373 /* Generate enough bitwise `or' operations to combine all the
1374 fragments we left on the stack. */
1375 while (fragment_count-- > 1)
1376 ax_simple (ax, aop_bit_or);
1378 /* Sign- or zero-extend the value as appropriate. */
1379 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1381 /* This is *not* an lvalue. Ugh. */
1382 value->kind = axs_rvalue;
1387 /* Generate code to reference the member named FIELD of a structure or
1388 union. The top of the stack, as described by VALUE, should have
1389 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1390 the operator being compiled, and OPERAND_NAME is the kind of thing
1391 it operates on; we use them in error messages. */
1393 gen_struct_ref (ax, value, field, operator_name, operand_name)
1394 struct agent_expr *ax;
1395 struct axs_value *value;
1397 char *operator_name;
1403 /* Follow pointers until we reach a non-pointer. These aren't the C
1404 semantics, but they're what the normal GDB evaluator does, so we
1405 should at least be consistent. */
1406 while (value->type->code == TYPE_CODE_PTR)
1408 gen_usual_unary (ax, value);
1409 gen_deref (ax, value);
1413 /* This must yield a structure or a union. */
1414 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1415 && TYPE_CODE (type) != TYPE_CODE_UNION)
1416 error ("The left operand of `%s' is not a %s.",
1417 operator_name, operand_name);
1419 /* And it must be in memory; we don't deal with structure rvalues,
1420 or structures living in registers. */
1421 if (value->kind != axs_lvalue_memory)
1422 error ("Structure does not live in memory.");
1424 i = find_field (type, field);
1426 /* Is this a bitfield? */
1427 if (TYPE_FIELD_PACKED (type, i))
1428 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1429 TYPE_FIELD_BITPOS (type, i),
1430 (TYPE_FIELD_BITPOS (type, i)
1431 + TYPE_FIELD_BITSIZE (type, i)));
1434 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1435 value->kind = axs_lvalue_memory;
1436 value->type = TYPE_FIELD_TYPE (type, i);
1441 /* Generate code for GDB's magical `repeat' operator.
1442 LVALUE @ INT creates an array INT elements long, and whose elements
1443 have the same type as LVALUE, located in memory so that LVALUE is
1444 its first element. For example, argv[0]@argc gives you the array
1445 of command-line arguments.
1447 Unfortunately, because we have to know the types before we actually
1448 have a value for the expression, we can't implement this perfectly
1449 without changing the type system, having values that occupy two
1450 stack slots, doing weird things with sizeof, etc. So we require
1451 the right operand to be a constant expression. */
1453 gen_repeat (pc, ax, value)
1454 union exp_element **pc;
1455 struct agent_expr *ax;
1456 struct axs_value *value;
1458 struct axs_value value1;
1459 /* We don't want to turn this into an rvalue, so no conversions
1461 gen_expr (pc, ax, &value1);
1462 if (value1.kind != axs_lvalue_memory)
1463 error ("Left operand of `@' must be an object in memory.");
1465 /* Evaluate the length; it had better be a constant. */
1467 struct value *v = const_expr (pc);
1471 error ("Right operand of `@' must be a constant, in agent expressions.");
1472 if (v->type->code != TYPE_CODE_INT)
1473 error ("Right operand of `@' must be an integer.");
1474 length = value_as_long (v);
1476 error ("Right operand of `@' must be positive.");
1478 /* The top of the stack is already the address of the object, so
1479 all we need to do is frob the type of the lvalue. */
1481 /* FIXME-type-allocation: need a way to free this type when we are
1484 = create_range_type (0, builtin_type_int, 0, length - 1);
1485 struct type *array = create_array_type (0, value1.type, range);
1487 value->kind = axs_lvalue_memory;
1488 value->type = array;
1494 /* Emit code for the `sizeof' operator.
1495 *PC should point at the start of the operand expression; we advance it
1496 to the first instruction after the operand. */
1498 gen_sizeof (pc, ax, value)
1499 union exp_element **pc;
1500 struct agent_expr *ax;
1501 struct axs_value *value;
1503 /* We don't care about the value of the operand expression; we only
1504 care about its type. However, in the current arrangement, the
1505 only way to find an expression's type is to generate code for it.
1506 So we generate code for the operand, and then throw it away,
1507 replacing it with code that simply pushes its size. */
1508 int start = ax->len;
1509 gen_expr (pc, ax, value);
1511 /* Throw away the code we just generated. */
1514 ax_const_l (ax, TYPE_LENGTH (value->type));
1515 value->kind = axs_rvalue;
1516 value->type = builtin_type_int;
1520 /* Generating bytecode from GDB expressions: general recursive thingy */
1522 /* A gen_expr function written by a Gen-X'er guy.
1523 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1525 gen_expr (pc, ax, value)
1526 union exp_element **pc;
1527 struct agent_expr *ax;
1528 struct axs_value *value;
1530 /* Used to hold the descriptions of operand expressions. */
1531 struct axs_value value1, value2;
1532 enum exp_opcode op = (*pc)[0].opcode;
1534 /* If we're looking at a constant expression, just push its value. */
1536 struct value *v = maybe_const_expr (pc);
1540 ax_const_l (ax, value_as_long (v));
1541 value->kind = axs_rvalue;
1542 value->type = check_typedef (VALUE_TYPE (v));
1547 /* Otherwise, go ahead and generate code for it. */
1550 /* Binary arithmetic operators. */
1556 case BINOP_SUBSCRIPT:
1557 case BINOP_BITWISE_AND:
1558 case BINOP_BITWISE_IOR:
1559 case BINOP_BITWISE_XOR:
1561 gen_expr (pc, ax, &value1);
1562 gen_usual_unary (ax, &value1);
1563 gen_expr (pc, ax, &value2);
1564 gen_usual_unary (ax, &value2);
1565 gen_usual_arithmetic (ax, &value1, &value2);
1569 gen_add (ax, value, &value1, &value2, "addition");
1572 gen_sub (ax, value, &value1, &value2);
1575 gen_binop (ax, value, &value1, &value2,
1576 aop_mul, aop_mul, 1, "multiplication");
1579 gen_binop (ax, value, &value1, &value2,
1580 aop_div_signed, aop_div_unsigned, 1, "division");
1583 gen_binop (ax, value, &value1, &value2,
1584 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1586 case BINOP_SUBSCRIPT:
1587 gen_add (ax, value, &value1, &value2, "array subscripting");
1588 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1589 error ("Illegal combination of types in array subscripting.");
1590 gen_deref (ax, value);
1592 case BINOP_BITWISE_AND:
1593 gen_binop (ax, value, &value1, &value2,
1594 aop_bit_and, aop_bit_and, 0, "bitwise and");
1597 case BINOP_BITWISE_IOR:
1598 gen_binop (ax, value, &value1, &value2,
1599 aop_bit_or, aop_bit_or, 0, "bitwise or");
1602 case BINOP_BITWISE_XOR:
1603 gen_binop (ax, value, &value1, &value2,
1604 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1608 /* We should only list operators in the outer case statement
1609 that we actually handle in the inner case statement. */
1610 internal_error ("ax-gdb.c (gen_expr): op case sets don't match");
1614 /* Note that we need to be a little subtle about generating code
1615 for comma. In C, we can do some optimizations here because
1616 we know the left operand is only being evaluated for effect.
1617 However, if the tracing kludge is in effect, then we always
1618 need to evaluate the left hand side fully, so that all the
1619 variables it mentions get traced. */
1622 gen_expr (pc, ax, &value1);
1623 /* Don't just dispose of the left operand. We might be tracing,
1624 in which case we want to emit code to trace it if it's an
1626 gen_traced_pop (ax, &value1);
1627 gen_expr (pc, ax, value);
1628 /* It's the consumer's responsibility to trace the right operand. */
1631 case OP_LONG: /* some integer constant */
1633 struct type *type = (*pc)[1].type;
1634 LONGEST k = (*pc)[2].longconst;
1636 gen_int_literal (ax, value, k, type);
1641 gen_var_ref (ax, value, (*pc)[2].symbol);
1647 int reg = (int) (*pc)[1].longconst;
1649 value->kind = axs_lvalue_register;
1651 value->type = REGISTER_VIRTUAL_TYPE (reg);
1655 case OP_INTERNALVAR:
1656 error ("GDB agent expressions cannot use convenience variables.");
1658 /* Weirdo operator: see comments for gen_repeat for details. */
1660 /* Note that gen_repeat handles its own argument evaluation. */
1662 gen_repeat (pc, ax, value);
1667 struct type *type = (*pc)[1].type;
1669 gen_expr (pc, ax, value);
1670 gen_cast (ax, value, type);
1676 struct type *type = check_typedef ((*pc)[1].type);
1678 gen_expr (pc, ax, value);
1679 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1680 it's just a hack for dealing with minsyms; you take some
1681 integer constant, pretend it's the address of an lvalue of
1682 the given type, and dereference it. */
1683 if (value->kind != axs_rvalue)
1684 /* This would be weird. */
1685 internal_error ("ax-gdb.c (gen_expr): OP_MEMVAL operand isn't an rvalue???");
1687 value->kind = axs_lvalue_memory;
1693 /* -FOO is equivalent to 0 - FOO. */
1694 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
1695 gen_usual_unary (ax, &value1); /* shouldn't do much */
1696 gen_expr (pc, ax, &value2);
1697 gen_usual_unary (ax, &value2);
1698 gen_usual_arithmetic (ax, &value1, &value2);
1699 gen_sub (ax, value, &value1, &value2);
1702 case UNOP_LOGICAL_NOT:
1704 gen_expr (pc, ax, value);
1705 gen_logical_not (ax, value);
1708 case UNOP_COMPLEMENT:
1710 gen_expr (pc, ax, value);
1711 gen_complement (ax, value);
1716 gen_expr (pc, ax, value);
1717 gen_usual_unary (ax, value);
1718 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1719 error ("Argument of unary `*' is not a pointer.");
1720 gen_deref (ax, value);
1725 gen_expr (pc, ax, value);
1726 gen_address_of (ax, value);
1731 /* Notice that gen_sizeof handles its own operand, unlike most
1732 of the other unary operator functions. This is because we
1733 have to throw away the code we generate. */
1734 gen_sizeof (pc, ax, value);
1737 case STRUCTOP_STRUCT:
1740 int length = (*pc)[1].longconst;
1741 char *name = &(*pc)[2].string;
1743 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1744 gen_expr (pc, ax, value);
1745 if (op == STRUCTOP_STRUCT)
1746 gen_struct_ref (ax, value, name, ".", "structure or union");
1747 else if (op == STRUCTOP_PTR)
1748 gen_struct_ref (ax, value, name, "->",
1749 "pointer to a structure or union");
1751 /* If this `if' chain doesn't handle it, then the case list
1752 shouldn't mention it, and we shouldn't be here. */
1753 internal_error ("ax-gdb.c (gen_expr): unhandled struct case");
1758 error ("Attempt to use a type name as an expression.");
1761 error ("Unsupported operator in expression.");
1767 /* Generating bytecode from GDB expressions: driver */
1769 /* Given a GDB expression EXPR, produce a string of agent bytecode
1770 which computes its value. Return the agent expression, and set
1771 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1773 expr_to_agent (expr, value)
1774 struct expression *expr;
1775 struct axs_value *value;
1777 struct cleanup *old_chain = 0;
1778 struct agent_expr *ax = new_agent_expr (0);
1779 union exp_element *pc;
1781 old_chain = make_cleanup_free_agent_expr (ax);
1785 gen_expr (&pc, ax, value);
1787 /* We have successfully built the agent expr, so cancel the cleanup
1788 request. If we add more cleanups that we always want done, this
1789 will have to get more complicated. */
1790 discard_cleanups (old_chain);
1795 #if 0 /* not used */
1796 /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1797 string of agent bytecode which will leave its address and size on
1798 the top of stack. Return the agent expression.
1800 Not sure this function is useful at all. */
1802 expr_to_address_and_size (expr)
1803 struct expression *expr;
1805 struct axs_value value;
1806 struct agent_expr *ax = expr_to_agent (expr, &value);
1808 /* Complain if the result is not a memory lvalue. */
1809 if (value.kind != axs_lvalue_memory)
1811 free_agent_expr (ax);
1812 error ("Expression does not denote an object in memory.");
1815 /* Push the object's size on the stack. */
1816 ax_const_l (ax, TYPE_LENGTH (value.type));
1822 /* Given a GDB expression EXPR, return bytecode to trace its value.
1823 The result will use the `trace' and `trace_quick' bytecodes to
1824 record the value of all memory touched by the expression. The
1825 caller can then use the ax_reqs function to discover which
1826 registers it relies upon. */
1828 gen_trace_for_expr (scope, expr)
1830 struct expression *expr;
1832 struct cleanup *old_chain = 0;
1833 struct agent_expr *ax = new_agent_expr (scope);
1834 union exp_element *pc;
1835 struct axs_value value;
1837 old_chain = make_cleanup_free_agent_expr (ax);
1841 gen_expr (&pc, ax, &value);
1843 /* Make sure we record the final object, and get rid of it. */
1844 gen_traced_pop (ax, &value);
1846 /* Oh, and terminate. */
1847 ax_simple (ax, aop_end);
1849 /* We have successfully built the agent expr, so cancel the cleanup
1850 request. If we add more cleanups that we always want done, this
1851 will have to get more complicated. */
1852 discard_cleanups (old_chain);
1858 /* The "agent" command, for testing: compile and disassemble an expression. */
1861 print_axs_value (f, value)
1863 struct axs_value *value;
1865 switch (value->kind)
1868 fputs_filtered ("rvalue", f);
1871 case axs_lvalue_memory:
1872 fputs_filtered ("memory lvalue", f);
1875 case axs_lvalue_register:
1876 fprintf_filtered (f, "register %d lvalue", value->u.reg);
1880 fputs_filtered (" : ", f);
1881 type_print (value->type, "", f, -1);
1886 agent_command (exp, from_tty)
1890 struct cleanup *old_chain = 0;
1891 struct expression *expr;
1892 struct agent_expr *agent;
1893 struct frame_info *fi = get_current_frame (); /* need current scope */
1895 /* We don't deal with overlay debugging at the moment. We need to
1896 think more carefully about this. If you copy this code into
1897 another command, change the error message; the user shouldn't
1898 have to know anything about agent expressions. */
1899 if (overlay_debugging)
1900 error ("GDB can't do agent expression translation with overlays.");
1903 error_no_arg ("expression to translate");
1905 expr = parse_expression (exp);
1906 old_chain = make_cleanup (free_current_contents, &expr);
1907 agent = gen_trace_for_expr (fi->pc, expr);
1908 make_cleanup_free_agent_expr (agent);
1909 ax_print (gdb_stdout, agent);
1911 /* It would be nice to call ax_reqs here to gather some general info
1912 about the expression, and then print out the result. */
1914 do_cleanups (old_chain);
1919 /* Initialization code. */
1921 void _initialize_ax_gdb (void);
1923 _initialize_ax_gdb ()
1925 add_cmd ("agent", class_maintenance, agent_command,
1926 "Translate an expression into remote agent bytecode.",