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 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,
77 struct symbol * var));
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,
90 struct type * type2));
91 static struct type *max_type PARAMS ((struct type * type1,
92 struct type * type2));
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,
105 struct type * type));
106 static void gen_scale PARAMS ((struct agent_expr * ax,
108 struct type * type));
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 (struct ui_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_pointer (type, (CORE_ADDR) 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:
426 ax_simple (ax, aop_ref8);
428 case 16 / TARGET_CHAR_BIT:
429 ax_simple (ax, aop_ref16);
431 case 32 / TARGET_CHAR_BIT:
432 ax_simple (ax, aop_ref32);
434 case 64 / TARGET_CHAR_BIT:
435 ax_simple (ax, aop_ref64);
438 /* Either our caller shouldn't have asked us to dereference
439 that pointer (other code's fault), or we're not
440 implementing something we should be (this code's fault).
441 In any case, it's a bug the user shouldn't see. */
443 internal_error ("ax-gdb.c (gen_fetch): strange size");
446 gen_sign_extend (ax, type);
450 /* Either our caller shouldn't have asked us to dereference that
451 pointer (other code's fault), or we're not implementing
452 something we should be (this code's fault). In any case,
453 it's a bug the user shouldn't see. */
454 internal_error ("ax-gdb.c (gen_fetch): bad type code");
459 /* Generate code to left shift the top of the stack by DISTANCE bits, or
460 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
461 unsigned (logical) right shifts. */
463 gen_left_shift (ax, distance)
464 struct agent_expr *ax;
469 ax_const_l (ax, distance);
470 ax_simple (ax, aop_lsh);
472 else if (distance < 0)
474 ax_const_l (ax, -distance);
475 ax_simple (ax, aop_rsh_unsigned);
481 /* Generating bytecode from GDB expressions: symbol references */
483 /* Generate code to push the base address of the argument portion of
484 the top stack frame. */
486 gen_frame_args_address (ax)
487 struct agent_expr *ax;
489 long frame_reg, frame_offset;
491 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
492 ax_reg (ax, frame_reg);
493 gen_offset (ax, frame_offset);
497 /* Generate code to push the base address of the locals portion of the
500 gen_frame_locals_address (ax)
501 struct agent_expr *ax;
503 long frame_reg, frame_offset;
505 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
506 ax_reg (ax, frame_reg);
507 gen_offset (ax, frame_offset);
511 /* Generate code to add OFFSET to the top of the stack. Try to
512 generate short and readable code. We use this for getting to
513 variables on the stack, and structure members. If we were
514 programming in ML, it would be clearer why these are the same
517 gen_offset (ax, offset)
518 struct agent_expr *ax;
521 /* It would suffice to simply push the offset and add it, but this
522 makes it easier to read positive and negative offsets in the
526 ax_const_l (ax, offset);
527 ax_simple (ax, aop_add);
531 ax_const_l (ax, -offset);
532 ax_simple (ax, aop_sub);
537 /* In many cases, a symbol's value is the offset from some other
538 address (stack frame, base register, etc.) Generate code to add
539 VAR's value to the top of the stack. */
541 gen_sym_offset (ax, var)
542 struct agent_expr *ax;
545 gen_offset (ax, SYMBOL_VALUE (var));
549 /* Generate code for a variable reference to AX. The variable is the
550 symbol VAR. Set VALUE to describe the result. */
553 gen_var_ref (ax, value, var)
554 struct agent_expr *ax;
555 struct axs_value *value;
558 /* Dereference any typedefs. */
559 value->type = check_typedef (SYMBOL_TYPE (var));
561 /* I'm imitating the code in read_var_value. */
562 switch (SYMBOL_CLASS (var))
564 case LOC_CONST: /* A constant, like an enum value. */
565 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
566 value->kind = axs_rvalue;
569 case LOC_LABEL: /* A goto label, being used as a value. */
570 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
571 value->kind = axs_rvalue;
574 case LOC_CONST_BYTES:
575 internal_error ("ax-gdb.c (gen_var_ref): LOC_CONST_BYTES symbols are not supported");
577 /* Variable at a fixed location in memory. Easy. */
579 /* Push the address of the variable. */
580 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
581 value->kind = axs_lvalue_memory;
584 case LOC_ARG: /* var lives in argument area of frame */
585 gen_frame_args_address (ax);
586 gen_sym_offset (ax, var);
587 value->kind = axs_lvalue_memory;
590 case LOC_REF_ARG: /* As above, but the frame slot really
591 holds the address of the variable. */
592 gen_frame_args_address (ax);
593 gen_sym_offset (ax, var);
594 /* Don't assume any particular pointer size. */
595 gen_fetch (ax, lookup_pointer_type (builtin_type_void));
596 value->kind = axs_lvalue_memory;
599 case LOC_LOCAL: /* var lives in locals area of frame */
601 gen_frame_locals_address (ax);
602 gen_sym_offset (ax, var);
603 value->kind = axs_lvalue_memory;
606 case LOC_BASEREG: /* relative to some base register */
607 case LOC_BASEREG_ARG:
608 ax_reg (ax, SYMBOL_BASEREG (var));
609 gen_sym_offset (ax, var);
610 value->kind = axs_lvalue_memory;
614 error ("Cannot compute value of typedef `%s'.",
615 SYMBOL_SOURCE_NAME (var));
619 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
620 value->kind = axs_rvalue;
625 /* Don't generate any code at all; in the process of treating
626 this as an lvalue or rvalue, the caller will generate the
628 value->kind = axs_lvalue_register;
629 value->u.reg = SYMBOL_VALUE (var);
632 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
633 register, not on the stack. Simpler than LOC_REGISTER and
634 LOC_REGPARM, because it's just like any other case where the
635 thing has a real address. */
636 case LOC_REGPARM_ADDR:
637 ax_reg (ax, SYMBOL_VALUE (var));
638 value->kind = axs_lvalue_memory;
643 struct minimal_symbol *msym
644 = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
646 error ("Couldn't resolve symbol `%s'.", SYMBOL_SOURCE_NAME (var));
648 /* Push the address of the variable. */
649 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
650 value->kind = axs_lvalue_memory;
654 case LOC_OPTIMIZED_OUT:
655 error ("The variable `%s' has been optimized out.",
656 SYMBOL_SOURCE_NAME (var));
660 error ("Cannot find value of botched symbol `%s'.",
661 SYMBOL_SOURCE_NAME (var));
668 /* Generating bytecode from GDB expressions: literals */
671 gen_int_literal (ax, value, k, type)
672 struct agent_expr *ax;
673 struct axs_value *value;
678 value->kind = axs_rvalue;
684 /* Generating bytecode from GDB expressions: unary conversions, casts */
686 /* Take what's on the top of the stack (as described by VALUE), and
687 try to make an rvalue out of it. Signal an error if we can't do
690 require_rvalue (ax, value)
691 struct agent_expr *ax;
692 struct axs_value *value;
697 /* It's already an rvalue. */
700 case axs_lvalue_memory:
701 /* The top of stack is the address of the object. Dereference. */
702 gen_fetch (ax, value->type);
705 case axs_lvalue_register:
706 /* There's nothing on the stack, but value->u.reg is the
707 register number containing the value.
709 When we add floating-point support, this is going to have to
710 change. What about SPARC register pairs, for example? */
711 ax_reg (ax, value->u.reg);
712 gen_extend (ax, value->type);
716 value->kind = axs_rvalue;
720 /* Assume the top of the stack is described by VALUE, and perform the
721 usual unary conversions. This is motivated by ANSI 6.2.2, but of
722 course GDB expressions are not ANSI; they're the mishmash union of
723 a bunch of languages. Rah.
725 NOTE! This function promises to produce an rvalue only when the
726 incoming value is of an appropriate type. In other words, the
727 consumer of the value this function produces may assume the value
728 is an rvalue only after checking its type.
730 The immediate issue is that if the user tries to use a structure or
731 union as an operand of, say, the `+' operator, we don't want to try
732 to convert that structure to an rvalue; require_rvalue will bomb on
733 structs and unions. Rather, we want to simply pass the struct
734 lvalue through unchanged, and let `+' raise an error. */
737 gen_usual_unary (ax, value)
738 struct agent_expr *ax;
739 struct axs_value *value;
741 /* We don't have to generate any code for the usual integral
742 conversions, since values are always represented as full-width on
743 the stack. Should we tweak the type? */
745 /* Some types require special handling. */
746 switch (value->type->code)
748 /* Functions get converted to a pointer to the function. */
750 value->type = lookup_pointer_type (value->type);
751 value->kind = axs_rvalue; /* Should always be true, but just in case. */
754 /* Arrays get converted to a pointer to their first element, and
755 are no longer an lvalue. */
756 case TYPE_CODE_ARRAY:
758 struct type *elements = TYPE_TARGET_TYPE (value->type);
759 value->type = lookup_pointer_type (elements);
760 value->kind = axs_rvalue;
761 /* We don't need to generate any code; the address of the array
762 is also the address of its first element. */
766 /* Don't try to convert structures and unions to rvalues. Let the
767 consumer signal an error. */
768 case TYPE_CODE_STRUCT:
769 case TYPE_CODE_UNION:
772 /* If the value is an enum, call it an integer. */
774 value->type = builtin_type_int;
778 /* If the value is an lvalue, dereference it. */
779 require_rvalue (ax, value);
783 /* Return non-zero iff the type TYPE1 is considered "wider" than the
784 type TYPE2, according to the rules described in gen_usual_arithmetic. */
786 type_wider_than (type1, type2)
787 struct type *type1, *type2;
789 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
790 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
791 && TYPE_UNSIGNED (type1)
792 && !TYPE_UNSIGNED (type2)));
796 /* Return the "wider" of the two types TYPE1 and TYPE2. */
798 max_type (type1, type2)
799 struct type *type1, *type2;
801 return type_wider_than (type1, type2) ? type1 : type2;
805 /* Generate code to convert a scalar value of type FROM to type TO. */
807 gen_conversion (ax, from, to)
808 struct agent_expr *ax;
809 struct type *from, *to;
811 /* Perhaps there is a more graceful way to state these rules. */
813 /* If we're converting to a narrower type, then we need to clear out
815 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
816 gen_extend (ax, from);
818 /* If the two values have equal width, but different signednesses,
819 then we need to extend. */
820 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
822 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
826 /* If we're converting to a wider type, and becoming unsigned, then
827 we need to zero out any possible sign bits. */
828 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
830 if (TYPE_UNSIGNED (to))
836 /* Return non-zero iff the type FROM will require any bytecodes to be
837 emitted to be converted to the type TO. */
839 is_nontrivial_conversion (from, to)
840 struct type *from, *to;
842 struct agent_expr *ax = new_agent_expr (0);
845 /* Actually generate the code, and see if anything came out. At the
846 moment, it would be trivial to replicate the code in
847 gen_conversion here, but in the future, when we're supporting
848 floating point and the like, it may not be. Doing things this
849 way allows this function to be independent of the logic in
851 gen_conversion (ax, from, to);
852 nontrivial = ax->len > 0;
853 free_agent_expr (ax);
858 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
859 6.2.1.5) for the two operands of an arithmetic operator. This
860 effectively finds a "least upper bound" type for the two arguments,
861 and promotes each argument to that type. *VALUE1 and *VALUE2
862 describe the values as they are passed in, and as they are left. */
864 gen_usual_arithmetic (ax, value1, value2)
865 struct agent_expr *ax;
866 struct axs_value *value1, *value2;
868 /* Do the usual binary conversions. */
869 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
870 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
872 /* The ANSI integral promotions seem to work this way: Order the
873 integer types by size, and then by signedness: an n-bit
874 unsigned type is considered "wider" than an n-bit signed
875 type. Promote to the "wider" of the two types, and always
876 promote at least to int. */
877 struct type *target = max_type (builtin_type_int,
878 max_type (value1->type, value2->type));
880 /* Deal with value2, on the top of the stack. */
881 gen_conversion (ax, value2->type, target);
883 /* Deal with value1, not on the top of the stack. Don't
884 generate the `swap' instructions if we're not actually going
886 if (is_nontrivial_conversion (value1->type, target))
888 ax_simple (ax, aop_swap);
889 gen_conversion (ax, value1->type, target);
890 ax_simple (ax, aop_swap);
893 value1->type = value2->type = target;
898 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
899 the value on the top of the stack, as described by VALUE. Assume
900 the value has integral type. */
902 gen_integral_promotions (ax, value)
903 struct agent_expr *ax;
904 struct axs_value *value;
906 if (!type_wider_than (value->type, builtin_type_int))
908 gen_conversion (ax, value->type, builtin_type_int);
909 value->type = builtin_type_int;
911 else if (!type_wider_than (value->type, builtin_type_unsigned_int))
913 gen_conversion (ax, value->type, builtin_type_unsigned_int);
914 value->type = builtin_type_unsigned_int;
919 /* Generate code for a cast to TYPE. */
921 gen_cast (ax, value, type)
922 struct agent_expr *ax;
923 struct axs_value *value;
926 /* GCC does allow casts to yield lvalues, so this should be fixed
927 before merging these changes into the trunk. */
928 require_rvalue (ax, value);
929 /* Dereference typedefs. */
930 type = check_typedef (type);
935 /* It's implementation-defined, and I'll bet this is what GCC
939 case TYPE_CODE_ARRAY:
940 case TYPE_CODE_STRUCT:
941 case TYPE_CODE_UNION:
943 error ("Illegal type cast: intended type must be scalar.");
946 /* We don't have to worry about the size of the value, because
947 all our integral values are fully sign-extended, and when
948 casting pointers we can do anything we like. Is there any
949 way for us to actually know what GCC actually does with a
955 gen_conversion (ax, value->type, type);
959 /* We could pop the value, and rely on everyone else to check
960 the type and notice that this value doesn't occupy a stack
961 slot. But for now, leave the value on the stack, and
962 preserve the "value == stack element" assumption. */
966 error ("Casts to requested type are not yet implemented.");
974 /* Generating bytecode from GDB expressions: arithmetic */
976 /* Scale the integer on the top of the stack by the size of the target
977 of the pointer type TYPE. */
979 gen_scale (ax, op, type)
980 struct agent_expr *ax;
984 struct type *element = TYPE_TARGET_TYPE (type);
986 if (element->length != 1)
988 ax_const_l (ax, element->length);
994 /* Generate code for an addition; non-trivial because we deal with
995 pointer arithmetic. We set VALUE to describe the result value; we
996 assume VALUE1 and VALUE2 describe the two operands, and that
997 they've undergone the usual binary conversions. Used by both
998 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
1000 gen_add (ax, value, value1, value2, name)
1001 struct agent_expr *ax;
1002 struct axs_value *value, *value1, *value2;
1005 /* Is it INT+PTR? */
1006 if (value1->type->code == TYPE_CODE_INT
1007 && value2->type->code == TYPE_CODE_PTR)
1009 /* Swap the values and proceed normally. */
1010 ax_simple (ax, aop_swap);
1011 gen_scale (ax, aop_mul, value2->type);
1012 ax_simple (ax, aop_add);
1013 gen_extend (ax, value2->type); /* Catch overflow. */
1014 value->type = value2->type;
1017 /* Is it PTR+INT? */
1018 else if (value1->type->code == TYPE_CODE_PTR
1019 && value2->type->code == TYPE_CODE_INT)
1021 gen_scale (ax, aop_mul, value1->type);
1022 ax_simple (ax, aop_add);
1023 gen_extend (ax, value1->type); /* Catch overflow. */
1024 value->type = value1->type;
1027 /* Must be number + number; the usual binary conversions will have
1028 brought them both to the same width. */
1029 else if (value1->type->code == TYPE_CODE_INT
1030 && value2->type->code == TYPE_CODE_INT)
1032 ax_simple (ax, aop_add);
1033 gen_extend (ax, value1->type); /* Catch overflow. */
1034 value->type = value1->type;
1038 error ("Illegal combination of types in %s.", name);
1040 value->kind = axs_rvalue;
1044 /* Generate code for an addition; non-trivial because we have to deal
1045 with pointer arithmetic. We set VALUE to describe the result
1046 value; we assume VALUE1 and VALUE2 describe the two operands, and
1047 that they've undergone the usual binary conversions. */
1049 gen_sub (ax, value, value1, value2)
1050 struct agent_expr *ax;
1051 struct axs_value *value, *value1, *value2;
1053 if (value1->type->code == TYPE_CODE_PTR)
1055 /* Is it PTR - INT? */
1056 if (value2->type->code == TYPE_CODE_INT)
1058 gen_scale (ax, aop_mul, value1->type);
1059 ax_simple (ax, aop_sub);
1060 gen_extend (ax, value1->type); /* Catch overflow. */
1061 value->type = value1->type;
1064 /* Is it PTR - PTR? Strictly speaking, the types ought to
1065 match, but this is what the normal GDB expression evaluator
1067 else if (value2->type->code == TYPE_CODE_PTR
1068 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1069 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1071 ax_simple (ax, aop_sub);
1072 gen_scale (ax, aop_div_unsigned, value1->type);
1073 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
1077 First argument of `-' is a pointer, but second argument is neither\n\
1078 an integer nor a pointer of the same type.");
1081 /* Must be number + number. */
1082 else if (value1->type->code == TYPE_CODE_INT
1083 && value2->type->code == TYPE_CODE_INT)
1085 ax_simple (ax, aop_sub);
1086 gen_extend (ax, value1->type); /* Catch overflow. */
1087 value->type = value1->type;
1091 error ("Illegal combination of types in subtraction.");
1093 value->kind = axs_rvalue;
1096 /* Generate code for a binary operator that doesn't do pointer magic.
1097 We set VALUE to describe the result value; we assume VALUE1 and
1098 VALUE2 describe the two operands, and that they've undergone the
1099 usual binary conversions. MAY_CARRY should be non-zero iff the
1100 result needs to be extended. NAME is the English name of the
1101 operator, used in error messages */
1103 gen_binop (ax, value, value1, value2, op, op_unsigned, may_carry, name)
1104 struct agent_expr *ax;
1105 struct axs_value *value, *value1, *value2;
1106 enum agent_op op, op_unsigned;
1110 /* We only handle INT op INT. */
1111 if ((value1->type->code != TYPE_CODE_INT)
1112 || (value2->type->code != TYPE_CODE_INT))
1113 error ("Illegal combination of types in %s.", name);
1116 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1118 gen_extend (ax, value1->type); /* catch overflow */
1119 value->type = value1->type;
1120 value->kind = axs_rvalue;
1125 gen_logical_not (ax, value)
1126 struct agent_expr *ax;
1127 struct axs_value *value;
1129 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1130 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1131 error ("Illegal type of operand to `!'.");
1133 gen_usual_unary (ax, value);
1134 ax_simple (ax, aop_log_not);
1135 value->type = builtin_type_int;
1140 gen_complement (ax, value)
1141 struct agent_expr *ax;
1142 struct axs_value *value;
1144 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1145 error ("Illegal type of operand to `~'.");
1147 gen_usual_unary (ax, value);
1148 gen_integral_promotions (ax, value);
1149 ax_simple (ax, aop_bit_not);
1150 gen_extend (ax, value->type);
1155 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1157 /* Dereference the value on the top of the stack. */
1159 gen_deref (ax, value)
1160 struct agent_expr *ax;
1161 struct axs_value *value;
1163 /* The caller should check the type, because several operators use
1164 this, and we don't know what error message to generate. */
1165 if (value->type->code != TYPE_CODE_PTR)
1166 internal_error ("ax-gdb.c (gen_deref): expected a pointer");
1168 /* We've got an rvalue now, which is a pointer. We want to yield an
1169 lvalue, whose address is exactly that pointer. So we don't
1170 actually emit any code; we just change the type from "Pointer to
1171 T" to "T", and mark the value as an lvalue in memory. Leave it
1172 to the consumer to actually dereference it. */
1173 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1174 value->kind = ((value->type->code == TYPE_CODE_FUNC)
1175 ? axs_rvalue : axs_lvalue_memory);
1179 /* Produce the address of the lvalue on the top of the stack. */
1181 gen_address_of (ax, value)
1182 struct agent_expr *ax;
1183 struct axs_value *value;
1185 /* Special case for taking the address of a function. The ANSI
1186 standard describes this as a special case, too, so this
1187 arrangement is not without motivation. */
1188 if (value->type->code == TYPE_CODE_FUNC)
1189 /* The value's already an rvalue on the stack, so we just need to
1191 value->type = lookup_pointer_type (value->type);
1193 switch (value->kind)
1196 error ("Operand of `&' is an rvalue, which has no address.");
1198 case axs_lvalue_register:
1199 error ("Operand of `&' is in a register, and has no address.");
1201 case axs_lvalue_memory:
1202 value->kind = axs_rvalue;
1203 value->type = lookup_pointer_type (value->type);
1209 /* A lot of this stuff will have to change to support C++. But we're
1210 not going to deal with that at the moment. */
1212 /* Find the field in the structure type TYPE named NAME, and return
1213 its index in TYPE's field array. */
1215 find_field (type, name)
1221 CHECK_TYPEDEF (type);
1223 /* Make sure this isn't C++. */
1224 if (TYPE_N_BASECLASSES (type) != 0)
1225 internal_error ("ax-gdb.c (find_field): derived classes supported");
1227 for (i = 0; i < TYPE_NFIELDS (type); i++)
1229 char *this_name = TYPE_FIELD_NAME (type, i);
1231 if (this_name && STREQ (name, this_name))
1234 if (this_name[0] == '\0')
1235 internal_error ("ax-gdb.c (find_field): anonymous unions not supported");
1238 error ("Couldn't find member named `%s' in struct/union `%s'",
1239 name, type->tag_name);
1245 /* Generate code to push the value of a bitfield of a structure whose
1246 address is on the top of the stack. START and END give the
1247 starting and one-past-ending *bit* numbers of the field within the
1250 gen_bitfield_ref (ax, value, type, start, end)
1251 struct agent_expr *ax;
1252 struct axs_value *value;
1256 /* Note that ops[i] fetches 8 << i bits. */
1257 static enum agent_op ops[]
1259 {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1260 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1262 /* We don't want to touch any byte that the bitfield doesn't
1263 actually occupy; we shouldn't make any accesses we're not
1264 explicitly permitted to. We rely here on the fact that the
1265 bytecode `ref' operators work on unaligned addresses.
1267 It takes some fancy footwork to get the stack to work the way
1268 we'd like. Say we're retrieving a bitfield that requires three
1269 fetches. Initially, the stack just contains the address:
1271 For the first fetch, we duplicate the address
1273 then add the byte offset, do the fetch, and shift and mask as
1274 needed, yielding a fragment of the value, properly aligned for
1275 the final bitwise or:
1277 then we swap, and repeat the process:
1278 frag1 addr --- address on top
1279 frag1 addr addr --- duplicate it
1280 frag1 addr frag2 --- get second fragment
1281 frag1 frag2 addr --- swap again
1282 frag1 frag2 frag3 --- get third fragment
1283 Notice that, since the third fragment is the last one, we don't
1284 bother duplicating the address this time. Now we have all the
1285 fragments on the stack, and we can simply `or' them together,
1286 yielding the final value of the bitfield. */
1288 /* The first and one-after-last bits in the field, but rounded down
1289 and up to byte boundaries. */
1290 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1291 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1295 /* current bit offset within the structure */
1298 /* The index in ops of the opcode we're considering. */
1301 /* The number of fragments we generated in the process. Probably
1302 equal to the number of `one' bits in bytesize, but who cares? */
1305 /* Dereference any typedefs. */
1306 type = check_typedef (type);
1308 /* Can we fetch the number of bits requested at all? */
1309 if ((end - start) > ((1 << num_ops) * 8))
1310 internal_error ("ax-gdb.c (gen_bitfield_ref): bitfield too wide");
1312 /* Note that we know here that we only need to try each opcode once.
1313 That may not be true on machines with weird byte sizes. */
1314 offset = bound_start;
1316 for (op = num_ops - 1; op >= 0; op--)
1318 /* number of bits that ops[op] would fetch */
1319 int op_size = 8 << op;
1321 /* The stack at this point, from bottom to top, contains zero or
1322 more fragments, then the address. */
1324 /* Does this fetch fit within the bitfield? */
1325 if (offset + op_size <= bound_end)
1327 /* Is this the last fragment? */
1328 int last_frag = (offset + op_size == bound_end);
1331 ax_simple (ax, aop_dup); /* keep a copy of the address */
1333 /* Add the offset. */
1334 gen_offset (ax, offset / TARGET_CHAR_BIT);
1338 /* Record the area of memory we're about to fetch. */
1339 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1342 /* Perform the fetch. */
1343 ax_simple (ax, ops[op]);
1345 /* Shift the bits we have to their proper position.
1346 gen_left_shift will generate right shifts when the operand
1349 A big-endian field diagram to ponder:
1350 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1351 +------++------++------++------++------++------++------++------+
1352 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1354 bit number 16 32 48 53
1355 These are bit numbers as supplied by GDB. Note that the
1356 bit numbers run from right to left once you've fetched the
1359 A little-endian field diagram to ponder:
1360 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1361 +------++------++------++------++------++------++------++------+
1362 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1364 bit number 48 32 16 4 0
1366 In both cases, the most significant end is on the left
1367 (i.e. normal numeric writing order), which means that you
1368 don't go crazy thinking about `left' and `right' shifts.
1370 We don't have to worry about masking yet:
1371 - If they contain garbage off the least significant end, then we
1372 must be looking at the low end of the field, and the right
1373 shift will wipe them out.
1374 - If they contain garbage off the most significant end, then we
1375 must be looking at the most significant end of the word, and
1376 the sign/zero extension will wipe them out.
1377 - If we're in the interior of the word, then there is no garbage
1378 on either end, because the ref operators zero-extend. */
1379 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1380 gen_left_shift (ax, end - (offset + op_size));
1382 gen_left_shift (ax, offset - start);
1385 /* Bring the copy of the address up to the top. */
1386 ax_simple (ax, aop_swap);
1393 /* Generate enough bitwise `or' operations to combine all the
1394 fragments we left on the stack. */
1395 while (fragment_count-- > 1)
1396 ax_simple (ax, aop_bit_or);
1398 /* Sign- or zero-extend the value as appropriate. */
1399 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1401 /* This is *not* an lvalue. Ugh. */
1402 value->kind = axs_rvalue;
1407 /* Generate code to reference the member named FIELD of a structure or
1408 union. The top of the stack, as described by VALUE, should have
1409 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1410 the operator being compiled, and OPERAND_NAME is the kind of thing
1411 it operates on; we use them in error messages. */
1413 gen_struct_ref (ax, value, field, operator_name, operand_name)
1414 struct agent_expr *ax;
1415 struct axs_value *value;
1417 char *operator_name;
1423 /* Follow pointers until we reach a non-pointer. These aren't the C
1424 semantics, but they're what the normal GDB evaluator does, so we
1425 should at least be consistent. */
1426 while (value->type->code == TYPE_CODE_PTR)
1428 gen_usual_unary (ax, value);
1429 gen_deref (ax, value);
1433 /* This must yield a structure or a union. */
1434 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1435 && TYPE_CODE (type) != TYPE_CODE_UNION)
1436 error ("The left operand of `%s' is not a %s.",
1437 operator_name, operand_name);
1439 /* And it must be in memory; we don't deal with structure rvalues,
1440 or structures living in registers. */
1441 if (value->kind != axs_lvalue_memory)
1442 error ("Structure does not live in memory.");
1444 i = find_field (type, field);
1446 /* Is this a bitfield? */
1447 if (TYPE_FIELD_PACKED (type, i))
1448 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1449 TYPE_FIELD_BITPOS (type, i),
1450 (TYPE_FIELD_BITPOS (type, i)
1451 + TYPE_FIELD_BITSIZE (type, i)));
1454 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1455 value->kind = axs_lvalue_memory;
1456 value->type = TYPE_FIELD_TYPE (type, i);
1461 /* Generate code for GDB's magical `repeat' operator.
1462 LVALUE @ INT creates an array INT elements long, and whose elements
1463 have the same type as LVALUE, located in memory so that LVALUE is
1464 its first element. For example, argv[0]@argc gives you the array
1465 of command-line arguments.
1467 Unfortunately, because we have to know the types before we actually
1468 have a value for the expression, we can't implement this perfectly
1469 without changing the type system, having values that occupy two
1470 stack slots, doing weird things with sizeof, etc. So we require
1471 the right operand to be a constant expression. */
1473 gen_repeat (pc, ax, value)
1474 union exp_element **pc;
1475 struct agent_expr *ax;
1476 struct axs_value *value;
1478 struct axs_value value1;
1479 /* We don't want to turn this into an rvalue, so no conversions
1481 gen_expr (pc, ax, &value1);
1482 if (value1.kind != axs_lvalue_memory)
1483 error ("Left operand of `@' must be an object in memory.");
1485 /* Evaluate the length; it had better be a constant. */
1487 struct value *v = const_expr (pc);
1491 error ("Right operand of `@' must be a constant, in agent expressions.");
1492 if (v->type->code != TYPE_CODE_INT)
1493 error ("Right operand of `@' must be an integer.");
1494 length = value_as_long (v);
1496 error ("Right operand of `@' must be positive.");
1498 /* The top of the stack is already the address of the object, so
1499 all we need to do is frob the type of the lvalue. */
1501 /* FIXME-type-allocation: need a way to free this type when we are
1504 = create_range_type (0, builtin_type_int, 0, length - 1);
1505 struct type *array = create_array_type (0, value1.type, range);
1507 value->kind = axs_lvalue_memory;
1508 value->type = array;
1514 /* Emit code for the `sizeof' operator.
1515 *PC should point at the start of the operand expression; we advance it
1516 to the first instruction after the operand. */
1518 gen_sizeof (pc, ax, value)
1519 union exp_element **pc;
1520 struct agent_expr *ax;
1521 struct axs_value *value;
1523 /* We don't care about the value of the operand expression; we only
1524 care about its type. However, in the current arrangement, the
1525 only way to find an expression's type is to generate code for it.
1526 So we generate code for the operand, and then throw it away,
1527 replacing it with code that simply pushes its size. */
1528 int start = ax->len;
1529 gen_expr (pc, ax, value);
1531 /* Throw away the code we just generated. */
1534 ax_const_l (ax, TYPE_LENGTH (value->type));
1535 value->kind = axs_rvalue;
1536 value->type = builtin_type_int;
1540 /* Generating bytecode from GDB expressions: general recursive thingy */
1542 /* A gen_expr function written by a Gen-X'er guy.
1543 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1545 gen_expr (pc, ax, value)
1546 union exp_element **pc;
1547 struct agent_expr *ax;
1548 struct axs_value *value;
1550 /* Used to hold the descriptions of operand expressions. */
1551 struct axs_value value1, value2;
1552 enum exp_opcode op = (*pc)[0].opcode;
1554 /* If we're looking at a constant expression, just push its value. */
1556 struct value *v = maybe_const_expr (pc);
1560 ax_const_l (ax, value_as_long (v));
1561 value->kind = axs_rvalue;
1562 value->type = check_typedef (VALUE_TYPE (v));
1567 /* Otherwise, go ahead and generate code for it. */
1570 /* Binary arithmetic operators. */
1576 case BINOP_SUBSCRIPT:
1577 case BINOP_BITWISE_AND:
1578 case BINOP_BITWISE_IOR:
1579 case BINOP_BITWISE_XOR:
1581 gen_expr (pc, ax, &value1);
1582 gen_usual_unary (ax, &value1);
1583 gen_expr (pc, ax, &value2);
1584 gen_usual_unary (ax, &value2);
1585 gen_usual_arithmetic (ax, &value1, &value2);
1589 gen_add (ax, value, &value1, &value2, "addition");
1592 gen_sub (ax, value, &value1, &value2);
1595 gen_binop (ax, value, &value1, &value2,
1596 aop_mul, aop_mul, 1, "multiplication");
1599 gen_binop (ax, value, &value1, &value2,
1600 aop_div_signed, aop_div_unsigned, 1, "division");
1603 gen_binop (ax, value, &value1, &value2,
1604 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1606 case BINOP_SUBSCRIPT:
1607 gen_add (ax, value, &value1, &value2, "array subscripting");
1608 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1609 error ("Illegal combination of types in array subscripting.");
1610 gen_deref (ax, value);
1612 case BINOP_BITWISE_AND:
1613 gen_binop (ax, value, &value1, &value2,
1614 aop_bit_and, aop_bit_and, 0, "bitwise and");
1617 case BINOP_BITWISE_IOR:
1618 gen_binop (ax, value, &value1, &value2,
1619 aop_bit_or, aop_bit_or, 0, "bitwise or");
1622 case BINOP_BITWISE_XOR:
1623 gen_binop (ax, value, &value1, &value2,
1624 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1628 /* We should only list operators in the outer case statement
1629 that we actually handle in the inner case statement. */
1630 internal_error ("ax-gdb.c (gen_expr): op case sets don't match");
1634 /* Note that we need to be a little subtle about generating code
1635 for comma. In C, we can do some optimizations here because
1636 we know the left operand is only being evaluated for effect.
1637 However, if the tracing kludge is in effect, then we always
1638 need to evaluate the left hand side fully, so that all the
1639 variables it mentions get traced. */
1642 gen_expr (pc, ax, &value1);
1643 /* Don't just dispose of the left operand. We might be tracing,
1644 in which case we want to emit code to trace it if it's an
1646 gen_traced_pop (ax, &value1);
1647 gen_expr (pc, ax, value);
1648 /* It's the consumer's responsibility to trace the right operand. */
1651 case OP_LONG: /* some integer constant */
1653 struct type *type = (*pc)[1].type;
1654 LONGEST k = (*pc)[2].longconst;
1656 gen_int_literal (ax, value, k, type);
1661 gen_var_ref (ax, value, (*pc)[2].symbol);
1667 int reg = (int) (*pc)[1].longconst;
1669 value->kind = axs_lvalue_register;
1671 value->type = REGISTER_VIRTUAL_TYPE (reg);
1675 case OP_INTERNALVAR:
1676 error ("GDB agent expressions cannot use convenience variables.");
1678 /* Weirdo operator: see comments for gen_repeat for details. */
1680 /* Note that gen_repeat handles its own argument evaluation. */
1682 gen_repeat (pc, ax, value);
1687 struct type *type = (*pc)[1].type;
1689 gen_expr (pc, ax, value);
1690 gen_cast (ax, value, type);
1696 struct type *type = check_typedef ((*pc)[1].type);
1698 gen_expr (pc, ax, value);
1699 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1700 it's just a hack for dealing with minsyms; you take some
1701 integer constant, pretend it's the address of an lvalue of
1702 the given type, and dereference it. */
1703 if (value->kind != axs_rvalue)
1704 /* This would be weird. */
1705 internal_error ("ax-gdb.c (gen_expr): OP_MEMVAL operand isn't an rvalue???");
1707 value->kind = axs_lvalue_memory;
1713 /* -FOO is equivalent to 0 - FOO. */
1714 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
1715 gen_usual_unary (ax, &value1); /* shouldn't do much */
1716 gen_expr (pc, ax, &value2);
1717 gen_usual_unary (ax, &value2);
1718 gen_usual_arithmetic (ax, &value1, &value2);
1719 gen_sub (ax, value, &value1, &value2);
1722 case UNOP_LOGICAL_NOT:
1724 gen_expr (pc, ax, value);
1725 gen_logical_not (ax, value);
1728 case UNOP_COMPLEMENT:
1730 gen_expr (pc, ax, value);
1731 gen_complement (ax, value);
1736 gen_expr (pc, ax, value);
1737 gen_usual_unary (ax, value);
1738 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1739 error ("Argument of unary `*' is not a pointer.");
1740 gen_deref (ax, value);
1745 gen_expr (pc, ax, value);
1746 gen_address_of (ax, value);
1751 /* Notice that gen_sizeof handles its own operand, unlike most
1752 of the other unary operator functions. This is because we
1753 have to throw away the code we generate. */
1754 gen_sizeof (pc, ax, value);
1757 case STRUCTOP_STRUCT:
1760 int length = (*pc)[1].longconst;
1761 char *name = &(*pc)[2].string;
1763 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1764 gen_expr (pc, ax, value);
1765 if (op == STRUCTOP_STRUCT)
1766 gen_struct_ref (ax, value, name, ".", "structure or union");
1767 else if (op == STRUCTOP_PTR)
1768 gen_struct_ref (ax, value, name, "->",
1769 "pointer to a structure or union");
1771 /* If this `if' chain doesn't handle it, then the case list
1772 shouldn't mention it, and we shouldn't be here. */
1773 internal_error ("ax-gdb.c (gen_expr): unhandled struct case");
1778 error ("Attempt to use a type name as an expression.");
1781 error ("Unsupported operator in expression.");
1787 /* Generating bytecode from GDB expressions: driver */
1789 /* Given a GDB expression EXPR, produce a string of agent bytecode
1790 which computes its value. Return the agent expression, and set
1791 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1793 expr_to_agent (expr, value)
1794 struct expression *expr;
1795 struct axs_value *value;
1797 struct cleanup *old_chain = 0;
1798 struct agent_expr *ax = new_agent_expr (0);
1799 union exp_element *pc;
1801 old_chain = make_cleanup ((make_cleanup_func) free_agent_expr, ax);
1805 gen_expr (&pc, ax, value);
1807 /* We have successfully built the agent expr, so cancel the cleanup
1808 request. If we add more cleanups that we always want done, this
1809 will have to get more complicated. */
1810 discard_cleanups (old_chain);
1815 #if 0 /* not used */
1816 /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1817 string of agent bytecode which will leave its address and size on
1818 the top of stack. Return the agent expression.
1820 Not sure this function is useful at all. */
1822 expr_to_address_and_size (expr)
1823 struct expression *expr;
1825 struct axs_value value;
1826 struct agent_expr *ax = expr_to_agent (expr, &value);
1828 /* Complain if the result is not a memory lvalue. */
1829 if (value.kind != axs_lvalue_memory)
1831 free_agent_expr (ax);
1832 error ("Expression does not denote an object in memory.");
1835 /* Push the object's size on the stack. */
1836 ax_const_l (ax, TYPE_LENGTH (value.type));
1842 /* Given a GDB expression EXPR, return bytecode to trace its value.
1843 The result will use the `trace' and `trace_quick' bytecodes to
1844 record the value of all memory touched by the expression. The
1845 caller can then use the ax_reqs function to discover which
1846 registers it relies upon. */
1848 gen_trace_for_expr (scope, expr)
1850 struct expression *expr;
1852 struct cleanup *old_chain = 0;
1853 struct agent_expr *ax = new_agent_expr (scope);
1854 union exp_element *pc;
1855 struct axs_value value;
1857 old_chain = make_cleanup ((make_cleanup_func) free_agent_expr, ax);
1861 gen_expr (&pc, ax, &value);
1863 /* Make sure we record the final object, and get rid of it. */
1864 gen_traced_pop (ax, &value);
1866 /* Oh, and terminate. */
1867 ax_simple (ax, aop_end);
1869 /* We have successfully built the agent expr, so cancel the cleanup
1870 request. If we add more cleanups that we always want done, this
1871 will have to get more complicated. */
1872 discard_cleanups (old_chain);
1878 /* The "agent" command, for testing: compile and disassemble an expression. */
1881 print_axs_value (f, value)
1883 struct axs_value *value;
1885 switch (value->kind)
1888 fputs_filtered ("rvalue", f);
1891 case axs_lvalue_memory:
1892 fputs_filtered ("memory lvalue", f);
1895 case axs_lvalue_register:
1896 fprintf_filtered (f, "register %d lvalue", value->u.reg);
1900 fputs_filtered (" : ", f);
1901 type_print (value->type, "", f, -1);
1906 agent_command (exp, from_tty)
1910 struct cleanup *old_chain = 0;
1911 struct expression *expr;
1912 struct agent_expr *agent;
1913 struct frame_info *fi = get_current_frame (); /* need current scope */
1915 /* We don't deal with overlay debugging at the moment. We need to
1916 think more carefully about this. If you copy this code into
1917 another command, change the error message; the user shouldn't
1918 have to know anything about agent expressions. */
1919 if (overlay_debugging)
1920 error ("GDB can't do agent expression translation with overlays.");
1923 error_no_arg ("expression to translate");
1925 expr = parse_expression (exp);
1926 old_chain = make_cleanup (free_current_contents, &expr);
1927 agent = gen_trace_for_expr (fi->pc, expr);
1928 make_cleanup ((make_cleanup_func) free_agent_expr, agent);
1929 ax_print (gdb_stdout, agent);
1931 /* It would be nice to call ax_reqs here to gather some general info
1932 about the expression, and then print out the result. */
1934 do_cleanups (old_chain);
1939 /* Initialization code. */
1941 void _initialize_ax_gdb PARAMS ((void));
1943 _initialize_ax_gdb ()
1945 add_cmd ("agent", class_maintenance, agent_command,
1946 "Translate an expression into remote agent bytecode.",