1 /* GDB-specific functions for operating on agent expressions
2 Copyright 1998 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, Boston, MA 02111-1307, USA. */
20 /* $Id: ax-gdb.c,v 1.8 1998/12/03 05:34:24 cagney Exp $ */
27 #include "expression.h"
35 /* Probably the best way to read this file is to start with the types
36 and enums in ax-gdb.h, and then look at gen_expr, towards the
37 bottom; that's the main function that looks at the GDB expressions
38 and calls everything else to generate code.
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 PARAMS ((struct symbol *var));
60 static struct value *const_expr PARAMS ((union exp_element **pc));
61 static struct value *maybe_const_expr PARAMS ((union exp_element **pc));
63 static void gen_traced_pop PARAMS ((struct agent_expr *, struct axs_value *));
65 static void gen_sign_extend PARAMS ((struct agent_expr *, struct type *));
66 static void gen_extend PARAMS ((struct agent_expr *, struct type *));
67 static void gen_fetch PARAMS ((struct agent_expr *, struct type *));
68 static void gen_left_shift PARAMS ((struct agent_expr *, int));
71 static void gen_frame_args_address PARAMS ((struct agent_expr *));
72 static void gen_frame_locals_address PARAMS ((struct agent_expr *));
73 static void gen_offset PARAMS ((struct agent_expr *ax, int offset));
74 static void gen_sym_offset PARAMS ((struct agent_expr *, struct symbol *));
75 static void gen_var_ref PARAMS ((struct agent_expr *ax,
76 struct axs_value *value,
80 static void gen_int_literal PARAMS ((struct agent_expr *ax,
81 struct axs_value *value,
82 LONGEST k, struct type *type));
85 static void require_rvalue PARAMS ((struct agent_expr *ax,
86 struct axs_value *value));
87 static void gen_usual_unary PARAMS ((struct agent_expr *ax,
88 struct axs_value *value));
89 static int type_wider_than PARAMS ((struct type *type1,
91 static struct type *max_type PARAMS ((struct type *type1,
93 static void gen_conversion PARAMS ((struct agent_expr *ax,
96 static int is_nontrivial_conversion PARAMS ((struct type *from,
98 static void gen_usual_arithmetic PARAMS ((struct agent_expr *ax,
99 struct axs_value *value1,
100 struct axs_value *value2));
101 static void gen_integral_promotions PARAMS ((struct agent_expr *ax,
102 struct axs_value *value));
103 static void gen_cast PARAMS ((struct agent_expr *ax,
104 struct axs_value *value,
106 static void gen_scale PARAMS ((struct agent_expr *ax,
109 static void gen_add PARAMS ((struct agent_expr *ax,
110 struct axs_value *value,
111 struct axs_value *value1,
112 struct axs_value *value2,
114 static void gen_sub PARAMS ((struct agent_expr *ax,
115 struct axs_value *value,
116 struct axs_value *value1,
117 struct axs_value *value2));
118 static void gen_binop PARAMS ((struct agent_expr *ax,
119 struct axs_value *value,
120 struct axs_value *value1,
121 struct axs_value *value2,
123 enum agent_op op_unsigned,
126 static void gen_logical_not PARAMS ((struct agent_expr *ax,
127 struct axs_value *value));
128 static void gen_complement PARAMS ((struct agent_expr *ax,
129 struct axs_value *value));
130 static void gen_deref PARAMS ((struct agent_expr *, struct axs_value *));
131 static void gen_address_of PARAMS ((struct agent_expr *, struct axs_value *));
132 static int find_field PARAMS ((struct type *type, char *name));
133 static void gen_bitfield_ref PARAMS ((struct agent_expr *ax,
134 struct axs_value *value,
136 int start, int end));
137 static void gen_struct_ref PARAMS ((struct agent_expr *ax,
138 struct axs_value *value,
141 char *operand_name));
142 static void gen_repeat PARAMS ((union exp_element **pc,
143 struct agent_expr *ax,
144 struct axs_value *value));
145 static void gen_sizeof PARAMS ((union exp_element **pc,
146 struct agent_expr *ax,
147 struct axs_value *value));
148 static void gen_expr PARAMS ((union exp_element **pc,
149 struct agent_expr *ax,
150 struct axs_value *value));
152 static void print_axs_value PARAMS ((GDB_FILE *f, struct axs_value *value));
153 static void agent_command PARAMS ((char *exp, int from_tty));
156 /* Detecting constant expressions. */
158 /* If the variable reference at *PC is a constant, return its value.
159 Otherwise, return zero.
161 Hey, Wally! How can a variable reference be a constant?
163 Well, Beav, this function really handles the OP_VAR_VALUE operator,
164 not specifically variable references. GDB uses OP_VAR_VALUE to
165 refer to any kind of symbolic reference: function names, enum
166 elements, and goto labels are all handled through the OP_VAR_VALUE
167 operator, even though they're constants. It makes sense given the
170 Gee, Wally, don'cha wonder sometimes if data representations that
171 subvert commonly accepted definitions of terms in favor of heavily
172 context-specific interpretations are really just a tool of the
173 programming hegemony to preserve their power and exclude the
176 static struct value *
180 struct type *type = SYMBOL_TYPE (var);
182 switch (SYMBOL_CLASS (var))
185 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
188 return value_from_longest (type, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
196 /* If the expression starting at *PC has a constant value, return it.
197 Otherwise, return zero. If we return a value, then *PC will be
198 advanced to the end of it. If we return zero, *PC could be
200 static struct value *
202 union exp_element **pc;
204 enum exp_opcode op = (*pc)->opcode;
211 struct type *type = (*pc)[1].type;
212 LONGEST k = (*pc)[2].longconst;
214 return value_from_longest (type, k);
219 struct value *v = const_var_ref ((*pc)[2].symbol);
224 /* We could add more operators in here. */
228 v1 = const_expr (pc);
230 return value_neg (v1);
240 /* Like const_expr, but guarantee also that *PC is undisturbed if the
241 expression is not constant. */
242 static struct value *
243 maybe_const_expr (pc)
244 union exp_element **pc;
246 union exp_element *tentative_pc = *pc;
247 struct value *v = const_expr (&tentative_pc);
249 /* If we got a value, then update the real PC. */
257 /* Generating bytecode from GDB expressions: general assumptions */
259 /* Here are a few general assumptions made throughout the code; if you
260 want to make a change that contradicts one of these, then you'd
261 better scan things pretty thoroughly.
263 - We assume that all values occupy one stack element. For example,
264 sometimes we'll swap to get at the left argument to a binary
265 operator. If we decide that void values should occupy no stack
266 elements, or that synthetic arrays (whose size is determined at
267 run time, created by the `@' operator) should occupy two stack
268 elements (address and length), then this will cause trouble.
270 - We assume the stack elements are infinitely wide, and that we
271 don't have to worry what happens if the user requests an
272 operation that is wider than the actual interpreter's stack.
273 That is, it's up to the interpreter to handle directly all the
274 integer widths the user has access to. (Woe betide the language
277 - We don't support side effects. Thus, we don't have to worry about
278 GCC's generalized lvalues, function calls, etc.
280 - We don't support floating point. Many places where we switch on
281 some type don't bother to include cases for floating point; there
282 may be even more subtle ways this assumption exists. For
283 example, the arguments to % must be integers.
285 - We assume all subexpressions have a static, unchanging type. If
286 we tried to support convenience variables, this would be a
289 - All values on the stack should always be fully zero- or
292 (I wasn't sure whether to choose this or its opposite --- that
293 only addresses are assumed extended --- but it turns out that
294 neither convention completely eliminates spurious extend
295 operations (if everything is always extended, then you have to
296 extend after add, because it could overflow; if nothing is
297 extended, then you end up producing extends whenever you change
298 sizes), and this is simpler.) */
301 /* Generating bytecode from GDB expressions: the `trace' kludge */
303 /* The compiler in this file is a general-purpose mechanism for
304 translating GDB expressions into bytecode. One ought to be able to
305 find a million and one uses for it.
307 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
308 of expediency. Let he who is without sin cast the first stone.
310 For the data tracing facility, we need to insert `trace' bytecodes
311 before each data fetch; this records all the memory that the
312 expression touches in the course of evaluation, so that memory will
313 be available when the user later tries to evaluate the expression
316 This should be done (I think) in a post-processing pass, that walks
317 an arbitrary agent expression and inserts `trace' operations at the
318 appropriate points. But it's much faster to just hack them
319 directly into the code. And since we're in a crunch, that's what
322 Setting the flag trace_kludge to non-zero enables the code that
323 emits the trace bytecodes at the appropriate points. */
324 static int trace_kludge;
326 /* Trace the lvalue on the stack, if it needs it. In either case, pop
327 the value. Useful on the left side of a comma, and at the end of
328 an expression being used for tracing. */
330 gen_traced_pop (ax, value)
331 struct agent_expr *ax;
332 struct axs_value *value;
338 /* We don't trace rvalues, just the lvalues necessary to
339 produce them. So just dispose of this value. */
340 ax_simple (ax, aop_pop);
343 case axs_lvalue_memory:
345 int length = TYPE_LENGTH (value->type);
347 /* There's no point in trying to use a trace_quick bytecode
348 here, since "trace_quick SIZE pop" is three bytes, whereas
349 "const8 SIZE trace" is also three bytes, does the same
350 thing, and the simplest code which generates that will also
351 work correctly for objects with large sizes. */
352 ax_const_l (ax, length);
353 ax_simple (ax, aop_trace);
357 case axs_lvalue_register:
358 /* We need to mention the register somewhere in the bytecode,
359 so ax_reqs will pick it up and add it to the mask of
361 ax_reg (ax, value->u.reg);
362 ax_simple (ax, aop_pop);
366 /* If we're not tracing, just pop the value. */
367 ax_simple (ax, aop_pop);
372 /* Generating bytecode from GDB expressions: helper functions */
374 /* Assume that the lower bits of the top of the stack is a value of
375 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
377 gen_sign_extend (ax, type)
378 struct agent_expr *ax;
381 /* Do we need to sign-extend this? */
382 if (! TYPE_UNSIGNED (type))
383 ax_ext (ax, type->length * TARGET_CHAR_BIT);
387 /* Assume the lower bits of the top of the stack hold a value of type
388 TYPE, and the upper bits are garbage. Sign-extend or truncate as
391 gen_extend (ax, type)
392 struct agent_expr *ax;
395 int bits = type->length * TARGET_CHAR_BIT;
397 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
401 /* Assume that the top of the stack contains a value of type "pointer
402 to TYPE"; generate code to fetch its value. Note that TYPE is the
403 target type, not the pointer type. */
406 struct agent_expr *ax;
411 /* Record the area of memory we're about to fetch. */
412 ax_trace_quick (ax, TYPE_LENGTH (type));
421 /* It's a scalar value, so we know how to dereference it. How
422 many bytes long is it? */
423 switch (type->length)
425 case 8 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref8 ); break;
426 case 16 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref16); break;
427 case 32 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref32); break;
428 case 64 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref64); break;
430 /* Either our caller shouldn't have asked us to dereference
431 that pointer (other code's fault), or we're not
432 implementing something we should be (this code's fault).
433 In any case, it's a bug the user shouldn't see. */
435 error ("GDB bug: ax-gdb.c (gen_fetch): strange size");
438 gen_sign_extend (ax, type);
442 /* Either our caller shouldn't have asked us to dereference that
443 pointer (other code's fault), or we're not implementing
444 something we should be (this code's fault). In any case,
445 it's a bug the user shouldn't see. */
446 error ("GDB bug: ax-gdb.c (gen_fetch): bad type code");
451 /* Generate code to left shift the top of the stack by DISTANCE bits, or
452 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
453 unsigned (logical) right shifts. */
455 gen_left_shift (ax, distance)
456 struct agent_expr *ax;
461 ax_const_l (ax, distance);
462 ax_simple (ax, aop_lsh);
464 else if (distance < 0)
466 ax_const_l (ax, -distance);
467 ax_simple (ax, aop_rsh_unsigned);
473 /* Generating bytecode from GDB expressions: symbol references */
475 /* Generate code to push the base address of the argument portion of
476 the top stack frame. */
478 gen_frame_args_address (ax)
479 struct agent_expr *ax;
481 long frame_reg, frame_offset;
483 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
484 ax_reg (ax, frame_reg);
485 gen_offset (ax, frame_offset);
489 /* Generate code to push the base address of the locals portion of the
492 gen_frame_locals_address (ax)
493 struct agent_expr *ax;
495 long frame_reg, frame_offset;
497 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
498 ax_reg (ax, frame_reg);
499 gen_offset (ax, frame_offset);
503 /* Generate code to add OFFSET to the top of the stack. Try to
504 generate short and readable code. We use this for getting to
505 variables on the stack, and structure members. If we were
506 programming in ML, it would be clearer why these are the same
509 gen_offset (ax, offset)
510 struct agent_expr *ax;
513 /* It would suffice to simply push the offset and add it, but this
514 makes it easier to read positive and negative offsets in the
518 ax_const_l (ax, offset);
519 ax_simple (ax, aop_add);
523 ax_const_l (ax, -offset);
524 ax_simple (ax, aop_sub);
529 /* In many cases, a symbol's value is the offset from some other
530 address (stack frame, base register, etc.) Generate code to add
531 VAR's value to the top of the stack. */
533 gen_sym_offset (ax, var)
534 struct agent_expr *ax;
537 gen_offset (ax, SYMBOL_VALUE (var));
541 /* Generate code for a variable reference to AX. The variable is the
542 symbol VAR. Set VALUE to describe the result. */
545 gen_var_ref (ax, value, var)
546 struct agent_expr *ax;
547 struct axs_value *value;
550 /* Dereference any typedefs. */
551 value->type = check_typedef (SYMBOL_TYPE (var));
553 /* I'm imitating the code in read_var_value. */
554 switch (SYMBOL_CLASS (var))
556 case LOC_CONST: /* A constant, like an enum value. */
557 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
558 value->kind = axs_rvalue;
561 case LOC_LABEL: /* A goto label, being used as a value. */
562 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
563 value->kind = axs_rvalue;
566 case LOC_CONST_BYTES:
567 error ("GDB bug: ax-gdb.c (gen_var_ref): LOC_CONST_BYTES symbols are not supported");
569 /* Variable at a fixed location in memory. Easy. */
571 /* Push the address of the variable. */
572 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
573 value->kind = axs_lvalue_memory;
576 case LOC_ARG: /* var lives in argument area of frame */
577 gen_frame_args_address (ax);
578 gen_sym_offset (ax, var);
579 value->kind = axs_lvalue_memory;
582 case LOC_REF_ARG: /* As above, but the frame slot really
583 holds the address of the variable. */
584 gen_frame_args_address (ax);
585 gen_sym_offset (ax, var);
586 /* Don't assume any particular pointer size. */
587 gen_fetch (ax, lookup_pointer_type (builtin_type_void));
588 value->kind = axs_lvalue_memory;
591 case LOC_LOCAL: /* var lives in locals area of frame */
593 gen_frame_locals_address (ax);
594 gen_sym_offset (ax, var);
595 value->kind = axs_lvalue_memory;
598 case LOC_BASEREG: /* relative to some base register */
599 case LOC_BASEREG_ARG:
600 ax_reg (ax, SYMBOL_BASEREG (var));
601 gen_sym_offset (ax, var);
602 value->kind = axs_lvalue_memory;
606 error ("Cannot compute value of typedef `%s'.",
607 SYMBOL_SOURCE_NAME (var));
611 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
612 value->kind = axs_rvalue;
617 /* Don't generate any code at all; in the process of treating
618 this as an lvalue or rvalue, the caller will generate the
620 value->kind = axs_lvalue_register;
621 value->u.reg = SYMBOL_VALUE (var);
624 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
625 register, not on the stack. Simpler than LOC_REGISTER and
626 LOC_REGPARM, because it's just like any other case where the
627 thing has a real address. */
628 case LOC_REGPARM_ADDR:
629 ax_reg (ax, SYMBOL_VALUE (var));
630 value->kind = axs_lvalue_memory;
635 struct minimal_symbol *msym
636 = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
638 error ("Couldn't resolve symbol `%s'.", SYMBOL_SOURCE_NAME (var));
640 /* Push the address of the variable. */
641 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
642 value->kind = axs_lvalue_memory;
646 case LOC_OPTIMIZED_OUT:
647 error ("The variable `%s' has been optimized out.",
648 SYMBOL_SOURCE_NAME (var));
652 error ("Cannot find value of botched symbol `%s'.",
653 SYMBOL_SOURCE_NAME (var));
660 /* Generating bytecode from GDB expressions: literals */
663 gen_int_literal (ax, value, k, type)
664 struct agent_expr *ax;
665 struct axs_value *value;
670 value->kind = axs_rvalue;
676 /* Generating bytecode from GDB expressions: unary conversions, casts */
678 /* Take what's on the top of the stack (as described by VALUE), and
679 try to make an rvalue out of it. Signal an error if we can't do
682 require_rvalue (ax, value)
683 struct agent_expr *ax;
684 struct axs_value *value;
689 /* It's already an rvalue. */
692 case axs_lvalue_memory:
693 /* The top of stack is the address of the object. Dereference. */
694 gen_fetch (ax, value->type);
697 case axs_lvalue_register:
698 /* There's nothing on the stack, but value->u.reg is the
699 register number containing the value.
701 When we add floating-point support, this is going to have to
702 change. What about SPARC register pairs, for example? */
703 ax_reg (ax, value->u.reg);
704 gen_extend (ax, value->type);
708 value->kind = axs_rvalue;
712 /* Assume the top of the stack is described by VALUE, and perform the
713 usual unary conversions. This is motivated by ANSI 6.2.2, but of
714 course GDB expressions are not ANSI; they're the mishmash union of
715 a bunch of languages. Rah.
717 NOTE! This function promises to produce an rvalue only when the
718 incoming value is of an appropriate type. In other words, the
719 consumer of the value this function produces may assume the value
720 is an rvalue only after checking its type.
722 The immediate issue is that if the user tries to use a structure or
723 union as an operand of, say, the `+' operator, we don't want to try
724 to convert that structure to an rvalue; require_rvalue will bomb on
725 structs and unions. Rather, we want to simply pass the struct
726 lvalue through unchanged, and let `+' raise an error. */
729 gen_usual_unary (ax, value)
730 struct agent_expr *ax;
731 struct axs_value *value;
733 /* We don't have to generate any code for the usual integral
734 conversions, since values are always represented as full-width on
735 the stack. Should we tweak the type? */
737 /* Some types require special handling. */
738 switch (value->type->code)
740 /* Functions get converted to a pointer to the function. */
742 value->type = lookup_pointer_type (value->type);
743 value->kind = axs_rvalue; /* Should always be true, but just in case. */
746 /* Arrays get converted to a pointer to their first element, and
747 are no longer an lvalue. */
748 case TYPE_CODE_ARRAY:
750 struct type *elements = TYPE_TARGET_TYPE (value->type);
751 value->type = lookup_pointer_type (elements);
752 value->kind = axs_rvalue;
753 /* We don't need to generate any code; the address of the array
754 is also the address of its first element. */
758 /* Don't try to convert structures and unions to rvalues. Let the
759 consumer signal an error. */
760 case TYPE_CODE_STRUCT:
761 case TYPE_CODE_UNION:
764 /* If the value is an enum, call it an integer. */
766 value->type = builtin_type_int;
770 /* If the value is an lvalue, dereference it. */
771 require_rvalue (ax, value);
775 /* Return non-zero iff the type TYPE1 is considered "wider" than the
776 type TYPE2, according to the rules described in gen_usual_arithmetic. */
778 type_wider_than (type1, type2)
779 struct type *type1, *type2;
781 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
782 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
783 && TYPE_UNSIGNED (type1)
784 && ! TYPE_UNSIGNED (type2)));
788 /* Return the "wider" of the two types TYPE1 and TYPE2. */
790 max_type (type1, type2)
791 struct type *type1, *type2;
793 return type_wider_than (type1, type2) ? type1 : type2;
797 /* Generate code to convert a scalar value of type FROM to type TO. */
799 gen_conversion (ax, from, to)
800 struct agent_expr *ax;
801 struct type *from, *to;
803 /* Perhaps there is a more graceful way to state these rules. */
805 /* If we're converting to a narrower type, then we need to clear out
807 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
808 gen_extend (ax, from);
810 /* If the two values have equal width, but different signednesses,
811 then we need to extend. */
812 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
814 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
818 /* If we're converting to a wider type, and becoming unsigned, then
819 we need to zero out any possible sign bits. */
820 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
822 if (TYPE_UNSIGNED (to))
828 /* Return non-zero iff the type FROM will require any bytecodes to be
829 emitted to be converted to the type TO. */
831 is_nontrivial_conversion (from, to)
832 struct type *from, *to;
834 struct agent_expr *ax = new_agent_expr (0);
837 /* Actually generate the code, and see if anything came out. At the
838 moment, it would be trivial to replicate the code in
839 gen_conversion here, but in the future, when we're supporting
840 floating point and the like, it may not be. Doing things this
841 way allows this function to be independent of the logic in
843 gen_conversion (ax, from, to);
844 nontrivial = ax->len > 0;
845 free_agent_expr (ax);
850 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
851 6.2.1.5) for the two operands of an arithmetic operator. This
852 effectively finds a "least upper bound" type for the two arguments,
853 and promotes each argument to that type. *VALUE1 and *VALUE2
854 describe the values as they are passed in, and as they are left. */
856 gen_usual_arithmetic (ax, value1, value2)
857 struct agent_expr *ax;
858 struct axs_value *value1, *value2;
860 /* Do the usual binary conversions. */
861 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
862 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
864 /* The ANSI integral promotions seem to work this way: Order the
865 integer types by size, and then by signedness: an n-bit
866 unsigned type is considered "wider" than an n-bit signed
867 type. Promote to the "wider" of the two types, and always
868 promote at least to int. */
869 struct type *target = max_type (builtin_type_int,
870 max_type (value1->type, value2->type));
872 /* Deal with value2, on the top of the stack. */
873 gen_conversion (ax, value2->type, target);
875 /* Deal with value1, not on the top of the stack. Don't
876 generate the `swap' instructions if we're not actually going
878 if (is_nontrivial_conversion (value1->type, target))
880 ax_simple (ax, aop_swap);
881 gen_conversion (ax, value1->type, target);
882 ax_simple (ax, aop_swap);
885 value1->type = value2->type = target;
890 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
891 the value on the top of the stack, as described by VALUE. Assume
892 the value has integral type. */
894 gen_integral_promotions (ax, value)
895 struct agent_expr *ax;
896 struct axs_value *value;
898 if (! type_wider_than (value->type, builtin_type_int))
900 gen_conversion (ax, value->type, builtin_type_int);
901 value->type = builtin_type_int;
903 else if (! type_wider_than (value->type, builtin_type_unsigned_int))
905 gen_conversion (ax, value->type, builtin_type_unsigned_int);
906 value->type = builtin_type_unsigned_int;
911 /* Generate code for a cast to TYPE. */
913 gen_cast (ax, value, type)
914 struct agent_expr *ax;
915 struct axs_value *value;
918 /* GCC does allow casts to yield lvalues, so this should be fixed
919 before merging these changes into the trunk. */
920 require_rvalue (ax, value);
921 /* Dereference typedefs. */
922 type = check_typedef (type);
927 /* It's implementation-defined, and I'll bet this is what GCC
931 case TYPE_CODE_ARRAY:
932 case TYPE_CODE_STRUCT:
933 case TYPE_CODE_UNION:
935 error ("Illegal type cast: intended type must be scalar.");
938 /* We don't have to worry about the size of the value, because
939 all our integral values are fully sign-extended, and when
940 casting pointers we can do anything we like. Is there any
941 way for us to actually know what GCC actually does with a
947 gen_conversion (ax, value->type, type);
951 /* We could pop the value, and rely on everyone else to check
952 the type and notice that this value doesn't occupy a stack
953 slot. But for now, leave the value on the stack, and
954 preserve the "value == stack element" assumption. */
958 error ("Casts to requested type are not yet implemented.");
966 /* Generating bytecode from GDB expressions: arithmetic */
968 /* Scale the integer on the top of the stack by the size of the target
969 of the pointer type TYPE. */
971 gen_scale (ax, op, type)
972 struct agent_expr *ax;
976 struct type *element = TYPE_TARGET_TYPE (type);
978 if (element->length != 1)
980 ax_const_l (ax, element->length);
986 /* Generate code for an addition; non-trivial because we deal with
987 pointer arithmetic. We set VALUE to describe the result value; we
988 assume VALUE1 and VALUE2 describe the two operands, and that
989 they've undergone the usual binary conversions. Used by both
990 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
992 gen_add (ax, value, value1, value2, name)
993 struct agent_expr *ax;
994 struct axs_value *value, *value1, *value2;
998 if (value1->type->code == TYPE_CODE_INT
999 && value2->type->code == TYPE_CODE_PTR)
1001 /* Swap the values and proceed normally. */
1002 ax_simple (ax, aop_swap);
1003 gen_scale (ax, aop_mul, value2->type);
1004 ax_simple (ax, aop_add);
1005 gen_extend (ax, value2->type); /* Catch overflow. */
1006 value->type = value2->type;
1009 /* Is it PTR+INT? */
1010 else if (value1->type->code == TYPE_CODE_PTR
1011 && value2->type->code == TYPE_CODE_INT)
1013 gen_scale (ax, aop_mul, value1->type);
1014 ax_simple (ax, aop_add);
1015 gen_extend (ax, value1->type); /* Catch overflow. */
1016 value->type = value1->type;
1019 /* Must be number + number; the usual binary conversions will have
1020 brought them both to the same width. */
1021 else if (value1->type->code == TYPE_CODE_INT
1022 && value2->type->code == TYPE_CODE_INT)
1024 ax_simple (ax, aop_add);
1025 gen_extend (ax, value1->type); /* Catch overflow. */
1026 value->type = value1->type;
1030 error ("Illegal combination of types in %s.", name);
1032 value->kind = axs_rvalue;
1036 /* Generate code for an addition; non-trivial because we have to deal
1037 with pointer arithmetic. We set VALUE to describe the result
1038 value; we assume VALUE1 and VALUE2 describe the two operands, and
1039 that they've undergone the usual binary conversions. */
1041 gen_sub (ax, value, value1, value2)
1042 struct agent_expr *ax;
1043 struct axs_value *value, *value1, *value2;
1045 struct type *element;
1047 if (value1->type->code == TYPE_CODE_PTR)
1049 /* Is it PTR - INT? */
1050 if (value2->type->code == TYPE_CODE_INT)
1052 gen_scale (ax, aop_mul, value1->type);
1053 ax_simple (ax, aop_sub);
1054 gen_extend (ax, value1->type); /* Catch overflow. */
1055 value->type = value1->type;
1058 /* Is it PTR - PTR? Strictly speaking, the types ought to
1059 match, but this is what the normal GDB expression evaluator
1061 else if (value2->type->code == TYPE_CODE_PTR
1062 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1063 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1065 ax_simple (ax, aop_sub);
1066 gen_scale (ax, aop_div_unsigned, value1->type);
1067 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
1071 First argument of `-' is a pointer, but second argument is neither\n\
1072 an integer nor a pointer of the same type.");
1075 /* Must be number + number. */
1076 else if (value1->type->code == TYPE_CODE_INT
1077 && value2->type->code == TYPE_CODE_INT)
1079 ax_simple (ax, aop_sub);
1080 gen_extend (ax, value1->type); /* Catch overflow. */
1081 value->type = value1->type;
1085 error ("Illegal combination of types in subtraction.");
1087 value->kind = axs_rvalue;
1090 /* Generate code for a binary operator that doesn't do pointer magic.
1091 We set VALUE to describe the result value; we assume VALUE1 and
1092 VALUE2 describe the two operands, and that they've undergone the
1093 usual binary conversions. MAY_CARRY should be non-zero iff the
1094 result needs to be extended. NAME is the English name of the
1095 operator, used in error messages */
1097 gen_binop (ax, value, value1, value2, op, op_unsigned, may_carry, name)
1098 struct agent_expr *ax;
1099 struct axs_value *value, *value1, *value2;
1100 enum agent_op op, op_unsigned;
1104 /* We only handle INT op INT. */
1105 if ((value1->type->code != TYPE_CODE_INT)
1106 || (value2->type->code != TYPE_CODE_INT))
1107 error ("Illegal combination of types in %s.", name);
1110 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1112 gen_extend (ax, value1->type); /* catch overflow */
1113 value->type = value1->type;
1114 value->kind = axs_rvalue;
1119 gen_logical_not (ax, value)
1120 struct agent_expr *ax;
1121 struct axs_value *value;
1123 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1124 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1125 error ("Illegal type of operand to `!'.");
1127 gen_usual_unary (ax, value);
1128 ax_simple (ax, aop_log_not);
1129 value->type = builtin_type_int;
1134 gen_complement (ax, value)
1135 struct agent_expr *ax;
1136 struct axs_value *value;
1138 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1139 error ("Illegal type of operand to `~'.");
1141 gen_usual_unary (ax, value);
1142 gen_integral_promotions (ax, value);
1143 ax_simple (ax, aop_bit_not);
1144 gen_extend (ax, value->type);
1149 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1151 /* Dereference the value on the top of the stack. */
1153 gen_deref (ax, value)
1154 struct agent_expr *ax;
1155 struct axs_value *value;
1157 /* The caller should check the type, because several operators use
1158 this, and we don't know what error message to generate. */
1159 if (value->type->code != TYPE_CODE_PTR)
1160 error ("GDB bug: ax-gdb.c (gen_deref): expected a pointer");
1162 /* We've got an rvalue now, which is a pointer. We want to yield an
1163 lvalue, whose address is exactly that pointer. So we don't
1164 actually emit any code; we just change the type from "Pointer to
1165 T" to "T", and mark the value as an lvalue in memory. Leave it
1166 to the consumer to actually dereference it. */
1167 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1168 value->kind = ((value->type->code == TYPE_CODE_FUNC)
1169 ? axs_rvalue : axs_lvalue_memory);
1173 /* Produce the address of the lvalue on the top of the stack. */
1175 gen_address_of (ax, value)
1176 struct agent_expr *ax;
1177 struct axs_value *value;
1179 /* Special case for taking the address of a function. The ANSI
1180 standard describes this as a special case, too, so this
1181 arrangement is not without motivation. */
1182 if (value->type->code == TYPE_CODE_FUNC)
1183 /* The value's already an rvalue on the stack, so we just need to
1185 value->type = lookup_pointer_type (value->type);
1187 switch (value->kind)
1190 error ("Operand of `&' is an rvalue, which has no address.");
1192 case axs_lvalue_register:
1193 error ("Operand of `&' is in a register, and has no address.");
1195 case axs_lvalue_memory:
1196 value->kind = axs_rvalue;
1197 value->type = lookup_pointer_type (value->type);
1203 /* A lot of this stuff will have to change to support C++. But we're
1204 not going to deal with that at the moment. */
1206 /* Find the field in the structure type TYPE named NAME, and return
1207 its index in TYPE's field array. */
1209 find_field (type, name)
1215 CHECK_TYPEDEF (type);
1217 /* Make sure this isn't C++. */
1218 if (TYPE_N_BASECLASSES (type) != 0)
1219 error ("GDB bug: ax-gdb.c (find_field): derived classes supported");
1221 for (i = 0; i < TYPE_NFIELDS (type); i++)
1223 char *this_name = TYPE_FIELD_NAME (type, i);
1225 if (this_name && STREQ (name, this_name))
1228 if (this_name[0] == '\0')
1229 error ("GDB bug: ax-gdb.c (find_field): anonymous unions not supported");
1232 error ("Couldn't find member named `%s' in struct/union `%s'",
1233 name, type->tag_name);
1239 /* Generate code to push the value of a bitfield of a structure whose
1240 address is on the top of the stack. START and END give the
1241 starting and one-past-ending *bit* numbers of the field within the
1244 gen_bitfield_ref (ax, value, type, start, end)
1245 struct agent_expr *ax;
1246 struct axs_value *value;
1250 /* Note that ops[i] fetches 8 << i bits. */
1251 static enum agent_op ops[]
1252 = { aop_ref8, aop_ref16, aop_ref32, aop_ref64 };
1253 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1255 /* We don't want to touch any byte that the bitfield doesn't
1256 actually occupy; we shouldn't make any accesses we're not
1257 explicitly permitted to. We rely here on the fact that the
1258 bytecode `ref' operators work on unaligned addresses.
1260 It takes some fancy footwork to get the stack to work the way
1261 we'd like. Say we're retrieving a bitfield that requires three
1262 fetches. Initially, the stack just contains the address:
1264 For the first fetch, we duplicate the address
1266 then add the byte offset, do the fetch, and shift and mask as
1267 needed, yielding a fragment of the value, properly aligned for
1268 the final bitwise or:
1270 then we swap, and repeat the process:
1271 frag1 addr --- address on top
1272 frag1 addr addr --- duplicate it
1273 frag1 addr frag2 --- get second fragment
1274 frag1 frag2 addr --- swap again
1275 frag1 frag2 frag3 --- get third fragment
1276 Notice that, since the third fragment is the last one, we don't
1277 bother duplicating the address this time. Now we have all the
1278 fragments on the stack, and we can simply `or' them together,
1279 yielding the final value of the bitfield. */
1281 /* The first and one-after-last bits in the field, but rounded down
1282 and up to byte boundaries. */
1283 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1284 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1288 /* current bit offset within the structure */
1291 /* The index in ops of the opcode we're considering. */
1294 /* The number of fragments we generated in the process. Probably
1295 equal to the number of `one' bits in bytesize, but who cares? */
1298 /* Dereference any typedefs. */
1299 type = check_typedef (type);
1301 /* Can we fetch the number of bits requested at all? */
1302 if ((end - start) > ((1 << num_ops) * 8))
1303 error ("GDB bug: ax-gdb.c (gen_bitfield_ref): bitfield too wide");
1305 /* Note that we know here that we only need to try each opcode once.
1306 That may not be true on machines with weird byte sizes. */
1307 offset = bound_start;
1309 for (op = num_ops - 1; op >= 0; op--)
1311 /* number of bits that ops[op] would fetch */
1312 int op_size = 8 << op;
1314 /* The stack at this point, from bottom to top, contains zero or
1315 more fragments, then the address. */
1317 /* Does this fetch fit within the bitfield? */
1318 if (offset + op_size <= bound_end)
1320 /* Is this the last fragment? */
1321 int last_frag = (offset + op_size == bound_end);
1324 ax_simple (ax, aop_dup); /* keep a copy of the address */
1326 /* Add the offset. */
1327 gen_offset (ax, offset / TARGET_CHAR_BIT);
1331 /* Record the area of memory we're about to fetch. */
1332 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1335 /* Perform the fetch. */
1336 ax_simple (ax, ops[op]);
1338 /* Shift the bits we have to their proper position.
1339 gen_left_shift will generate right shifts when the operand
1342 A big-endian field diagram to ponder:
1343 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1344 +------++------++------++------++------++------++------++------+
1345 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1347 bit number 16 32 48 53
1348 These are bit numbers as supplied by GDB. Note that the
1349 bit numbers run from right to left once you've fetched the
1352 A little-endian field diagram to ponder:
1353 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1354 +------++------++------++------++------++------++------++------+
1355 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1357 bit number 48 32 16 4 0
1359 In both cases, the most significant end is on the left
1360 (i.e. normal numeric writing order), which means that you
1361 don't go crazy thinking about `left' and `right' shifts.
1363 We don't have to worry about masking yet:
1364 - If they contain garbage off the least significant end, then we
1365 must be looking at the low end of the field, and the right
1366 shift will wipe them out.
1367 - If they contain garbage off the most significant end, then we
1368 must be looking at the most significant end of the word, and
1369 the sign/zero extension will wipe them out.
1370 - If we're in the interior of the word, then there is no garbage
1371 on either end, because the ref operators zero-extend. */
1372 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1373 gen_left_shift (ax, end - (offset + op_size));
1375 gen_left_shift (ax, offset - start);
1378 /* Bring the copy of the address up to the top. */
1379 ax_simple (ax, aop_swap);
1386 /* Generate enough bitwise `or' operations to combine all the
1387 fragments we left on the stack. */
1388 while (fragment_count-- > 1)
1389 ax_simple (ax, aop_bit_or);
1391 /* Sign- or zero-extend the value as appropriate. */
1392 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1394 /* This is *not* an lvalue. Ugh. */
1395 value->kind = axs_rvalue;
1400 /* Generate code to reference the member named FIELD of a structure or
1401 union. The top of the stack, as described by VALUE, should have
1402 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1403 the operator being compiled, and OPERAND_NAME is the kind of thing
1404 it operates on; we use them in error messages. */
1406 gen_struct_ref (ax, value, field, operator_name, operand_name)
1407 struct agent_expr *ax;
1408 struct axs_value *value;
1410 char *operator_name;
1416 /* Follow pointers until we reach a non-pointer. These aren't the C
1417 semantics, but they're what the normal GDB evaluator does, so we
1418 should at least be consistent. */
1419 while (value->type->code == TYPE_CODE_PTR)
1421 gen_usual_unary (ax, value);
1422 gen_deref (ax, value);
1426 /* This must yield a structure or a union. */
1427 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1428 && TYPE_CODE (type) != TYPE_CODE_UNION)
1429 error ("The left operand of `%s' is not a %s.",
1430 operator_name, operand_name);
1432 /* And it must be in memory; we don't deal with structure rvalues,
1433 or structures living in registers. */
1434 if (value->kind != axs_lvalue_memory)
1435 error ("Structure does not live in memory.");
1437 i = find_field (type, field);
1439 /* Is this a bitfield? */
1440 if (TYPE_FIELD_PACKED (type, i))
1441 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1442 TYPE_FIELD_BITPOS (type, i),
1443 (TYPE_FIELD_BITPOS (type, i)
1444 + TYPE_FIELD_BITSIZE (type, i)));
1447 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1448 value->kind = axs_lvalue_memory;
1449 value->type = TYPE_FIELD_TYPE (type, i);
1454 /* Generate code for GDB's magical `repeat' operator.
1455 LVALUE @ INT creates an array INT elements long, and whose elements
1456 have the same type as LVALUE, located in memory so that LVALUE is
1457 its first element. For example, argv[0]@argc gives you the array
1458 of command-line arguments.
1460 Unfortunately, because we have to know the types before we actually
1461 have a value for the expression, we can't implement this perfectly
1462 without changing the type system, having values that occupy two
1463 stack slots, doing weird things with sizeof, etc. So we require
1464 the right operand to be a constant expression. */
1466 gen_repeat (pc, ax, value)
1467 union exp_element **pc;
1468 struct agent_expr *ax;
1469 struct axs_value *value;
1471 struct axs_value value1;
1472 /* We don't want to turn this into an rvalue, so no conversions
1474 gen_expr (pc, ax, &value1);
1475 if (value1.kind != axs_lvalue_memory)
1476 error ("Left operand of `@' must be an object in memory.");
1478 /* Evaluate the length; it had better be a constant. */
1480 struct value *v = const_expr (pc);
1484 error ("Right operand of `@' must be a constant, in agent expressions.");
1485 if (v->type->code != TYPE_CODE_INT)
1486 error ("Right operand of `@' must be an integer.");
1487 length = value_as_long (v);
1489 error ("Right operand of `@' must be positive.");
1491 /* The top of the stack is already the address of the object, so
1492 all we need to do is frob the type of the lvalue. */
1494 /* FIXME-type-allocation: need a way to free this type when we are
1497 = create_range_type (0, builtin_type_int, 0, length - 1);
1498 struct type *array = create_array_type (0, value1.type, range);
1500 value->kind = axs_lvalue_memory;
1501 value->type = array;
1507 /* Emit code for the `sizeof' operator.
1508 *PC should point at the start of the operand expression; we advance it
1509 to the first instruction after the operand. */
1511 gen_sizeof (pc, ax, value)
1512 union exp_element **pc;
1513 struct agent_expr *ax;
1514 struct axs_value *value;
1516 /* We don't care about the value of the operand expression; we only
1517 care about its type. However, in the current arrangement, the
1518 only way to find an expression's type is to generate code for it.
1519 So we generate code for the operand, and then throw it away,
1520 replacing it with code that simply pushes its size. */
1521 int start = ax->len;
1522 gen_expr (pc, ax, value);
1524 /* Throw away the code we just generated. */
1527 ax_const_l (ax, TYPE_LENGTH (value->type));
1528 value->kind = axs_rvalue;
1529 value->type = builtin_type_int;
1533 /* Generating bytecode from GDB expressions: general recursive thingy */
1535 /* A gen_expr function written by a Gen-X'er guy.
1536 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1538 gen_expr (pc, ax, value)
1539 union exp_element **pc;
1540 struct agent_expr *ax;
1541 struct axs_value *value;
1543 /* Used to hold the descriptions of operand expressions. */
1544 struct axs_value value1, value2;
1545 enum exp_opcode op = (*pc)[0].opcode;
1547 /* If we're looking at a constant expression, just push its value. */
1549 struct value *v = maybe_const_expr (pc);
1553 ax_const_l (ax, value_as_long (v));
1554 value->kind = axs_rvalue;
1555 value->type = check_typedef (VALUE_TYPE (v));
1560 /* Otherwise, go ahead and generate code for it. */
1563 /* Binary arithmetic operators. */
1569 case BINOP_SUBSCRIPT:
1570 case BINOP_BITWISE_AND:
1571 case BINOP_BITWISE_IOR:
1572 case BINOP_BITWISE_XOR:
1574 gen_expr (pc, ax, &value1);
1575 gen_usual_unary (ax, &value1);
1576 gen_expr (pc, ax, &value2);
1577 gen_usual_unary (ax, &value2);
1578 gen_usual_arithmetic (ax, &value1, &value2);
1582 gen_add (ax, value, &value1, &value2, "addition");
1585 gen_sub (ax, value, &value1, &value2);
1588 gen_binop (ax, value, &value1, &value2,
1589 aop_mul, aop_mul, 1, "multiplication");
1592 gen_binop (ax, value, &value1, &value2,
1593 aop_div_signed, aop_div_unsigned, 1, "division");
1596 gen_binop (ax, value, &value1, &value2,
1597 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1599 case BINOP_SUBSCRIPT:
1600 gen_add (ax, value, &value1, &value2, "array subscripting");
1601 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1602 error ("Illegal combination of types in array subscripting.");
1603 gen_deref (ax, value);
1605 case BINOP_BITWISE_AND:
1606 gen_binop (ax, value, &value1, &value2,
1607 aop_bit_and, aop_bit_and, 0, "bitwise and");
1610 case BINOP_BITWISE_IOR:
1611 gen_binop (ax, value, &value1, &value2,
1612 aop_bit_or, aop_bit_or, 0, "bitwise or");
1615 case BINOP_BITWISE_XOR:
1616 gen_binop (ax, value, &value1, &value2,
1617 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1621 /* We should only list operators in the outer case statement
1622 that we actually handle in the inner case statement. */
1623 error ("GDB bug: ax-gdb.c (gen_expr): op case sets don't match");
1627 /* Note that we need to be a little subtle about generating code
1628 for comma. In C, we can do some optimizations here because
1629 we know the left operand is only being evaluated for effect.
1630 However, if the tracing kludge is in effect, then we always
1631 need to evaluate the left hand side fully, so that all the
1632 variables it mentions get traced. */
1635 gen_expr (pc, ax, &value1);
1636 /* Don't just dispose of the left operand. We might be tracing,
1637 in which case we want to emit code to trace it if it's an
1639 gen_traced_pop (ax, &value1);
1640 gen_expr (pc, ax, value);
1641 /* It's the consumer's responsibility to trace the right operand. */
1644 case OP_LONG: /* some integer constant */
1646 struct type *type = (*pc)[1].type;
1647 LONGEST k = (*pc)[2].longconst;
1649 gen_int_literal (ax, value, k, type);
1654 gen_var_ref (ax, value, (*pc)[2].symbol);
1660 int reg = (int) (*pc)[1].longconst;
1662 value->kind = axs_lvalue_register;
1664 value->type = REGISTER_VIRTUAL_TYPE (reg);
1668 case OP_INTERNALVAR:
1669 error ("GDB agent expressions cannot use convenience variables.");
1671 /* Weirdo operator: see comments for gen_repeat for details. */
1673 /* Note that gen_repeat handles its own argument evaluation. */
1675 gen_repeat (pc, ax, value);
1680 struct type *type = (*pc)[1].type;
1682 gen_expr (pc, ax, value);
1683 gen_cast (ax, value, type);
1689 struct type *type = check_typedef ((*pc)[1].type);
1691 gen_expr (pc, ax, value);
1692 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1693 it's just a hack for dealing with minsyms; you take some
1694 integer constant, pretend it's the address of an lvalue of
1695 the given type, and dereference it. */
1696 if (value->kind != axs_rvalue)
1697 /* This would be weird. */
1698 error ("GDB bug: ax-gdb.c (gen_expr): OP_MEMVAL operand isn't an rvalue???");
1700 value->kind = axs_lvalue_memory;
1706 /* -FOO is equivalent to 0 - FOO. */
1707 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
1708 gen_usual_unary (ax, &value1); /* shouldn't do much */
1709 gen_expr (pc, ax, &value2);
1710 gen_usual_unary (ax, &value2);
1711 gen_usual_arithmetic (ax, &value1, &value2);
1712 gen_sub (ax, value, &value1, &value2);
1715 case UNOP_LOGICAL_NOT:
1717 gen_expr (pc, ax, value);
1718 gen_logical_not (ax, value);
1721 case UNOP_COMPLEMENT:
1723 gen_expr (pc, ax, value);
1724 gen_complement (ax, value);
1729 gen_expr (pc, ax, value);
1730 gen_usual_unary (ax, value);
1731 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1732 error ("Argument of unary `*' is not a pointer.");
1733 gen_deref (ax, value);
1738 gen_expr (pc, ax, value);
1739 gen_address_of (ax, value);
1744 /* Notice that gen_sizeof handles its own operand, unlike most
1745 of the other unary operator functions. This is because we
1746 have to throw away the code we generate. */
1747 gen_sizeof (pc, ax, value);
1750 case STRUCTOP_STRUCT:
1753 int length = (*pc)[1].longconst;
1754 char *name = &(*pc)[2].string;
1756 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1757 gen_expr (pc, ax, value);
1758 if (op == STRUCTOP_STRUCT)
1759 gen_struct_ref (ax, value, name, ".", "structure or union");
1760 else if (op == STRUCTOP_PTR)
1761 gen_struct_ref (ax, value, name, "->",
1762 "pointer to a structure or union");
1764 /* If this `if' chain doesn't handle it, then the case list
1765 shouldn't mention it, and we shouldn't be here. */
1766 error ("GDB bug: ax-gdb.c (gen_expr): unhandled struct case");
1771 error ("Attempt to use a type name as an expression.");
1774 error ("Unsupported operator in expression.");
1780 #if 0 /* not used */
1781 /* Generating bytecode from GDB expressions: driver */
1783 /* Given a GDB expression EXPR, produce a string of agent bytecode
1784 which computes its value. Return the agent expression, and set
1785 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1787 expr_to_agent (expr, value)
1788 struct expression *expr;
1789 struct axs_value *value;
1791 struct cleanup *old_chain = 0;
1792 struct agent_expr *ax = new_agent_expr ();
1793 union exp_element *pc;
1795 old_chain = make_cleanup ((make_cleanup_func) free_agent_expr, ax);
1799 gen_expr (&pc, ax, value);
1801 /* We have successfully built the agent expr, so cancel the cleanup
1802 request. If we add more cleanups that we always want done, this
1803 will have to get more complicated. */
1804 discard_cleanups (old_chain);
1809 /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1810 string of agent bytecode which will leave its address and size on
1811 the top of stack. Return the agent expression.
1813 Not sure this function is useful at all. */
1815 expr_to_address_and_size (expr)
1816 struct expression *expr;
1818 struct axs_value value;
1819 struct agent_expr *ax = expr_to_agent (expr, &value);
1821 /* Complain if the result is not a memory lvalue. */
1822 if (value.kind != axs_lvalue_memory)
1824 free_agent_expr (ax);
1825 error ("Expression does not denote an object in memory.");
1828 /* Push the object's size on the stack. */
1829 ax_const_l (ax, TYPE_LENGTH (value.type));
1835 /* Given a GDB expression EXPR, return bytecode to trace its value.
1836 The result will use the `trace' and `trace_quick' bytecodes to
1837 record the value of all memory touched by the expression. The
1838 caller can then use the ax_reqs function to discover which
1839 registers it relies upon. */
1841 gen_trace_for_expr (scope, expr)
1843 struct expression *expr;
1845 struct cleanup *old_chain = 0;
1846 struct agent_expr *ax = new_agent_expr (scope);
1847 union exp_element *pc;
1848 struct axs_value value;
1850 old_chain = make_cleanup ((make_cleanup_func) free_agent_expr, ax);
1854 gen_expr (&pc, ax, &value);
1856 /* Make sure we record the final object, and get rid of it. */
1857 gen_traced_pop (ax, &value);
1859 /* Oh, and terminate. */
1860 ax_simple (ax, aop_end);
1862 /* We have successfully built the agent expr, so cancel the cleanup
1863 request. If we add more cleanups that we always want done, this
1864 will have to get more complicated. */
1865 discard_cleanups (old_chain);
1871 /* The "agent" command, for testing: compile and disassemble an expression. */
1874 print_axs_value (f, value)
1876 struct axs_value *value;
1878 switch (value->kind)
1881 fputs_filtered ("rvalue", f);
1884 case axs_lvalue_memory:
1885 fputs_filtered ("memory lvalue", f);
1888 case axs_lvalue_register:
1889 fprintf_filtered (f, "register %d lvalue", value->u.reg);
1893 fputs_filtered (" : ", f);
1894 type_print (value->type, "", f, -1);
1899 agent_command (exp, from_tty)
1903 struct cleanup *old_chain = 0;
1904 struct expression *expr;
1905 struct agent_expr *agent;
1906 struct agent_reqs reqs;
1907 struct frame_info *fi = get_current_frame (); /* need current scope */
1909 /* We don't deal with overlay debugging at the moment. We need to
1910 think more carefully about this. If you copy this code into
1911 another command, change the error message; the user shouldn't
1912 have to know anything about agent expressions. */
1913 if (overlay_debugging)
1914 error ("GDB can't do agent expression translation with overlays.");
1917 error_no_arg ("expression to translate");
1919 expr = parse_expression (exp);
1920 old_chain = make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1921 agent = gen_trace_for_expr (fi->pc, expr);
1922 make_cleanup ((make_cleanup_func) free_agent_expr, agent);
1923 ax_print (gdb_stdout, agent);
1924 ax_reqs (agent, &reqs);
1926 do_cleanups (old_chain);
1931 /* Initialization code. */
1933 void _initialize_ax_gdb PARAMS ((void));
1935 _initialize_ax_gdb ()
1937 struct cmd_list_element *c;
1939 add_cmd ("agent", class_maintenance, agent_command,
1940 "Translate an expression into remote agent bytecode.",