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. */
25 #include "expression.h"
33 /* Probably the best way to read this file is to start with the types
34 and enums in ax-gdb.h, and then look at gen_expr, towards the
35 bottom; that's the main function that looks at the GDB expressions
36 and calls everything else to generate code.
38 I'm beginning to wonder whether it wouldn't be nicer to internally
39 generate trees, with types, and then spit out the bytecode in
40 linear form afterwards; we could generate fewer `swap', `ext', and
41 `zero_ext' bytecodes that way; it would make good constant folding
42 easier, too. But at the moment, I think we should be willing to
43 pay for the simplicity of this code with less-than-optimal bytecode
46 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
50 /* Prototypes for local functions. */
52 /* There's a standard order to the arguments of these functions:
53 union exp_element ** --- pointer into expression
54 struct agent_expr * --- agent expression buffer to generate code into
55 struct axs_value * --- describes value left on top of stack */
57 static struct value *const_var_ref PARAMS ((struct symbol *var));
58 static struct value *const_expr PARAMS ((union exp_element **pc));
59 static struct value *maybe_const_expr PARAMS ((union exp_element **pc));
61 static void gen_traced_pop PARAMS ((struct agent_expr *, struct axs_value *));
63 static void gen_sign_extend PARAMS ((struct agent_expr *, struct type *));
64 static void gen_extend PARAMS ((struct agent_expr *, struct type *));
65 static void gen_fetch PARAMS ((struct agent_expr *, struct type *));
66 static void gen_left_shift PARAMS ((struct agent_expr *, int));
69 static void gen_frame_args_address PARAMS ((struct agent_expr *));
70 static void gen_frame_locals_address PARAMS ((struct agent_expr *));
71 static void gen_offset PARAMS ((struct agent_expr *ax, int offset));
72 static void gen_sym_offset PARAMS ((struct agent_expr *, struct symbol *));
73 static void gen_var_ref PARAMS ((struct agent_expr *ax,
74 struct axs_value *value,
78 static void gen_int_literal PARAMS ((struct agent_expr *ax,
79 struct axs_value *value,
80 LONGEST k, struct type *type));
83 static void require_rvalue PARAMS ((struct agent_expr *ax,
84 struct axs_value *value));
85 static void gen_usual_unary PARAMS ((struct agent_expr *ax,
86 struct axs_value *value));
87 static int type_wider_than PARAMS ((struct type *type1,
89 static struct type *max_type PARAMS ((struct type *type1,
91 static void gen_conversion PARAMS ((struct agent_expr *ax,
94 static int is_nontrivial_conversion PARAMS ((struct type *from,
96 static void gen_usual_arithmetic PARAMS ((struct agent_expr *ax,
97 struct axs_value *value1,
98 struct axs_value *value2));
99 static void gen_integral_promotions PARAMS ((struct agent_expr *ax,
100 struct axs_value *value));
101 static void gen_cast PARAMS ((struct agent_expr *ax,
102 struct axs_value *value,
104 static void gen_scale PARAMS ((struct agent_expr *ax,
107 static void gen_add PARAMS ((struct agent_expr *ax,
108 struct axs_value *value,
109 struct axs_value *value1,
110 struct axs_value *value2,
112 static void gen_sub PARAMS ((struct agent_expr *ax,
113 struct axs_value *value,
114 struct axs_value *value1,
115 struct axs_value *value2));
116 static void gen_binop PARAMS ((struct agent_expr *ax,
117 struct axs_value *value,
118 struct axs_value *value1,
119 struct axs_value *value2,
121 enum agent_op op_unsigned,
124 static void gen_logical_not PARAMS ((struct agent_expr *ax,
125 struct axs_value *value));
126 static void gen_complement PARAMS ((struct agent_expr *ax,
127 struct axs_value *value));
128 static void gen_deref PARAMS ((struct agent_expr *, struct axs_value *));
129 static void gen_address_of PARAMS ((struct agent_expr *, struct axs_value *));
130 static int find_field PARAMS ((struct type *type, char *name));
131 static void gen_bitfield_ref PARAMS ((struct agent_expr *ax,
132 struct axs_value *value,
134 int start, int end));
135 static void gen_struct_ref PARAMS ((struct agent_expr *ax,
136 struct axs_value *value,
139 char *operand_name));
140 static void gen_repeat PARAMS ((union exp_element **pc,
141 struct agent_expr *ax,
142 struct axs_value *value));
143 static void gen_sizeof PARAMS ((union exp_element **pc,
144 struct agent_expr *ax,
145 struct axs_value *value));
146 static void gen_expr PARAMS ((union exp_element **pc,
147 struct agent_expr *ax,
148 struct axs_value *value));
150 static void print_axs_value PARAMS ((GDB_FILE *f, struct axs_value *value));
151 static void agent_command PARAMS ((char *exp, int from_tty));
154 /* Detecting constant expressions. */
156 /* If the variable reference at *PC is a constant, return its value.
157 Otherwise, return zero.
159 Hey, Wally! How can a variable reference be a constant?
161 Well, Beav, this function really handles the OP_VAR_VALUE operator,
162 not specifically variable references. GDB uses OP_VAR_VALUE to
163 refer to any kind of symbolic reference: function names, enum
164 elements, and goto labels are all handled through the OP_VAR_VALUE
165 operator, even though they're constants. It makes sense given the
168 Gee, Wally, don'cha wonder sometimes if data representations that
169 subvert commonly accepted definitions of terms in favor of heavily
170 context-specific interpretations are really just a tool of the
171 programming hegemony to preserve their power and exclude the
174 static struct value *
178 struct type *type = SYMBOL_TYPE (var);
180 switch (SYMBOL_CLASS (var))
183 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
186 return value_from_longest (type, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
194 /* If the expression starting at *PC has a constant value, return it.
195 Otherwise, return zero. If we return a value, then *PC will be
196 advanced to the end of it. If we return zero, *PC could be
198 static struct value *
200 union exp_element **pc;
202 enum exp_opcode op = (*pc)->opcode;
209 struct type *type = (*pc)[1].type;
210 LONGEST k = (*pc)[2].longconst;
212 return value_from_longest (type, k);
217 struct value *v = const_var_ref ((*pc)[2].symbol);
222 /* We could add more operators in here. */
226 v1 = const_expr (pc);
228 return value_neg (v1);
238 /* Like const_expr, but guarantee also that *PC is undisturbed if the
239 expression is not constant. */
240 static struct value *
241 maybe_const_expr (pc)
242 union exp_element **pc;
244 union exp_element *tentative_pc = *pc;
245 struct value *v = const_expr (&tentative_pc);
247 /* If we got a value, then update the real PC. */
255 /* Generating bytecode from GDB expressions: general assumptions */
257 /* Here are a few general assumptions made throughout the code; if you
258 want to make a change that contradicts one of these, then you'd
259 better scan things pretty thoroughly.
261 - We assume that all values occupy one stack element. For example,
262 sometimes we'll swap to get at the left argument to a binary
263 operator. If we decide that void values should occupy no stack
264 elements, or that synthetic arrays (whose size is determined at
265 run time, created by the `@' operator) should occupy two stack
266 elements (address and length), then this will cause trouble.
268 - We assume the stack elements are infinitely wide, and that we
269 don't have to worry what happens if the user requests an
270 operation that is wider than the actual interpreter's stack.
271 That is, it's up to the interpreter to handle directly all the
272 integer widths the user has access to. (Woe betide the language
275 - We don't support side effects. Thus, we don't have to worry about
276 GCC's generalized lvalues, function calls, etc.
278 - We don't support floating point. Many places where we switch on
279 some type don't bother to include cases for floating point; there
280 may be even more subtle ways this assumption exists. For
281 example, the arguments to % must be integers.
283 - We assume all subexpressions have a static, unchanging type. If
284 we tried to support convenience variables, this would be a
287 - All values on the stack should always be fully zero- or
290 (I wasn't sure whether to choose this or its opposite --- that
291 only addresses are assumed extended --- but it turns out that
292 neither convention completely eliminates spurious extend
293 operations (if everything is always extended, then you have to
294 extend after add, because it could overflow; if nothing is
295 extended, then you end up producing extends whenever you change
296 sizes), and this is simpler.) */
299 /* Generating bytecode from GDB expressions: the `trace' kludge */
301 /* The compiler in this file is a general-purpose mechanism for
302 translating GDB expressions into bytecode. One ought to be able to
303 find a million and one uses for it.
305 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
306 of expediency. Let he who is without sin cast the first stone.
308 For the data tracing facility, we need to insert `trace' bytecodes
309 before each data fetch; this records all the memory that the
310 expression touches in the course of evaluation, so that memory will
311 be available when the user later tries to evaluate the expression
314 This should be done (I think) in a post-processing pass, that walks
315 an arbitrary agent expression and inserts `trace' operations at the
316 appropriate points. But it's much faster to just hack them
317 directly into the code. And since we're in a crunch, that's what
320 Setting the flag trace_kludge to non-zero enables the code that
321 emits the trace bytecodes at the appropriate points. */
322 static int trace_kludge;
324 /* Trace the lvalue on the stack, if it needs it. In either case, pop
325 the value. Useful on the left side of a comma, and at the end of
326 an expression being used for tracing. */
328 gen_traced_pop (ax, value)
329 struct agent_expr *ax;
330 struct axs_value *value;
336 /* We don't trace rvalues, just the lvalues necessary to
337 produce them. So just dispose of this value. */
338 ax_simple (ax, aop_pop);
341 case axs_lvalue_memory:
343 int length = TYPE_LENGTH (value->type);
345 /* There's no point in trying to use a trace_quick bytecode
346 here, since "trace_quick SIZE pop" is three bytes, whereas
347 "const8 SIZE trace" is also three bytes, does the same
348 thing, and the simplest code which generates that will also
349 work correctly for objects with large sizes. */
350 ax_const_l (ax, length);
351 ax_simple (ax, aop_trace);
355 case axs_lvalue_register:
356 /* We need to mention the register somewhere in the bytecode,
357 so ax_reqs will pick it up and add it to the mask of
359 ax_reg (ax, value->u.reg);
360 ax_simple (ax, aop_pop);
364 /* If we're not tracing, just pop the value. */
365 ax_simple (ax, aop_pop);
370 /* Generating bytecode from GDB expressions: helper functions */
372 /* Assume that the lower bits of the top of the stack is a value of
373 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
375 gen_sign_extend (ax, type)
376 struct agent_expr *ax;
379 /* Do we need to sign-extend this? */
380 if (! TYPE_UNSIGNED (type))
381 ax_ext (ax, type->length * TARGET_CHAR_BIT);
385 /* Assume the lower bits of the top of the stack hold a value of type
386 TYPE, and the upper bits are garbage. Sign-extend or truncate as
389 gen_extend (ax, type)
390 struct agent_expr *ax;
393 int bits = type->length * TARGET_CHAR_BIT;
395 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
399 /* Assume that the top of the stack contains a value of type "pointer
400 to TYPE"; generate code to fetch its value. Note that TYPE is the
401 target type, not the pointer type. */
404 struct agent_expr *ax;
409 /* Record the area of memory we're about to fetch. */
410 ax_trace_quick (ax, TYPE_LENGTH (type));
419 /* It's a scalar value, so we know how to dereference it. How
420 many bytes long is it? */
421 switch (type->length)
423 case 8 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref8 ); break;
424 case 16 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref16); break;
425 case 32 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref32); break;
426 case 64 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref64); break;
428 /* Either our caller shouldn't have asked us to dereference
429 that pointer (other code's fault), or we're not
430 implementing something we should be (this code's fault).
431 In any case, it's a bug the user shouldn't see. */
433 error ("GDB bug: ax-gdb.c (gen_fetch): strange size");
436 gen_sign_extend (ax, type);
440 /* Either our caller shouldn't have asked us to dereference that
441 pointer (other code's fault), or we're not implementing
442 something we should be (this code's fault). In any case,
443 it's a bug the user shouldn't see. */
444 error ("GDB bug: ax-gdb.c (gen_fetch): bad type code");
449 /* Generate code to left shift the top of the stack by DISTANCE bits, or
450 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
451 unsigned (logical) right shifts. */
453 gen_left_shift (ax, distance)
454 struct agent_expr *ax;
459 ax_const_l (ax, distance);
460 ax_simple (ax, aop_lsh);
462 else if (distance < 0)
464 ax_const_l (ax, -distance);
465 ax_simple (ax, aop_rsh_unsigned);
471 /* Generating bytecode from GDB expressions: symbol references */
473 /* Generate code to push the base address of the argument portion of
474 the top stack frame. */
476 gen_frame_args_address (ax)
477 struct agent_expr *ax;
479 long frame_reg, frame_offset;
481 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
482 ax_reg (ax, frame_reg);
483 gen_offset (ax, frame_offset);
487 /* Generate code to push the base address of the locals portion of the
490 gen_frame_locals_address (ax)
491 struct agent_expr *ax;
493 long frame_reg, frame_offset;
495 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
496 ax_reg (ax, frame_reg);
497 gen_offset (ax, frame_offset);
501 /* Generate code to add OFFSET to the top of the stack. Try to
502 generate short and readable code. We use this for getting to
503 variables on the stack, and structure members. If we were
504 programming in ML, it would be clearer why these are the same
507 gen_offset (ax, offset)
508 struct agent_expr *ax;
511 /* It would suffice to simply push the offset and add it, but this
512 makes it easier to read positive and negative offsets in the
516 ax_const_l (ax, offset);
517 ax_simple (ax, aop_add);
521 ax_const_l (ax, -offset);
522 ax_simple (ax, aop_sub);
527 /* In many cases, a symbol's value is the offset from some other
528 address (stack frame, base register, etc.) Generate code to add
529 VAR's value to the top of the stack. */
531 gen_sym_offset (ax, var)
532 struct agent_expr *ax;
535 gen_offset (ax, SYMBOL_VALUE (var));
539 /* Generate code for a variable reference to AX. The variable is the
540 symbol VAR. Set VALUE to describe the result. */
543 gen_var_ref (ax, value, var)
544 struct agent_expr *ax;
545 struct axs_value *value;
548 /* Dereference any typedefs. */
549 value->type = check_typedef (SYMBOL_TYPE (var));
551 /* I'm imitating the code in read_var_value. */
552 switch (SYMBOL_CLASS (var))
554 case LOC_CONST: /* A constant, like an enum value. */
555 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
556 value->kind = axs_rvalue;
559 case LOC_LABEL: /* A goto label, being used as a value. */
560 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
561 value->kind = axs_rvalue;
564 case LOC_CONST_BYTES:
565 error ("GDB bug: ax-gdb.c (gen_var_ref): LOC_CONST_BYTES symbols are not supported");
567 /* Variable at a fixed location in memory. Easy. */
569 /* Push the address of the variable. */
570 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
571 value->kind = axs_lvalue_memory;
574 case LOC_ARG: /* var lives in argument area of frame */
575 gen_frame_args_address (ax);
576 gen_sym_offset (ax, var);
577 value->kind = axs_lvalue_memory;
580 case LOC_REF_ARG: /* As above, but the frame slot really
581 holds the address of the variable. */
582 gen_frame_args_address (ax);
583 gen_sym_offset (ax, var);
584 /* Don't assume any particular pointer size. */
585 gen_fetch (ax, lookup_pointer_type (builtin_type_void));
586 value->kind = axs_lvalue_memory;
589 case LOC_LOCAL: /* var lives in locals area of frame */
591 gen_frame_locals_address (ax);
592 gen_sym_offset (ax, var);
593 value->kind = axs_lvalue_memory;
596 case LOC_BASEREG: /* relative to some base register */
597 case LOC_BASEREG_ARG:
598 ax_reg (ax, SYMBOL_BASEREG (var));
599 gen_sym_offset (ax, var);
600 value->kind = axs_lvalue_memory;
604 error ("Cannot compute value of typedef `%s'.",
605 SYMBOL_SOURCE_NAME (var));
609 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
610 value->kind = axs_rvalue;
615 /* Don't generate any code at all; in the process of treating
616 this as an lvalue or rvalue, the caller will generate the
618 value->kind = axs_lvalue_register;
619 value->u.reg = SYMBOL_VALUE (var);
622 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
623 register, not on the stack. Simpler than LOC_REGISTER and
624 LOC_REGPARM, because it's just like any other case where the
625 thing has a real address. */
626 case LOC_REGPARM_ADDR:
627 ax_reg (ax, SYMBOL_VALUE (var));
628 value->kind = axs_lvalue_memory;
633 struct minimal_symbol *msym
634 = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
636 error ("Couldn't resolve symbol `%s'.", SYMBOL_SOURCE_NAME (var));
638 /* Push the address of the variable. */
639 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
640 value->kind = axs_lvalue_memory;
644 case LOC_OPTIMIZED_OUT:
645 error ("The variable `%s' has been optimized out.",
646 SYMBOL_SOURCE_NAME (var));
650 error ("Cannot find value of botched symbol `%s'.",
651 SYMBOL_SOURCE_NAME (var));
658 /* Generating bytecode from GDB expressions: literals */
661 gen_int_literal (ax, value, k, type)
662 struct agent_expr *ax;
663 struct axs_value *value;
668 value->kind = axs_rvalue;
674 /* Generating bytecode from GDB expressions: unary conversions, casts */
676 /* Take what's on the top of the stack (as described by VALUE), and
677 try to make an rvalue out of it. Signal an error if we can't do
680 require_rvalue (ax, value)
681 struct agent_expr *ax;
682 struct axs_value *value;
687 /* It's already an rvalue. */
690 case axs_lvalue_memory:
691 /* The top of stack is the address of the object. Dereference. */
692 gen_fetch (ax, value->type);
695 case axs_lvalue_register:
696 /* There's nothing on the stack, but value->u.reg is the
697 register number containing the value.
699 When we add floating-point support, this is going to have to
700 change. What about SPARC register pairs, for example? */
701 ax_reg (ax, value->u.reg);
702 gen_extend (ax, value->type);
706 value->kind = axs_rvalue;
710 /* Assume the top of the stack is described by VALUE, and perform the
711 usual unary conversions. This is motivated by ANSI 6.2.2, but of
712 course GDB expressions are not ANSI; they're the mishmash union of
713 a bunch of languages. Rah.
715 NOTE! This function promises to produce an rvalue only when the
716 incoming value is of an appropriate type. In other words, the
717 consumer of the value this function produces may assume the value
718 is an rvalue only after checking its type.
720 The immediate issue is that if the user tries to use a structure or
721 union as an operand of, say, the `+' operator, we don't want to try
722 to convert that structure to an rvalue; require_rvalue will bomb on
723 structs and unions. Rather, we want to simply pass the struct
724 lvalue through unchanged, and let `+' raise an error. */
727 gen_usual_unary (ax, value)
728 struct agent_expr *ax;
729 struct axs_value *value;
731 /* We don't have to generate any code for the usual integral
732 conversions, since values are always represented as full-width on
733 the stack. Should we tweak the type? */
735 /* Some types require special handling. */
736 switch (value->type->code)
738 /* Functions get converted to a pointer to the function. */
740 value->type = lookup_pointer_type (value->type);
741 value->kind = axs_rvalue; /* Should always be true, but just in case. */
744 /* Arrays get converted to a pointer to their first element, and
745 are no longer an lvalue. */
746 case TYPE_CODE_ARRAY:
748 struct type *elements = TYPE_TARGET_TYPE (value->type);
749 value->type = lookup_pointer_type (elements);
750 value->kind = axs_rvalue;
751 /* We don't need to generate any code; the address of the array
752 is also the address of its first element. */
756 /* Don't try to convert structures and unions to rvalues. Let the
757 consumer signal an error. */
758 case TYPE_CODE_STRUCT:
759 case TYPE_CODE_UNION:
762 /* If the value is an enum, call it an integer. */
764 value->type = builtin_type_int;
768 /* If the value is an lvalue, dereference it. */
769 require_rvalue (ax, value);
773 /* Return non-zero iff the type TYPE1 is considered "wider" than the
774 type TYPE2, according to the rules described in gen_usual_arithmetic. */
776 type_wider_than (type1, type2)
777 struct type *type1, *type2;
779 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
780 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
781 && TYPE_UNSIGNED (type1)
782 && ! TYPE_UNSIGNED (type2)));
786 /* Return the "wider" of the two types TYPE1 and TYPE2. */
788 max_type (type1, type2)
789 struct type *type1, *type2;
791 return type_wider_than (type1, type2) ? type1 : type2;
795 /* Generate code to convert a scalar value of type FROM to type TO. */
797 gen_conversion (ax, from, to)
798 struct agent_expr *ax;
799 struct type *from, *to;
801 /* Perhaps there is a more graceful way to state these rules. */
803 /* If we're converting to a narrower type, then we need to clear out
805 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
806 gen_extend (ax, from);
808 /* If the two values have equal width, but different signednesses,
809 then we need to extend. */
810 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
812 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
816 /* If we're converting to a wider type, and becoming unsigned, then
817 we need to zero out any possible sign bits. */
818 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
820 if (TYPE_UNSIGNED (to))
826 /* Return non-zero iff the type FROM will require any bytecodes to be
827 emitted to be converted to the type TO. */
829 is_nontrivial_conversion (from, to)
830 struct type *from, *to;
832 struct agent_expr *ax = new_agent_expr (0);
835 /* Actually generate the code, and see if anything came out. At the
836 moment, it would be trivial to replicate the code in
837 gen_conversion here, but in the future, when we're supporting
838 floating point and the like, it may not be. Doing things this
839 way allows this function to be independent of the logic in
841 gen_conversion (ax, from, to);
842 nontrivial = ax->len > 0;
843 free_agent_expr (ax);
848 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
849 6.2.1.5) for the two operands of an arithmetic operator. This
850 effectively finds a "least upper bound" type for the two arguments,
851 and promotes each argument to that type. *VALUE1 and *VALUE2
852 describe the values as they are passed in, and as they are left. */
854 gen_usual_arithmetic (ax, value1, value2)
855 struct agent_expr *ax;
856 struct axs_value *value1, *value2;
858 /* Do the usual binary conversions. */
859 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
860 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
862 /* The ANSI integral promotions seem to work this way: Order the
863 integer types by size, and then by signedness: an n-bit
864 unsigned type is considered "wider" than an n-bit signed
865 type. Promote to the "wider" of the two types, and always
866 promote at least to int. */
867 struct type *target = max_type (builtin_type_int,
868 max_type (value1->type, value2->type));
870 /* Deal with value2, on the top of the stack. */
871 gen_conversion (ax, value2->type, target);
873 /* Deal with value1, not on the top of the stack. Don't
874 generate the `swap' instructions if we're not actually going
876 if (is_nontrivial_conversion (value1->type, target))
878 ax_simple (ax, aop_swap);
879 gen_conversion (ax, value1->type, target);
880 ax_simple (ax, aop_swap);
883 value1->type = value2->type = target;
888 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
889 the value on the top of the stack, as described by VALUE. Assume
890 the value has integral type. */
892 gen_integral_promotions (ax, value)
893 struct agent_expr *ax;
894 struct axs_value *value;
896 if (! type_wider_than (value->type, builtin_type_int))
898 gen_conversion (ax, value->type, builtin_type_int);
899 value->type = builtin_type_int;
901 else if (! type_wider_than (value->type, builtin_type_unsigned_int))
903 gen_conversion (ax, value->type, builtin_type_unsigned_int);
904 value->type = builtin_type_unsigned_int;
909 /* Generate code for a cast to TYPE. */
911 gen_cast (ax, value, type)
912 struct agent_expr *ax;
913 struct axs_value *value;
916 /* GCC does allow casts to yield lvalues, so this should be fixed
917 before merging these changes into the trunk. */
918 require_rvalue (ax, value);
919 /* Dereference typedefs. */
920 type = check_typedef (type);
925 /* It's implementation-defined, and I'll bet this is what GCC
929 case TYPE_CODE_ARRAY:
930 case TYPE_CODE_STRUCT:
931 case TYPE_CODE_UNION:
933 error ("Illegal type cast: intended type must be scalar.");
936 /* We don't have to worry about the size of the value, because
937 all our integral values are fully sign-extended, and when
938 casting pointers we can do anything we like. Is there any
939 way for us to actually know what GCC actually does with a
945 gen_conversion (ax, value->type, type);
949 /* We could pop the value, and rely on everyone else to check
950 the type and notice that this value doesn't occupy a stack
951 slot. But for now, leave the value on the stack, and
952 preserve the "value == stack element" assumption. */
956 error ("Casts to requested type are not yet implemented.");
964 /* Generating bytecode from GDB expressions: arithmetic */
966 /* Scale the integer on the top of the stack by the size of the target
967 of the pointer type TYPE. */
969 gen_scale (ax, op, type)
970 struct agent_expr *ax;
974 struct type *element = TYPE_TARGET_TYPE (type);
976 if (element->length != 1)
978 ax_const_l (ax, element->length);
984 /* Generate code for an addition; non-trivial because we deal with
985 pointer arithmetic. We set VALUE to describe the result value; we
986 assume VALUE1 and VALUE2 describe the two operands, and that
987 they've undergone the usual binary conversions. Used by both
988 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
990 gen_add (ax, value, value1, value2, name)
991 struct agent_expr *ax;
992 struct axs_value *value, *value1, *value2;
996 if (value1->type->code == TYPE_CODE_INT
997 && value2->type->code == TYPE_CODE_PTR)
999 /* Swap the values and proceed normally. */
1000 ax_simple (ax, aop_swap);
1001 gen_scale (ax, aop_mul, value2->type);
1002 ax_simple (ax, aop_add);
1003 gen_extend (ax, value2->type); /* Catch overflow. */
1004 value->type = value2->type;
1007 /* Is it PTR+INT? */
1008 else if (value1->type->code == TYPE_CODE_PTR
1009 && value2->type->code == TYPE_CODE_INT)
1011 gen_scale (ax, aop_mul, value1->type);
1012 ax_simple (ax, aop_add);
1013 gen_extend (ax, value1->type); /* Catch overflow. */
1014 value->type = value1->type;
1017 /* Must be number + number; the usual binary conversions will have
1018 brought them both to the same width. */
1019 else if (value1->type->code == TYPE_CODE_INT
1020 && value2->type->code == TYPE_CODE_INT)
1022 ax_simple (ax, aop_add);
1023 gen_extend (ax, value1->type); /* Catch overflow. */
1024 value->type = value1->type;
1028 error ("Illegal combination of types in %s.", name);
1030 value->kind = axs_rvalue;
1034 /* Generate code for an addition; non-trivial because we have to deal
1035 with pointer arithmetic. We set VALUE to describe the result
1036 value; we assume VALUE1 and VALUE2 describe the two operands, and
1037 that they've undergone the usual binary conversions. */
1039 gen_sub (ax, value, value1, value2)
1040 struct agent_expr *ax;
1041 struct axs_value *value, *value1, *value2;
1043 struct type *element;
1045 if (value1->type->code == TYPE_CODE_PTR)
1047 /* Is it PTR - INT? */
1048 if (value2->type->code == TYPE_CODE_INT)
1050 gen_scale (ax, aop_mul, value1->type);
1051 ax_simple (ax, aop_sub);
1052 gen_extend (ax, value1->type); /* Catch overflow. */
1053 value->type = value1->type;
1056 /* Is it PTR - PTR? Strictly speaking, the types ought to
1057 match, but this is what the normal GDB expression evaluator
1059 else if (value2->type->code == TYPE_CODE_PTR
1060 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1061 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1063 ax_simple (ax, aop_sub);
1064 gen_scale (ax, aop_div_unsigned, value1->type);
1065 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
1069 First argument of `-' is a pointer, but second argument is neither\n\
1070 an integer nor a pointer of the same type.");
1073 /* Must be number + number. */
1074 else if (value1->type->code == TYPE_CODE_INT
1075 && value2->type->code == TYPE_CODE_INT)
1077 ax_simple (ax, aop_sub);
1078 gen_extend (ax, value1->type); /* Catch overflow. */
1079 value->type = value1->type;
1083 error ("Illegal combination of types in subtraction.");
1085 value->kind = axs_rvalue;
1088 /* Generate code for a binary operator that doesn't do pointer magic.
1089 We set VALUE to describe the result value; we assume VALUE1 and
1090 VALUE2 describe the two operands, and that they've undergone the
1091 usual binary conversions. MAY_CARRY should be non-zero iff the
1092 result needs to be extended. NAME is the English name of the
1093 operator, used in error messages */
1095 gen_binop (ax, value, value1, value2, op, op_unsigned, may_carry, name)
1096 struct agent_expr *ax;
1097 struct axs_value *value, *value1, *value2;
1098 enum agent_op op, op_unsigned;
1102 /* We only handle INT op INT. */
1103 if ((value1->type->code != TYPE_CODE_INT)
1104 || (value2->type->code != TYPE_CODE_INT))
1105 error ("Illegal combination of types in %s.", name);
1108 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1110 gen_extend (ax, value1->type); /* catch overflow */
1111 value->type = value1->type;
1112 value->kind = axs_rvalue;
1117 gen_logical_not (ax, value)
1118 struct agent_expr *ax;
1119 struct axs_value *value;
1121 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1122 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1123 error ("Illegal type of operand to `!'.");
1125 gen_usual_unary (ax, value);
1126 ax_simple (ax, aop_log_not);
1127 value->type = builtin_type_int;
1132 gen_complement (ax, value)
1133 struct agent_expr *ax;
1134 struct axs_value *value;
1136 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1137 error ("Illegal type of operand to `~'.");
1139 gen_usual_unary (ax, value);
1140 gen_integral_promotions (ax, value);
1141 ax_simple (ax, aop_bit_not);
1142 gen_extend (ax, value->type);
1147 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1149 /* Dereference the value on the top of the stack. */
1151 gen_deref (ax, value)
1152 struct agent_expr *ax;
1153 struct axs_value *value;
1155 /* The caller should check the type, because several operators use
1156 this, and we don't know what error message to generate. */
1157 if (value->type->code != TYPE_CODE_PTR)
1158 error ("GDB bug: ax-gdb.c (gen_deref): expected a pointer");
1160 /* We've got an rvalue now, which is a pointer. We want to yield an
1161 lvalue, whose address is exactly that pointer. So we don't
1162 actually emit any code; we just change the type from "Pointer to
1163 T" to "T", and mark the value as an lvalue in memory. Leave it
1164 to the consumer to actually dereference it. */
1165 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1166 value->kind = ((value->type->code == TYPE_CODE_FUNC)
1167 ? axs_rvalue : axs_lvalue_memory);
1171 /* Produce the address of the lvalue on the top of the stack. */
1173 gen_address_of (ax, value)
1174 struct agent_expr *ax;
1175 struct axs_value *value;
1177 /* Special case for taking the address of a function. The ANSI
1178 standard describes this as a special case, too, so this
1179 arrangement is not without motivation. */
1180 if (value->type->code == TYPE_CODE_FUNC)
1181 /* The value's already an rvalue on the stack, so we just need to
1183 value->type = lookup_pointer_type (value->type);
1185 switch (value->kind)
1188 error ("Operand of `&' is an rvalue, which has no address.");
1190 case axs_lvalue_register:
1191 error ("Operand of `&' is in a register, and has no address.");
1193 case axs_lvalue_memory:
1194 value->kind = axs_rvalue;
1195 value->type = lookup_pointer_type (value->type);
1201 /* A lot of this stuff will have to change to support C++. But we're
1202 not going to deal with that at the moment. */
1204 /* Find the field in the structure type TYPE named NAME, and return
1205 its index in TYPE's field array. */
1207 find_field (type, name)
1213 CHECK_TYPEDEF (type);
1215 /* Make sure this isn't C++. */
1216 if (TYPE_N_BASECLASSES (type) != 0)
1217 error ("GDB bug: ax-gdb.c (find_field): derived classes supported");
1219 for (i = 0; i < TYPE_NFIELDS (type); i++)
1221 char *this_name = TYPE_FIELD_NAME (type, i);
1223 if (this_name && STREQ (name, this_name))
1226 if (this_name[0] == '\0')
1227 error ("GDB bug: ax-gdb.c (find_field): anonymous unions not supported");
1230 error ("Couldn't find member named `%s' in struct/union `%s'",
1231 name, type->tag_name);
1237 /* Generate code to push the value of a bitfield of a structure whose
1238 address is on the top of the stack. START and END give the
1239 starting and one-past-ending *bit* numbers of the field within the
1242 gen_bitfield_ref (ax, value, type, start, end)
1243 struct agent_expr *ax;
1244 struct axs_value *value;
1248 /* Note that ops[i] fetches 8 << i bits. */
1249 static enum agent_op ops[]
1250 = { aop_ref8, aop_ref16, aop_ref32, aop_ref64 };
1251 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1253 /* We don't want to touch any byte that the bitfield doesn't
1254 actually occupy; we shouldn't make any accesses we're not
1255 explicitly permitted to. We rely here on the fact that the
1256 bytecode `ref' operators work on unaligned addresses.
1258 It takes some fancy footwork to get the stack to work the way
1259 we'd like. Say we're retrieving a bitfield that requires three
1260 fetches. Initially, the stack just contains the address:
1262 For the first fetch, we duplicate the address
1264 then add the byte offset, do the fetch, and shift and mask as
1265 needed, yielding a fragment of the value, properly aligned for
1266 the final bitwise or:
1268 then we swap, and repeat the process:
1269 frag1 addr --- address on top
1270 frag1 addr addr --- duplicate it
1271 frag1 addr frag2 --- get second fragment
1272 frag1 frag2 addr --- swap again
1273 frag1 frag2 frag3 --- get third fragment
1274 Notice that, since the third fragment is the last one, we don't
1275 bother duplicating the address this time. Now we have all the
1276 fragments on the stack, and we can simply `or' them together,
1277 yielding the final value of the bitfield. */
1279 /* The first and one-after-last bits in the field, but rounded down
1280 and up to byte boundaries. */
1281 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1282 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1286 /* current bit offset within the structure */
1289 /* The index in ops of the opcode we're considering. */
1292 /* The number of fragments we generated in the process. Probably
1293 equal to the number of `one' bits in bytesize, but who cares? */
1296 /* Dereference any typedefs. */
1297 type = check_typedef (type);
1299 /* Can we fetch the number of bits requested at all? */
1300 if ((end - start) > ((1 << num_ops) * 8))
1301 error ("GDB bug: ax-gdb.c (gen_bitfield_ref): bitfield too wide");
1303 /* Note that we know here that we only need to try each opcode once.
1304 That may not be true on machines with weird byte sizes. */
1305 offset = bound_start;
1307 for (op = num_ops - 1; op >= 0; op--)
1309 /* number of bits that ops[op] would fetch */
1310 int op_size = 8 << op;
1312 /* The stack at this point, from bottom to top, contains zero or
1313 more fragments, then the address. */
1315 /* Does this fetch fit within the bitfield? */
1316 if (offset + op_size <= bound_end)
1318 /* Is this the last fragment? */
1319 int last_frag = (offset + op_size == bound_end);
1322 ax_simple (ax, aop_dup); /* keep a copy of the address */
1324 /* Add the offset. */
1325 gen_offset (ax, offset / TARGET_CHAR_BIT);
1329 /* Record the area of memory we're about to fetch. */
1330 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1333 /* Perform the fetch. */
1334 ax_simple (ax, ops[op]);
1336 /* Shift the bits we have to their proper position.
1337 gen_left_shift will generate right shifts when the operand
1340 A big-endian field diagram to ponder:
1341 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1342 +------++------++------++------++------++------++------++------+
1343 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1345 bit number 16 32 48 53
1346 These are bit numbers as supplied by GDB. Note that the
1347 bit numbers run from right to left once you've fetched the
1350 A little-endian field diagram to ponder:
1351 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1352 +------++------++------++------++------++------++------++------+
1353 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1355 bit number 48 32 16 4 0
1357 In both cases, the most significant end is on the left
1358 (i.e. normal numeric writing order), which means that you
1359 don't go crazy thinking about `left' and `right' shifts.
1361 We don't have to worry about masking yet:
1362 - If they contain garbage off the least significant end, then we
1363 must be looking at the low end of the field, and the right
1364 shift will wipe them out.
1365 - If they contain garbage off the most significant end, then we
1366 must be looking at the most significant end of the word, and
1367 the sign/zero extension will wipe them out.
1368 - If we're in the interior of the word, then there is no garbage
1369 on either end, because the ref operators zero-extend. */
1370 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1371 gen_left_shift (ax, end - (offset + op_size));
1373 gen_left_shift (ax, offset - start);
1376 /* Bring the copy of the address up to the top. */
1377 ax_simple (ax, aop_swap);
1384 /* Generate enough bitwise `or' operations to combine all the
1385 fragments we left on the stack. */
1386 while (fragment_count-- > 1)
1387 ax_simple (ax, aop_bit_or);
1389 /* Sign- or zero-extend the value as appropriate. */
1390 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1392 /* This is *not* an lvalue. Ugh. */
1393 value->kind = axs_rvalue;
1398 /* Generate code to reference the member named FIELD of a structure or
1399 union. The top of the stack, as described by VALUE, should have
1400 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1401 the operator being compiled, and OPERAND_NAME is the kind of thing
1402 it operates on; we use them in error messages. */
1404 gen_struct_ref (ax, value, field, operator_name, operand_name)
1405 struct agent_expr *ax;
1406 struct axs_value *value;
1408 char *operator_name;
1414 /* Follow pointers until we reach a non-pointer. These aren't the C
1415 semantics, but they're what the normal GDB evaluator does, so we
1416 should at least be consistent. */
1417 while (value->type->code == TYPE_CODE_PTR)
1419 gen_usual_unary (ax, value);
1420 gen_deref (ax, value);
1424 /* This must yield a structure or a union. */
1425 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1426 && TYPE_CODE (type) != TYPE_CODE_UNION)
1427 error ("The left operand of `%s' is not a %s.",
1428 operator_name, operand_name);
1430 /* And it must be in memory; we don't deal with structure rvalues,
1431 or structures living in registers. */
1432 if (value->kind != axs_lvalue_memory)
1433 error ("Structure does not live in memory.");
1435 i = find_field (type, field);
1437 /* Is this a bitfield? */
1438 if (TYPE_FIELD_PACKED (type, i))
1439 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1440 TYPE_FIELD_BITPOS (type, i),
1441 (TYPE_FIELD_BITPOS (type, i)
1442 + TYPE_FIELD_BITSIZE (type, i)));
1445 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1446 value->kind = axs_lvalue_memory;
1447 value->type = TYPE_FIELD_TYPE (type, i);
1452 /* Generate code for GDB's magical `repeat' operator.
1453 LVALUE @ INT creates an array INT elements long, and whose elements
1454 have the same type as LVALUE, located in memory so that LVALUE is
1455 its first element. For example, argv[0]@argc gives you the array
1456 of command-line arguments.
1458 Unfortunately, because we have to know the types before we actually
1459 have a value for the expression, we can't implement this perfectly
1460 without changing the type system, having values that occupy two
1461 stack slots, doing weird things with sizeof, etc. So we require
1462 the right operand to be a constant expression. */
1464 gen_repeat (pc, ax, value)
1465 union exp_element **pc;
1466 struct agent_expr *ax;
1467 struct axs_value *value;
1469 struct axs_value value1;
1470 /* We don't want to turn this into an rvalue, so no conversions
1472 gen_expr (pc, ax, &value1);
1473 if (value1.kind != axs_lvalue_memory)
1474 error ("Left operand of `@' must be an object in memory.");
1476 /* Evaluate the length; it had better be a constant. */
1478 struct value *v = const_expr (pc);
1482 error ("Right operand of `@' must be a constant, in agent expressions.");
1483 if (v->type->code != TYPE_CODE_INT)
1484 error ("Right operand of `@' must be an integer.");
1485 length = value_as_long (v);
1487 error ("Right operand of `@' must be positive.");
1489 /* The top of the stack is already the address of the object, so
1490 all we need to do is frob the type of the lvalue. */
1492 /* FIXME-type-allocation: need a way to free this type when we are
1495 = create_range_type (0, builtin_type_int, 0, length - 1);
1496 struct type *array = create_array_type (0, value1.type, range);
1498 value->kind = axs_lvalue_memory;
1499 value->type = array;
1505 /* Emit code for the `sizeof' operator.
1506 *PC should point at the start of the operand expression; we advance it
1507 to the first instruction after the operand. */
1509 gen_sizeof (pc, ax, value)
1510 union exp_element **pc;
1511 struct agent_expr *ax;
1512 struct axs_value *value;
1514 /* We don't care about the value of the operand expression; we only
1515 care about its type. However, in the current arrangement, the
1516 only way to find an expression's type is to generate code for it.
1517 So we generate code for the operand, and then throw it away,
1518 replacing it with code that simply pushes its size. */
1519 int start = ax->len;
1520 gen_expr (pc, ax, value);
1522 /* Throw away the code we just generated. */
1525 ax_const_l (ax, TYPE_LENGTH (value->type));
1526 value->kind = axs_rvalue;
1527 value->type = builtin_type_int;
1531 /* Generating bytecode from GDB expressions: general recursive thingy */
1533 /* A gen_expr function written by a Gen-X'er guy.
1534 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1536 gen_expr (pc, ax, value)
1537 union exp_element **pc;
1538 struct agent_expr *ax;
1539 struct axs_value *value;
1541 /* Used to hold the descriptions of operand expressions. */
1542 struct axs_value value1, value2;
1543 enum exp_opcode op = (*pc)[0].opcode;
1545 /* If we're looking at a constant expression, just push its value. */
1547 struct value *v = maybe_const_expr (pc);
1551 ax_const_l (ax, value_as_long (v));
1552 value->kind = axs_rvalue;
1553 value->type = check_typedef (VALUE_TYPE (v));
1558 /* Otherwise, go ahead and generate code for it. */
1561 /* Binary arithmetic operators. */
1567 case BINOP_SUBSCRIPT:
1568 case BINOP_BITWISE_AND:
1569 case BINOP_BITWISE_IOR:
1570 case BINOP_BITWISE_XOR:
1572 gen_expr (pc, ax, &value1);
1573 gen_usual_unary (ax, &value1);
1574 gen_expr (pc, ax, &value2);
1575 gen_usual_unary (ax, &value2);
1576 gen_usual_arithmetic (ax, &value1, &value2);
1580 gen_add (ax, value, &value1, &value2, "addition");
1583 gen_sub (ax, value, &value1, &value2);
1586 gen_binop (ax, value, &value1, &value2,
1587 aop_mul, aop_mul, 1, "multiplication");
1590 gen_binop (ax, value, &value1, &value2,
1591 aop_div_signed, aop_div_unsigned, 1, "division");
1594 gen_binop (ax, value, &value1, &value2,
1595 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1597 case BINOP_SUBSCRIPT:
1598 gen_add (ax, value, &value1, &value2, "array subscripting");
1599 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1600 error ("Illegal combination of types in array subscripting.");
1601 gen_deref (ax, value);
1603 case BINOP_BITWISE_AND:
1604 gen_binop (ax, value, &value1, &value2,
1605 aop_bit_and, aop_bit_and, 0, "bitwise and");
1608 case BINOP_BITWISE_IOR:
1609 gen_binop (ax, value, &value1, &value2,
1610 aop_bit_or, aop_bit_or, 0, "bitwise or");
1613 case BINOP_BITWISE_XOR:
1614 gen_binop (ax, value, &value1, &value2,
1615 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1619 /* We should only list operators in the outer case statement
1620 that we actually handle in the inner case statement. */
1621 error ("GDB bug: ax-gdb.c (gen_expr): op case sets don't match");
1625 /* Note that we need to be a little subtle about generating code
1626 for comma. In C, we can do some optimizations here because
1627 we know the left operand is only being evaluated for effect.
1628 However, if the tracing kludge is in effect, then we always
1629 need to evaluate the left hand side fully, so that all the
1630 variables it mentions get traced. */
1633 gen_expr (pc, ax, &value1);
1634 /* Don't just dispose of the left operand. We might be tracing,
1635 in which case we want to emit code to trace it if it's an
1637 gen_traced_pop (ax, &value1);
1638 gen_expr (pc, ax, value);
1639 /* It's the consumer's responsibility to trace the right operand. */
1642 case OP_LONG: /* some integer constant */
1644 struct type *type = (*pc)[1].type;
1645 LONGEST k = (*pc)[2].longconst;
1647 gen_int_literal (ax, value, k, type);
1652 gen_var_ref (ax, value, (*pc)[2].symbol);
1658 int reg = (int) (*pc)[1].longconst;
1660 value->kind = axs_lvalue_register;
1662 value->type = REGISTER_VIRTUAL_TYPE (reg);
1666 case OP_INTERNALVAR:
1667 error ("GDB agent expressions cannot use convenience variables.");
1669 /* Weirdo operator: see comments for gen_repeat for details. */
1671 /* Note that gen_repeat handles its own argument evaluation. */
1673 gen_repeat (pc, ax, value);
1678 struct type *type = (*pc)[1].type;
1680 gen_expr (pc, ax, value);
1681 gen_cast (ax, value, type);
1687 struct type *type = check_typedef ((*pc)[1].type);
1689 gen_expr (pc, ax, value);
1690 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1691 it's just a hack for dealing with minsyms; you take some
1692 integer constant, pretend it's the address of an lvalue of
1693 the given type, and dereference it. */
1694 if (value->kind != axs_rvalue)
1695 /* This would be weird. */
1696 error ("GDB bug: ax-gdb.c (gen_expr): OP_MEMVAL operand isn't an rvalue???");
1698 value->kind = axs_lvalue_memory;
1704 /* -FOO is equivalent to 0 - FOO. */
1705 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
1706 gen_usual_unary (ax, &value1); /* shouldn't do much */
1707 gen_expr (pc, ax, &value2);
1708 gen_usual_unary (ax, &value2);
1709 gen_usual_arithmetic (ax, &value1, &value2);
1710 gen_sub (ax, value, &value1, &value2);
1713 case UNOP_LOGICAL_NOT:
1715 gen_expr (pc, ax, value);
1716 gen_logical_not (ax, value);
1719 case UNOP_COMPLEMENT:
1721 gen_expr (pc, ax, value);
1722 gen_complement (ax, value);
1727 gen_expr (pc, ax, value);
1728 gen_usual_unary (ax, value);
1729 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1730 error ("Argument of unary `*' is not a pointer.");
1731 gen_deref (ax, value);
1736 gen_expr (pc, ax, value);
1737 gen_address_of (ax, value);
1742 /* Notice that gen_sizeof handles its own operand, unlike most
1743 of the other unary operator functions. This is because we
1744 have to throw away the code we generate. */
1745 gen_sizeof (pc, ax, value);
1748 case STRUCTOP_STRUCT:
1751 int length = (*pc)[1].longconst;
1752 char *name = &(*pc)[2].string;
1754 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1755 gen_expr (pc, ax, value);
1756 if (op == STRUCTOP_STRUCT)
1757 gen_struct_ref (ax, value, name, ".", "structure or union");
1758 else if (op == STRUCTOP_PTR)
1759 gen_struct_ref (ax, value, name, "->",
1760 "pointer to a structure or union");
1762 /* If this `if' chain doesn't handle it, then the case list
1763 shouldn't mention it, and we shouldn't be here. */
1764 error ("GDB bug: ax-gdb.c (gen_expr): unhandled struct case");
1769 error ("Attempt to use a type name as an expression.");
1772 error ("Unsupported operator in expression.");
1778 #if 0 /* not used */
1779 /* Generating bytecode from GDB expressions: driver */
1781 /* Given a GDB expression EXPR, produce a string of agent bytecode
1782 which computes its value. Return the agent expression, and set
1783 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1785 expr_to_agent (expr, value)
1786 struct expression *expr;
1787 struct axs_value *value;
1789 struct cleanup *old_chain = 0;
1790 struct agent_expr *ax = new_agent_expr ();
1791 union exp_element *pc;
1793 old_chain = make_cleanup ((make_cleanup_func) free_agent_expr, ax);
1797 gen_expr (&pc, ax, value);
1799 /* We have successfully built the agent expr, so cancel the cleanup
1800 request. If we add more cleanups that we always want done, this
1801 will have to get more complicated. */
1802 discard_cleanups (old_chain);
1807 /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1808 string of agent bytecode which will leave its address and size on
1809 the top of stack. Return the agent expression.
1811 Not sure this function is useful at all. */
1813 expr_to_address_and_size (expr)
1814 struct expression *expr;
1816 struct axs_value value;
1817 struct agent_expr *ax = expr_to_agent (expr, &value);
1819 /* Complain if the result is not a memory lvalue. */
1820 if (value.kind != axs_lvalue_memory)
1822 free_agent_expr (ax);
1823 error ("Expression does not denote an object in memory.");
1826 /* Push the object's size on the stack. */
1827 ax_const_l (ax, TYPE_LENGTH (value.type));
1833 /* Given a GDB expression EXPR, return bytecode to trace its value.
1834 The result will use the `trace' and `trace_quick' bytecodes to
1835 record the value of all memory touched by the expression. The
1836 caller can then use the ax_reqs function to discover which
1837 registers it relies upon. */
1839 gen_trace_for_expr (scope, expr)
1841 struct expression *expr;
1843 struct cleanup *old_chain = 0;
1844 struct agent_expr *ax = new_agent_expr (scope);
1845 union exp_element *pc;
1846 struct axs_value value;
1848 old_chain = make_cleanup ((make_cleanup_func) free_agent_expr, ax);
1852 gen_expr (&pc, ax, &value);
1854 /* Make sure we record the final object, and get rid of it. */
1855 gen_traced_pop (ax, &value);
1857 /* Oh, and terminate. */
1858 ax_simple (ax, aop_end);
1860 /* We have successfully built the agent expr, so cancel the cleanup
1861 request. If we add more cleanups that we always want done, this
1862 will have to get more complicated. */
1863 discard_cleanups (old_chain);
1869 /* The "agent" command, for testing: compile and disassemble an expression. */
1872 print_axs_value (f, value)
1874 struct axs_value *value;
1876 switch (value->kind)
1879 fputs_filtered ("rvalue", f);
1882 case axs_lvalue_memory:
1883 fputs_filtered ("memory lvalue", f);
1886 case axs_lvalue_register:
1887 fprintf_filtered (f, "register %d lvalue", value->u.reg);
1891 fputs_filtered (" : ", f);
1892 type_print (value->type, "", f, -1);
1897 agent_command (exp, from_tty)
1901 struct cleanup *old_chain = 0;
1902 struct expression *expr;
1903 struct agent_expr *agent;
1904 struct agent_reqs reqs;
1905 struct frame_info *fi = get_current_frame (); /* need current scope */
1907 /* We don't deal with overlay debugging at the moment. We need to
1908 think more carefully about this. If you copy this code into
1909 another command, change the error message; the user shouldn't
1910 have to know anything about agent expressions. */
1911 if (overlay_debugging)
1912 error ("GDB can't do agent expression translation with overlays.");
1915 error_no_arg ("expression to translate");
1917 expr = parse_expression (exp);
1918 old_chain = make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1919 agent = gen_trace_for_expr (fi->pc, expr);
1920 make_cleanup ((make_cleanup_func) free_agent_expr, agent);
1921 ax_print (gdb_stdout, agent);
1922 ax_reqs (agent, &reqs);
1924 do_cleanups (old_chain);
1929 /* Initialization code. */
1931 void _initialize_ax_gdb PARAMS ((void));
1933 _initialize_ax_gdb ()
1935 struct cmd_list_element *c;
1937 add_cmd ("agent", class_maintenance, agent_command,
1938 "Translate an expression into remote agent bytecode.",