1 /* GDB-specific functions for operating on agent expressions
2 Copyright 1998, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include "expression.h"
34 /* To make sense of this file, you should read doc/agentexpr.texi.
35 Then look at the types and enums in ax-gdb.h. For the code itself,
36 look at gen_expr, towards the bottom; that's the main function that
37 looks at the GDB expressions and calls everything else to generate
40 I'm beginning to wonder whether it wouldn't be nicer to internally
41 generate trees, with types, and then spit out the bytecode in
42 linear form afterwards; we could generate fewer `swap', `ext', and
43 `zero_ext' bytecodes that way; it would make good constant folding
44 easier, too. But at the moment, I think we should be willing to
45 pay for the simplicity of this code with less-than-optimal bytecode
48 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
52 /* Prototypes for local functions. */
54 /* There's a standard order to the arguments of these functions:
55 union exp_element ** --- pointer into expression
56 struct agent_expr * --- agent expression buffer to generate code into
57 struct axs_value * --- describes value left on top of stack */
59 static struct value
*const_var_ref (struct symbol
*var
);
60 static struct value
*const_expr (union exp_element
**pc
);
61 static struct value
*maybe_const_expr (union exp_element
**pc
);
63 static void gen_traced_pop (struct agent_expr
*, struct axs_value
*);
65 static void gen_sign_extend (struct agent_expr
*, struct type
*);
66 static void gen_extend (struct agent_expr
*, struct type
*);
67 static void gen_fetch (struct agent_expr
*, struct type
*);
68 static void gen_left_shift (struct agent_expr
*, int);
71 static void gen_frame_args_address (struct agent_expr
*);
72 static void gen_frame_locals_address (struct agent_expr
*);
73 static void gen_offset (struct agent_expr
*ax
, int offset
);
74 static void gen_sym_offset (struct agent_expr
*, struct symbol
*);
75 static void gen_var_ref (struct agent_expr
*ax
,
76 struct axs_value
*value
, struct symbol
*var
);
79 static void gen_int_literal (struct agent_expr
*ax
,
80 struct axs_value
*value
,
81 LONGEST k
, struct type
*type
);
84 static void require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
);
85 static void gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
);
86 static int type_wider_than (struct type
*type1
, struct type
*type2
);
87 static struct type
*max_type (struct type
*type1
, struct type
*type2
);
88 static void gen_conversion (struct agent_expr
*ax
,
89 struct type
*from
, struct type
*to
);
90 static int is_nontrivial_conversion (struct type
*from
, struct type
*to
);
91 static void gen_usual_arithmetic (struct agent_expr
*ax
,
92 struct axs_value
*value1
,
93 struct axs_value
*value2
);
94 static void gen_integral_promotions (struct agent_expr
*ax
,
95 struct axs_value
*value
);
96 static void gen_cast (struct agent_expr
*ax
,
97 struct axs_value
*value
, struct type
*type
);
98 static void gen_scale (struct agent_expr
*ax
,
99 enum agent_op op
, struct type
*type
);
100 static void gen_add (struct agent_expr
*ax
,
101 struct axs_value
*value
,
102 struct axs_value
*value1
,
103 struct axs_value
*value2
, char *name
);
104 static void gen_sub (struct agent_expr
*ax
,
105 struct axs_value
*value
,
106 struct axs_value
*value1
, struct axs_value
*value2
);
107 static void gen_binop (struct agent_expr
*ax
,
108 struct axs_value
*value
,
109 struct axs_value
*value1
,
110 struct axs_value
*value2
,
112 enum agent_op op_unsigned
, int may_carry
, char *name
);
113 static void gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
);
114 static void gen_complement (struct agent_expr
*ax
, struct axs_value
*value
);
115 static void gen_deref (struct agent_expr
*, struct axs_value
*);
116 static void gen_address_of (struct agent_expr
*, struct axs_value
*);
117 static int find_field (struct type
*type
, char *name
);
118 static void gen_bitfield_ref (struct agent_expr
*ax
,
119 struct axs_value
*value
,
120 struct type
*type
, int start
, int end
);
121 static void gen_struct_ref (struct agent_expr
*ax
,
122 struct axs_value
*value
,
124 char *operator_name
, char *operand_name
);
125 static void gen_repeat (union exp_element
**pc
,
126 struct agent_expr
*ax
, struct axs_value
*value
);
127 static void gen_sizeof (union exp_element
**pc
,
128 struct agent_expr
*ax
, struct axs_value
*value
);
129 static void gen_expr (union exp_element
**pc
,
130 struct agent_expr
*ax
, struct axs_value
*value
);
132 static void print_axs_value (struct ui_file
*f
, struct axs_value
* value
);
133 static void agent_command (char *exp
, int from_tty
);
136 /* Detecting constant expressions. */
138 /* If the variable reference at *PC is a constant, return its value.
139 Otherwise, return zero.
141 Hey, Wally! How can a variable reference be a constant?
143 Well, Beav, this function really handles the OP_VAR_VALUE operator,
144 not specifically variable references. GDB uses OP_VAR_VALUE to
145 refer to any kind of symbolic reference: function names, enum
146 elements, and goto labels are all handled through the OP_VAR_VALUE
147 operator, even though they're constants. It makes sense given the
150 Gee, Wally, don'cha wonder sometimes if data representations that
151 subvert commonly accepted definitions of terms in favor of heavily
152 context-specific interpretations are really just a tool of the
153 programming hegemony to preserve their power and exclude the
156 static struct value
*
157 const_var_ref (struct symbol
*var
)
159 struct type
*type
= SYMBOL_TYPE (var
);
161 switch (SYMBOL_CLASS (var
))
164 return value_from_longest (type
, (LONGEST
) SYMBOL_VALUE (var
));
167 return value_from_pointer (type
, (CORE_ADDR
) SYMBOL_VALUE_ADDRESS (var
));
175 /* If the expression starting at *PC has a constant value, return it.
176 Otherwise, return zero. If we return a value, then *PC will be
177 advanced to the end of it. If we return zero, *PC could be
179 static struct value
*
180 const_expr (union exp_element
**pc
)
182 enum exp_opcode op
= (*pc
)->opcode
;
189 struct type
*type
= (*pc
)[1].type
;
190 LONGEST k
= (*pc
)[2].longconst
;
192 return value_from_longest (type
, k
);
197 struct value
*v
= const_var_ref ((*pc
)[2].symbol
);
202 /* We could add more operators in here. */
206 v1
= const_expr (pc
);
208 return value_neg (v1
);
218 /* Like const_expr, but guarantee also that *PC is undisturbed if the
219 expression is not constant. */
220 static struct value
*
221 maybe_const_expr (union exp_element
**pc
)
223 union exp_element
*tentative_pc
= *pc
;
224 struct value
*v
= const_expr (&tentative_pc
);
226 /* If we got a value, then update the real PC. */
234 /* Generating bytecode from GDB expressions: general assumptions */
236 /* Here are a few general assumptions made throughout the code; if you
237 want to make a change that contradicts one of these, then you'd
238 better scan things pretty thoroughly.
240 - We assume that all values occupy one stack element. For example,
241 sometimes we'll swap to get at the left argument to a binary
242 operator. If we decide that void values should occupy no stack
243 elements, or that synthetic arrays (whose size is determined at
244 run time, created by the `@' operator) should occupy two stack
245 elements (address and length), then this will cause trouble.
247 - We assume the stack elements are infinitely wide, and that we
248 don't have to worry what happens if the user requests an
249 operation that is wider than the actual interpreter's stack.
250 That is, it's up to the interpreter to handle directly all the
251 integer widths the user has access to. (Woe betide the language
254 - We don't support side effects. Thus, we don't have to worry about
255 GCC's generalized lvalues, function calls, etc.
257 - We don't support floating point. Many places where we switch on
258 some type don't bother to include cases for floating point; there
259 may be even more subtle ways this assumption exists. For
260 example, the arguments to % must be integers.
262 - We assume all subexpressions have a static, unchanging type. If
263 we tried to support convenience variables, this would be a
266 - All values on the stack should always be fully zero- or
269 (I wasn't sure whether to choose this or its opposite --- that
270 only addresses are assumed extended --- but it turns out that
271 neither convention completely eliminates spurious extend
272 operations (if everything is always extended, then you have to
273 extend after add, because it could overflow; if nothing is
274 extended, then you end up producing extends whenever you change
275 sizes), and this is simpler.) */
278 /* Generating bytecode from GDB expressions: the `trace' kludge */
280 /* The compiler in this file is a general-purpose mechanism for
281 translating GDB expressions into bytecode. One ought to be able to
282 find a million and one uses for it.
284 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
285 of expediency. Let he who is without sin cast the first stone.
287 For the data tracing facility, we need to insert `trace' bytecodes
288 before each data fetch; this records all the memory that the
289 expression touches in the course of evaluation, so that memory will
290 be available when the user later tries to evaluate the expression
293 This should be done (I think) in a post-processing pass, that walks
294 an arbitrary agent expression and inserts `trace' operations at the
295 appropriate points. But it's much faster to just hack them
296 directly into the code. And since we're in a crunch, that's what
299 Setting the flag trace_kludge to non-zero enables the code that
300 emits the trace bytecodes at the appropriate points. */
301 static int trace_kludge
;
303 /* Trace the lvalue on the stack, if it needs it. In either case, pop
304 the value. Useful on the left side of a comma, and at the end of
305 an expression being used for tracing. */
307 gen_traced_pop (struct agent_expr
*ax
, struct axs_value
*value
)
313 /* We don't trace rvalues, just the lvalues necessary to
314 produce them. So just dispose of this value. */
315 ax_simple (ax
, aop_pop
);
318 case axs_lvalue_memory
:
320 int length
= TYPE_LENGTH (value
->type
);
322 /* There's no point in trying to use a trace_quick bytecode
323 here, since "trace_quick SIZE pop" is three bytes, whereas
324 "const8 SIZE trace" is also three bytes, does the same
325 thing, and the simplest code which generates that will also
326 work correctly for objects with large sizes. */
327 ax_const_l (ax
, length
);
328 ax_simple (ax
, aop_trace
);
332 case axs_lvalue_register
:
333 /* We need to mention the register somewhere in the bytecode,
334 so ax_reqs will pick it up and add it to the mask of
336 ax_reg (ax
, value
->u
.reg
);
337 ax_simple (ax
, aop_pop
);
341 /* If we're not tracing, just pop the value. */
342 ax_simple (ax
, aop_pop
);
347 /* Generating bytecode from GDB expressions: helper functions */
349 /* Assume that the lower bits of the top of the stack is a value of
350 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
352 gen_sign_extend (struct agent_expr
*ax
, struct type
*type
)
354 /* Do we need to sign-extend this? */
355 if (!TYPE_UNSIGNED (type
))
356 ax_ext (ax
, type
->length
* TARGET_CHAR_BIT
);
360 /* Assume the lower bits of the top of the stack hold a value of type
361 TYPE, and the upper bits are garbage. Sign-extend or truncate as
364 gen_extend (struct agent_expr
*ax
, struct type
*type
)
366 int bits
= type
->length
* TARGET_CHAR_BIT
;
368 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, bits
));
372 /* Assume that the top of the stack contains a value of type "pointer
373 to TYPE"; generate code to fetch its value. Note that TYPE is the
374 target type, not the pointer type. */
376 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
380 /* Record the area of memory we're about to fetch. */
381 ax_trace_quick (ax
, TYPE_LENGTH (type
));
390 /* It's a scalar value, so we know how to dereference it. How
391 many bytes long is it? */
392 switch (type
->length
)
394 case 8 / TARGET_CHAR_BIT
:
395 ax_simple (ax
, aop_ref8
);
397 case 16 / TARGET_CHAR_BIT
:
398 ax_simple (ax
, aop_ref16
);
400 case 32 / TARGET_CHAR_BIT
:
401 ax_simple (ax
, aop_ref32
);
403 case 64 / TARGET_CHAR_BIT
:
404 ax_simple (ax
, aop_ref64
);
407 /* Either our caller shouldn't have asked us to dereference
408 that pointer (other code's fault), or we're not
409 implementing something we should be (this code's fault).
410 In any case, it's a bug the user shouldn't see. */
412 internal_error ("ax-gdb.c (gen_fetch): strange size");
415 gen_sign_extend (ax
, type
);
419 /* Either our caller shouldn't have asked us to dereference that
420 pointer (other code's fault), or we're not implementing
421 something we should be (this code's fault). In any case,
422 it's a bug the user shouldn't see. */
423 internal_error ("ax-gdb.c (gen_fetch): bad type code");
428 /* Generate code to left shift the top of the stack by DISTANCE bits, or
429 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
430 unsigned (logical) right shifts. */
432 gen_left_shift (struct agent_expr
*ax
, int distance
)
436 ax_const_l (ax
, distance
);
437 ax_simple (ax
, aop_lsh
);
439 else if (distance
< 0)
441 ax_const_l (ax
, -distance
);
442 ax_simple (ax
, aop_rsh_unsigned
);
448 /* Generating bytecode from GDB expressions: symbol references */
450 /* Generate code to push the base address of the argument portion of
451 the top stack frame. */
453 gen_frame_args_address (struct agent_expr
*ax
)
455 long frame_reg
, frame_offset
;
457 TARGET_VIRTUAL_FRAME_POINTER (ax
->scope
, &frame_reg
, &frame_offset
);
458 ax_reg (ax
, frame_reg
);
459 gen_offset (ax
, frame_offset
);
463 /* Generate code to push the base address of the locals portion of the
466 gen_frame_locals_address (struct agent_expr
*ax
)
468 long frame_reg
, frame_offset
;
470 TARGET_VIRTUAL_FRAME_POINTER (ax
->scope
, &frame_reg
, &frame_offset
);
471 ax_reg (ax
, frame_reg
);
472 gen_offset (ax
, frame_offset
);
476 /* Generate code to add OFFSET to the top of the stack. Try to
477 generate short and readable code. We use this for getting to
478 variables on the stack, and structure members. If we were
479 programming in ML, it would be clearer why these are the same
482 gen_offset (struct agent_expr
*ax
, int offset
)
484 /* It would suffice to simply push the offset and add it, but this
485 makes it easier to read positive and negative offsets in the
489 ax_const_l (ax
, offset
);
490 ax_simple (ax
, aop_add
);
494 ax_const_l (ax
, -offset
);
495 ax_simple (ax
, aop_sub
);
500 /* In many cases, a symbol's value is the offset from some other
501 address (stack frame, base register, etc.) Generate code to add
502 VAR's value to the top of the stack. */
504 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
506 gen_offset (ax
, SYMBOL_VALUE (var
));
510 /* Generate code for a variable reference to AX. The variable is the
511 symbol VAR. Set VALUE to describe the result. */
514 gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
, struct symbol
*var
)
516 /* Dereference any typedefs. */
517 value
->type
= check_typedef (SYMBOL_TYPE (var
));
519 /* I'm imitating the code in read_var_value. */
520 switch (SYMBOL_CLASS (var
))
522 case LOC_CONST
: /* A constant, like an enum value. */
523 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE (var
));
524 value
->kind
= axs_rvalue
;
527 case LOC_LABEL
: /* A goto label, being used as a value. */
528 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
529 value
->kind
= axs_rvalue
;
532 case LOC_CONST_BYTES
:
533 internal_error ("ax-gdb.c (gen_var_ref): LOC_CONST_BYTES symbols are not supported");
535 /* Variable at a fixed location in memory. Easy. */
537 /* Push the address of the variable. */
538 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (var
));
539 value
->kind
= axs_lvalue_memory
;
542 case LOC_ARG
: /* var lives in argument area of frame */
543 gen_frame_args_address (ax
);
544 gen_sym_offset (ax
, var
);
545 value
->kind
= axs_lvalue_memory
;
548 case LOC_REF_ARG
: /* As above, but the frame slot really
549 holds the address of the variable. */
550 gen_frame_args_address (ax
);
551 gen_sym_offset (ax
, var
);
552 /* Don't assume any particular pointer size. */
553 gen_fetch (ax
, lookup_pointer_type (builtin_type_void
));
554 value
->kind
= axs_lvalue_memory
;
557 case LOC_LOCAL
: /* var lives in locals area of frame */
559 gen_frame_locals_address (ax
);
560 gen_sym_offset (ax
, var
);
561 value
->kind
= axs_lvalue_memory
;
564 case LOC_BASEREG
: /* relative to some base register */
565 case LOC_BASEREG_ARG
:
566 ax_reg (ax
, SYMBOL_BASEREG (var
));
567 gen_sym_offset (ax
, var
);
568 value
->kind
= axs_lvalue_memory
;
572 error ("Cannot compute value of typedef `%s'.",
573 SYMBOL_SOURCE_NAME (var
));
577 ax_const_l (ax
, BLOCK_START (SYMBOL_BLOCK_VALUE (var
)));
578 value
->kind
= axs_rvalue
;
583 /* Don't generate any code at all; in the process of treating
584 this as an lvalue or rvalue, the caller will generate the
586 value
->kind
= axs_lvalue_register
;
587 value
->u
.reg
= SYMBOL_VALUE (var
);
590 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
591 register, not on the stack. Simpler than LOC_REGISTER and
592 LOC_REGPARM, because it's just like any other case where the
593 thing has a real address. */
594 case LOC_REGPARM_ADDR
:
595 ax_reg (ax
, SYMBOL_VALUE (var
));
596 value
->kind
= axs_lvalue_memory
;
601 struct minimal_symbol
*msym
602 = lookup_minimal_symbol (SYMBOL_NAME (var
), NULL
, NULL
);
604 error ("Couldn't resolve symbol `%s'.", SYMBOL_SOURCE_NAME (var
));
606 /* Push the address of the variable. */
607 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (msym
));
608 value
->kind
= axs_lvalue_memory
;
612 case LOC_OPTIMIZED_OUT
:
613 error ("The variable `%s' has been optimized out.",
614 SYMBOL_SOURCE_NAME (var
));
618 error ("Cannot find value of botched symbol `%s'.",
619 SYMBOL_SOURCE_NAME (var
));
626 /* Generating bytecode from GDB expressions: literals */
629 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
633 value
->kind
= axs_rvalue
;
639 /* Generating bytecode from GDB expressions: unary conversions, casts */
641 /* Take what's on the top of the stack (as described by VALUE), and
642 try to make an rvalue out of it. Signal an error if we can't do
645 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
650 /* It's already an rvalue. */
653 case axs_lvalue_memory
:
654 /* The top of stack is the address of the object. Dereference. */
655 gen_fetch (ax
, value
->type
);
658 case axs_lvalue_register
:
659 /* There's nothing on the stack, but value->u.reg is the
660 register number containing the value.
662 When we add floating-point support, this is going to have to
663 change. What about SPARC register pairs, for example? */
664 ax_reg (ax
, value
->u
.reg
);
665 gen_extend (ax
, value
->type
);
669 value
->kind
= axs_rvalue
;
673 /* Assume the top of the stack is described by VALUE, and perform the
674 usual unary conversions. This is motivated by ANSI 6.2.2, but of
675 course GDB expressions are not ANSI; they're the mishmash union of
676 a bunch of languages. Rah.
678 NOTE! This function promises to produce an rvalue only when the
679 incoming value is of an appropriate type. In other words, the
680 consumer of the value this function produces may assume the value
681 is an rvalue only after checking its type.
683 The immediate issue is that if the user tries to use a structure or
684 union as an operand of, say, the `+' operator, we don't want to try
685 to convert that structure to an rvalue; require_rvalue will bomb on
686 structs and unions. Rather, we want to simply pass the struct
687 lvalue through unchanged, and let `+' raise an error. */
690 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
692 /* We don't have to generate any code for the usual integral
693 conversions, since values are always represented as full-width on
694 the stack. Should we tweak the type? */
696 /* Some types require special handling. */
697 switch (value
->type
->code
)
699 /* Functions get converted to a pointer to the function. */
701 value
->type
= lookup_pointer_type (value
->type
);
702 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
705 /* Arrays get converted to a pointer to their first element, and
706 are no longer an lvalue. */
707 case TYPE_CODE_ARRAY
:
709 struct type
*elements
= TYPE_TARGET_TYPE (value
->type
);
710 value
->type
= lookup_pointer_type (elements
);
711 value
->kind
= axs_rvalue
;
712 /* We don't need to generate any code; the address of the array
713 is also the address of its first element. */
717 /* Don't try to convert structures and unions to rvalues. Let the
718 consumer signal an error. */
719 case TYPE_CODE_STRUCT
:
720 case TYPE_CODE_UNION
:
723 /* If the value is an enum, call it an integer. */
725 value
->type
= builtin_type_int
;
729 /* If the value is an lvalue, dereference it. */
730 require_rvalue (ax
, value
);
734 /* Return non-zero iff the type TYPE1 is considered "wider" than the
735 type TYPE2, according to the rules described in gen_usual_arithmetic. */
737 type_wider_than (struct type
*type1
, struct type
*type2
)
739 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)
740 || (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
741 && TYPE_UNSIGNED (type1
)
742 && !TYPE_UNSIGNED (type2
)));
746 /* Return the "wider" of the two types TYPE1 and TYPE2. */
748 max_type (struct type
*type1
, struct type
*type2
)
750 return type_wider_than (type1
, type2
) ? type1
: type2
;
754 /* Generate code to convert a scalar value of type FROM to type TO. */
756 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
758 /* Perhaps there is a more graceful way to state these rules. */
760 /* If we're converting to a narrower type, then we need to clear out
762 if (TYPE_LENGTH (to
) < TYPE_LENGTH (from
))
763 gen_extend (ax
, from
);
765 /* If the two values have equal width, but different signednesses,
766 then we need to extend. */
767 else if (TYPE_LENGTH (to
) == TYPE_LENGTH (from
))
769 if (TYPE_UNSIGNED (from
) != TYPE_UNSIGNED (to
))
773 /* If we're converting to a wider type, and becoming unsigned, then
774 we need to zero out any possible sign bits. */
775 else if (TYPE_LENGTH (to
) > TYPE_LENGTH (from
))
777 if (TYPE_UNSIGNED (to
))
783 /* Return non-zero iff the type FROM will require any bytecodes to be
784 emitted to be converted to the type TO. */
786 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
788 struct agent_expr
*ax
= new_agent_expr (0);
791 /* Actually generate the code, and see if anything came out. At the
792 moment, it would be trivial to replicate the code in
793 gen_conversion here, but in the future, when we're supporting
794 floating point and the like, it may not be. Doing things this
795 way allows this function to be independent of the logic in
797 gen_conversion (ax
, from
, to
);
798 nontrivial
= ax
->len
> 0;
799 free_agent_expr (ax
);
804 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
805 6.2.1.5) for the two operands of an arithmetic operator. This
806 effectively finds a "least upper bound" type for the two arguments,
807 and promotes each argument to that type. *VALUE1 and *VALUE2
808 describe the values as they are passed in, and as they are left. */
810 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
811 struct axs_value
*value2
)
813 /* Do the usual binary conversions. */
814 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
815 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
817 /* The ANSI integral promotions seem to work this way: Order the
818 integer types by size, and then by signedness: an n-bit
819 unsigned type is considered "wider" than an n-bit signed
820 type. Promote to the "wider" of the two types, and always
821 promote at least to int. */
822 struct type
*target
= max_type (builtin_type_int
,
823 max_type (value1
->type
, value2
->type
));
825 /* Deal with value2, on the top of the stack. */
826 gen_conversion (ax
, value2
->type
, target
);
828 /* Deal with value1, not on the top of the stack. Don't
829 generate the `swap' instructions if we're not actually going
831 if (is_nontrivial_conversion (value1
->type
, target
))
833 ax_simple (ax
, aop_swap
);
834 gen_conversion (ax
, value1
->type
, target
);
835 ax_simple (ax
, aop_swap
);
838 value1
->type
= value2
->type
= target
;
843 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
844 the value on the top of the stack, as described by VALUE. Assume
845 the value has integral type. */
847 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
849 if (!type_wider_than (value
->type
, builtin_type_int
))
851 gen_conversion (ax
, value
->type
, builtin_type_int
);
852 value
->type
= builtin_type_int
;
854 else if (!type_wider_than (value
->type
, builtin_type_unsigned_int
))
856 gen_conversion (ax
, value
->type
, builtin_type_unsigned_int
);
857 value
->type
= builtin_type_unsigned_int
;
862 /* Generate code for a cast to TYPE. */
864 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
866 /* GCC does allow casts to yield lvalues, so this should be fixed
867 before merging these changes into the trunk. */
868 require_rvalue (ax
, value
);
869 /* Dereference typedefs. */
870 type
= check_typedef (type
);
875 /* It's implementation-defined, and I'll bet this is what GCC
879 case TYPE_CODE_ARRAY
:
880 case TYPE_CODE_STRUCT
:
881 case TYPE_CODE_UNION
:
883 error ("Illegal type cast: intended type must be scalar.");
886 /* We don't have to worry about the size of the value, because
887 all our integral values are fully sign-extended, and when
888 casting pointers we can do anything we like. Is there any
889 way for us to actually know what GCC actually does with a
895 gen_conversion (ax
, value
->type
, type
);
899 /* We could pop the value, and rely on everyone else to check
900 the type and notice that this value doesn't occupy a stack
901 slot. But for now, leave the value on the stack, and
902 preserve the "value == stack element" assumption. */
906 error ("Casts to requested type are not yet implemented.");
914 /* Generating bytecode from GDB expressions: arithmetic */
916 /* Scale the integer on the top of the stack by the size of the target
917 of the pointer type TYPE. */
919 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
921 struct type
*element
= TYPE_TARGET_TYPE (type
);
923 if (element
->length
!= 1)
925 ax_const_l (ax
, element
->length
);
931 /* Generate code for an addition; non-trivial because we deal with
932 pointer arithmetic. We set VALUE to describe the result value; we
933 assume VALUE1 and VALUE2 describe the two operands, and that
934 they've undergone the usual binary conversions. Used by both
935 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
937 gen_add (struct agent_expr
*ax
, struct axs_value
*value
,
938 struct axs_value
*value1
, struct axs_value
*value2
, char *name
)
941 if (value1
->type
->code
== TYPE_CODE_INT
942 && value2
->type
->code
== TYPE_CODE_PTR
)
944 /* Swap the values and proceed normally. */
945 ax_simple (ax
, aop_swap
);
946 gen_scale (ax
, aop_mul
, value2
->type
);
947 ax_simple (ax
, aop_add
);
948 gen_extend (ax
, value2
->type
); /* Catch overflow. */
949 value
->type
= value2
->type
;
953 else if (value1
->type
->code
== TYPE_CODE_PTR
954 && value2
->type
->code
== TYPE_CODE_INT
)
956 gen_scale (ax
, aop_mul
, value1
->type
);
957 ax_simple (ax
, aop_add
);
958 gen_extend (ax
, value1
->type
); /* Catch overflow. */
959 value
->type
= value1
->type
;
962 /* Must be number + number; the usual binary conversions will have
963 brought them both to the same width. */
964 else if (value1
->type
->code
== TYPE_CODE_INT
965 && value2
->type
->code
== TYPE_CODE_INT
)
967 ax_simple (ax
, aop_add
);
968 gen_extend (ax
, value1
->type
); /* Catch overflow. */
969 value
->type
= value1
->type
;
973 error ("Illegal combination of types in %s.", name
);
975 value
->kind
= axs_rvalue
;
979 /* Generate code for an addition; non-trivial because we have to deal
980 with pointer arithmetic. We set VALUE to describe the result
981 value; we assume VALUE1 and VALUE2 describe the two operands, and
982 that they've undergone the usual binary conversions. */
984 gen_sub (struct agent_expr
*ax
, struct axs_value
*value
,
985 struct axs_value
*value1
, struct axs_value
*value2
)
987 if (value1
->type
->code
== TYPE_CODE_PTR
)
989 /* Is it PTR - INT? */
990 if (value2
->type
->code
== TYPE_CODE_INT
)
992 gen_scale (ax
, aop_mul
, value1
->type
);
993 ax_simple (ax
, aop_sub
);
994 gen_extend (ax
, value1
->type
); /* Catch overflow. */
995 value
->type
= value1
->type
;
998 /* Is it PTR - PTR? Strictly speaking, the types ought to
999 match, but this is what the normal GDB expression evaluator
1001 else if (value2
->type
->code
== TYPE_CODE_PTR
1002 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1
->type
))
1003 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2
->type
))))
1005 ax_simple (ax
, aop_sub
);
1006 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1007 value
->type
= builtin_type_long
; /* FIXME --- should be ptrdiff_t */
1011 First argument of `-' is a pointer, but second argument is neither\n\
1012 an integer nor a pointer of the same type.");
1015 /* Must be number + number. */
1016 else if (value1
->type
->code
== TYPE_CODE_INT
1017 && value2
->type
->code
== TYPE_CODE_INT
)
1019 ax_simple (ax
, aop_sub
);
1020 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1021 value
->type
= value1
->type
;
1025 error ("Illegal combination of types in subtraction.");
1027 value
->kind
= axs_rvalue
;
1030 /* Generate code for a binary operator that doesn't do pointer magic.
1031 We set VALUE to describe the result value; we assume VALUE1 and
1032 VALUE2 describe the two operands, and that they've undergone the
1033 usual binary conversions. MAY_CARRY should be non-zero iff the
1034 result needs to be extended. NAME is the English name of the
1035 operator, used in error messages */
1037 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1038 struct axs_value
*value1
, struct axs_value
*value2
, enum agent_op op
,
1039 enum agent_op op_unsigned
, int may_carry
, char *name
)
1041 /* We only handle INT op INT. */
1042 if ((value1
->type
->code
!= TYPE_CODE_INT
)
1043 || (value2
->type
->code
!= TYPE_CODE_INT
))
1044 error ("Illegal combination of types in %s.", name
);
1047 TYPE_UNSIGNED (value1
->type
) ? op_unsigned
: op
);
1049 gen_extend (ax
, value1
->type
); /* catch overflow */
1050 value
->type
= value1
->type
;
1051 value
->kind
= axs_rvalue
;
1056 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
)
1058 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
1059 && TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1060 error ("Illegal type of operand to `!'.");
1062 gen_usual_unary (ax
, value
);
1063 ax_simple (ax
, aop_log_not
);
1064 value
->type
= builtin_type_int
;
1069 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1071 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
)
1072 error ("Illegal type of operand to `~'.");
1074 gen_usual_unary (ax
, value
);
1075 gen_integral_promotions (ax
, value
);
1076 ax_simple (ax
, aop_bit_not
);
1077 gen_extend (ax
, value
->type
);
1082 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1084 /* Dereference the value on the top of the stack. */
1086 gen_deref (struct agent_expr
*ax
, struct axs_value
*value
)
1088 /* The caller should check the type, because several operators use
1089 this, and we don't know what error message to generate. */
1090 if (value
->type
->code
!= TYPE_CODE_PTR
)
1091 internal_error ("ax-gdb.c (gen_deref): expected a pointer");
1093 /* We've got an rvalue now, which is a pointer. We want to yield an
1094 lvalue, whose address is exactly that pointer. So we don't
1095 actually emit any code; we just change the type from "Pointer to
1096 T" to "T", and mark the value as an lvalue in memory. Leave it
1097 to the consumer to actually dereference it. */
1098 value
->type
= check_typedef (TYPE_TARGET_TYPE (value
->type
));
1099 value
->kind
= ((value
->type
->code
== TYPE_CODE_FUNC
)
1100 ? axs_rvalue
: axs_lvalue_memory
);
1104 /* Produce the address of the lvalue on the top of the stack. */
1106 gen_address_of (struct agent_expr
*ax
, struct axs_value
*value
)
1108 /* Special case for taking the address of a function. The ANSI
1109 standard describes this as a special case, too, so this
1110 arrangement is not without motivation. */
1111 if (value
->type
->code
== TYPE_CODE_FUNC
)
1112 /* The value's already an rvalue on the stack, so we just need to
1114 value
->type
= lookup_pointer_type (value
->type
);
1116 switch (value
->kind
)
1119 error ("Operand of `&' is an rvalue, which has no address.");
1121 case axs_lvalue_register
:
1122 error ("Operand of `&' is in a register, and has no address.");
1124 case axs_lvalue_memory
:
1125 value
->kind
= axs_rvalue
;
1126 value
->type
= lookup_pointer_type (value
->type
);
1132 /* A lot of this stuff will have to change to support C++. But we're
1133 not going to deal with that at the moment. */
1135 /* Find the field in the structure type TYPE named NAME, and return
1136 its index in TYPE's field array. */
1138 find_field (struct type
*type
, char *name
)
1142 CHECK_TYPEDEF (type
);
1144 /* Make sure this isn't C++. */
1145 if (TYPE_N_BASECLASSES (type
) != 0)
1146 internal_error ("ax-gdb.c (find_field): derived classes supported");
1148 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1150 char *this_name
= TYPE_FIELD_NAME (type
, i
);
1152 if (this_name
&& STREQ (name
, this_name
))
1155 if (this_name
[0] == '\0')
1156 internal_error ("ax-gdb.c (find_field): anonymous unions not supported");
1159 error ("Couldn't find member named `%s' in struct/union `%s'",
1160 name
, type
->tag_name
);
1166 /* Generate code to push the value of a bitfield of a structure whose
1167 address is on the top of the stack. START and END give the
1168 starting and one-past-ending *bit* numbers of the field within the
1171 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1172 struct type
*type
, int start
, int end
)
1174 /* Note that ops[i] fetches 8 << i bits. */
1175 static enum agent_op ops
[]
1177 {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1178 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1180 /* We don't want to touch any byte that the bitfield doesn't
1181 actually occupy; we shouldn't make any accesses we're not
1182 explicitly permitted to. We rely here on the fact that the
1183 bytecode `ref' operators work on unaligned addresses.
1185 It takes some fancy footwork to get the stack to work the way
1186 we'd like. Say we're retrieving a bitfield that requires three
1187 fetches. Initially, the stack just contains the address:
1189 For the first fetch, we duplicate the address
1191 then add the byte offset, do the fetch, and shift and mask as
1192 needed, yielding a fragment of the value, properly aligned for
1193 the final bitwise or:
1195 then we swap, and repeat the process:
1196 frag1 addr --- address on top
1197 frag1 addr addr --- duplicate it
1198 frag1 addr frag2 --- get second fragment
1199 frag1 frag2 addr --- swap again
1200 frag1 frag2 frag3 --- get third fragment
1201 Notice that, since the third fragment is the last one, we don't
1202 bother duplicating the address this time. Now we have all the
1203 fragments on the stack, and we can simply `or' them together,
1204 yielding the final value of the bitfield. */
1206 /* The first and one-after-last bits in the field, but rounded down
1207 and up to byte boundaries. */
1208 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1209 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1213 /* current bit offset within the structure */
1216 /* The index in ops of the opcode we're considering. */
1219 /* The number of fragments we generated in the process. Probably
1220 equal to the number of `one' bits in bytesize, but who cares? */
1223 /* Dereference any typedefs. */
1224 type
= check_typedef (type
);
1226 /* Can we fetch the number of bits requested at all? */
1227 if ((end
- start
) > ((1 << num_ops
) * 8))
1228 internal_error ("ax-gdb.c (gen_bitfield_ref): bitfield too wide");
1230 /* Note that we know here that we only need to try each opcode once.
1231 That may not be true on machines with weird byte sizes. */
1232 offset
= bound_start
;
1234 for (op
= num_ops
- 1; op
>= 0; op
--)
1236 /* number of bits that ops[op] would fetch */
1237 int op_size
= 8 << op
;
1239 /* The stack at this point, from bottom to top, contains zero or
1240 more fragments, then the address. */
1242 /* Does this fetch fit within the bitfield? */
1243 if (offset
+ op_size
<= bound_end
)
1245 /* Is this the last fragment? */
1246 int last_frag
= (offset
+ op_size
== bound_end
);
1249 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1251 /* Add the offset. */
1252 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1256 /* Record the area of memory we're about to fetch. */
1257 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1260 /* Perform the fetch. */
1261 ax_simple (ax
, ops
[op
]);
1263 /* Shift the bits we have to their proper position.
1264 gen_left_shift will generate right shifts when the operand
1267 A big-endian field diagram to ponder:
1268 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1269 +------++------++------++------++------++------++------++------+
1270 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1272 bit number 16 32 48 53
1273 These are bit numbers as supplied by GDB. Note that the
1274 bit numbers run from right to left once you've fetched the
1277 A little-endian field diagram to ponder:
1278 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1279 +------++------++------++------++------++------++------++------+
1280 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1282 bit number 48 32 16 4 0
1284 In both cases, the most significant end is on the left
1285 (i.e. normal numeric writing order), which means that you
1286 don't go crazy thinking about `left' and `right' shifts.
1288 We don't have to worry about masking yet:
1289 - If they contain garbage off the least significant end, then we
1290 must be looking at the low end of the field, and the right
1291 shift will wipe them out.
1292 - If they contain garbage off the most significant end, then we
1293 must be looking at the most significant end of the word, and
1294 the sign/zero extension will wipe them out.
1295 - If we're in the interior of the word, then there is no garbage
1296 on either end, because the ref operators zero-extend. */
1297 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1298 gen_left_shift (ax
, end
- (offset
+ op_size
));
1300 gen_left_shift (ax
, offset
- start
);
1303 /* Bring the copy of the address up to the top. */
1304 ax_simple (ax
, aop_swap
);
1311 /* Generate enough bitwise `or' operations to combine all the
1312 fragments we left on the stack. */
1313 while (fragment_count
-- > 1)
1314 ax_simple (ax
, aop_bit_or
);
1316 /* Sign- or zero-extend the value as appropriate. */
1317 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1319 /* This is *not* an lvalue. Ugh. */
1320 value
->kind
= axs_rvalue
;
1325 /* Generate code to reference the member named FIELD of a structure or
1326 union. The top of the stack, as described by VALUE, should have
1327 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1328 the operator being compiled, and OPERAND_NAME is the kind of thing
1329 it operates on; we use them in error messages. */
1331 gen_struct_ref (struct agent_expr
*ax
, struct axs_value
*value
, char *field
,
1332 char *operator_name
, char *operand_name
)
1337 /* Follow pointers until we reach a non-pointer. These aren't the C
1338 semantics, but they're what the normal GDB evaluator does, so we
1339 should at least be consistent. */
1340 while (value
->type
->code
== TYPE_CODE_PTR
)
1342 gen_usual_unary (ax
, value
);
1343 gen_deref (ax
, value
);
1347 /* This must yield a structure or a union. */
1348 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
1349 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
1350 error ("The left operand of `%s' is not a %s.",
1351 operator_name
, operand_name
);
1353 /* And it must be in memory; we don't deal with structure rvalues,
1354 or structures living in registers. */
1355 if (value
->kind
!= axs_lvalue_memory
)
1356 error ("Structure does not live in memory.");
1358 i
= find_field (type
, field
);
1360 /* Is this a bitfield? */
1361 if (TYPE_FIELD_PACKED (type
, i
))
1362 gen_bitfield_ref (ax
, value
, TYPE_FIELD_TYPE (type
, i
),
1363 TYPE_FIELD_BITPOS (type
, i
),
1364 (TYPE_FIELD_BITPOS (type
, i
)
1365 + TYPE_FIELD_BITSIZE (type
, i
)));
1368 gen_offset (ax
, TYPE_FIELD_BITPOS (type
, i
) / TARGET_CHAR_BIT
);
1369 value
->kind
= axs_lvalue_memory
;
1370 value
->type
= TYPE_FIELD_TYPE (type
, i
);
1375 /* Generate code for GDB's magical `repeat' operator.
1376 LVALUE @ INT creates an array INT elements long, and whose elements
1377 have the same type as LVALUE, located in memory so that LVALUE is
1378 its first element. For example, argv[0]@argc gives you the array
1379 of command-line arguments.
1381 Unfortunately, because we have to know the types before we actually
1382 have a value for the expression, we can't implement this perfectly
1383 without changing the type system, having values that occupy two
1384 stack slots, doing weird things with sizeof, etc. So we require
1385 the right operand to be a constant expression. */
1387 gen_repeat (union exp_element
**pc
, struct agent_expr
*ax
,
1388 struct axs_value
*value
)
1390 struct axs_value value1
;
1391 /* We don't want to turn this into an rvalue, so no conversions
1393 gen_expr (pc
, ax
, &value1
);
1394 if (value1
.kind
!= axs_lvalue_memory
)
1395 error ("Left operand of `@' must be an object in memory.");
1397 /* Evaluate the length; it had better be a constant. */
1399 struct value
*v
= const_expr (pc
);
1403 error ("Right operand of `@' must be a constant, in agent expressions.");
1404 if (v
->type
->code
!= TYPE_CODE_INT
)
1405 error ("Right operand of `@' must be an integer.");
1406 length
= value_as_long (v
);
1408 error ("Right operand of `@' must be positive.");
1410 /* The top of the stack is already the address of the object, so
1411 all we need to do is frob the type of the lvalue. */
1413 /* FIXME-type-allocation: need a way to free this type when we are
1416 = create_range_type (0, builtin_type_int
, 0, length
- 1);
1417 struct type
*array
= create_array_type (0, value1
.type
, range
);
1419 value
->kind
= axs_lvalue_memory
;
1420 value
->type
= array
;
1426 /* Emit code for the `sizeof' operator.
1427 *PC should point at the start of the operand expression; we advance it
1428 to the first instruction after the operand. */
1430 gen_sizeof (union exp_element
**pc
, struct agent_expr
*ax
,
1431 struct axs_value
*value
)
1433 /* We don't care about the value of the operand expression; we only
1434 care about its type. However, in the current arrangement, the
1435 only way to find an expression's type is to generate code for it.
1436 So we generate code for the operand, and then throw it away,
1437 replacing it with code that simply pushes its size. */
1438 int start
= ax
->len
;
1439 gen_expr (pc
, ax
, value
);
1441 /* Throw away the code we just generated. */
1444 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
1445 value
->kind
= axs_rvalue
;
1446 value
->type
= builtin_type_int
;
1450 /* Generating bytecode from GDB expressions: general recursive thingy */
1452 /* A gen_expr function written by a Gen-X'er guy.
1453 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1455 gen_expr (union exp_element
**pc
, struct agent_expr
*ax
,
1456 struct axs_value
*value
)
1458 /* Used to hold the descriptions of operand expressions. */
1459 struct axs_value value1
, value2
;
1460 enum exp_opcode op
= (*pc
)[0].opcode
;
1462 /* If we're looking at a constant expression, just push its value. */
1464 struct value
*v
= maybe_const_expr (pc
);
1468 ax_const_l (ax
, value_as_long (v
));
1469 value
->kind
= axs_rvalue
;
1470 value
->type
= check_typedef (VALUE_TYPE (v
));
1475 /* Otherwise, go ahead and generate code for it. */
1478 /* Binary arithmetic operators. */
1484 case BINOP_SUBSCRIPT
:
1485 case BINOP_BITWISE_AND
:
1486 case BINOP_BITWISE_IOR
:
1487 case BINOP_BITWISE_XOR
:
1489 gen_expr (pc
, ax
, &value1
);
1490 gen_usual_unary (ax
, &value1
);
1491 gen_expr (pc
, ax
, &value2
);
1492 gen_usual_unary (ax
, &value2
);
1493 gen_usual_arithmetic (ax
, &value1
, &value2
);
1497 gen_add (ax
, value
, &value1
, &value2
, "addition");
1500 gen_sub (ax
, value
, &value1
, &value2
);
1503 gen_binop (ax
, value
, &value1
, &value2
,
1504 aop_mul
, aop_mul
, 1, "multiplication");
1507 gen_binop (ax
, value
, &value1
, &value2
,
1508 aop_div_signed
, aop_div_unsigned
, 1, "division");
1511 gen_binop (ax
, value
, &value1
, &value2
,
1512 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
1514 case BINOP_SUBSCRIPT
:
1515 gen_add (ax
, value
, &value1
, &value2
, "array subscripting");
1516 if (TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1517 error ("Illegal combination of types in array subscripting.");
1518 gen_deref (ax
, value
);
1520 case BINOP_BITWISE_AND
:
1521 gen_binop (ax
, value
, &value1
, &value2
,
1522 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
1525 case BINOP_BITWISE_IOR
:
1526 gen_binop (ax
, value
, &value1
, &value2
,
1527 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
1530 case BINOP_BITWISE_XOR
:
1531 gen_binop (ax
, value
, &value1
, &value2
,
1532 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
1536 /* We should only list operators in the outer case statement
1537 that we actually handle in the inner case statement. */
1538 internal_error ("ax-gdb.c (gen_expr): op case sets don't match");
1542 /* Note that we need to be a little subtle about generating code
1543 for comma. In C, we can do some optimizations here because
1544 we know the left operand is only being evaluated for effect.
1545 However, if the tracing kludge is in effect, then we always
1546 need to evaluate the left hand side fully, so that all the
1547 variables it mentions get traced. */
1550 gen_expr (pc
, ax
, &value1
);
1551 /* Don't just dispose of the left operand. We might be tracing,
1552 in which case we want to emit code to trace it if it's an
1554 gen_traced_pop (ax
, &value1
);
1555 gen_expr (pc
, ax
, value
);
1556 /* It's the consumer's responsibility to trace the right operand. */
1559 case OP_LONG
: /* some integer constant */
1561 struct type
*type
= (*pc
)[1].type
;
1562 LONGEST k
= (*pc
)[2].longconst
;
1564 gen_int_literal (ax
, value
, k
, type
);
1569 gen_var_ref (ax
, value
, (*pc
)[2].symbol
);
1575 int reg
= (int) (*pc
)[1].longconst
;
1577 value
->kind
= axs_lvalue_register
;
1579 value
->type
= REGISTER_VIRTUAL_TYPE (reg
);
1583 case OP_INTERNALVAR
:
1584 error ("GDB agent expressions cannot use convenience variables.");
1586 /* Weirdo operator: see comments for gen_repeat for details. */
1588 /* Note that gen_repeat handles its own argument evaluation. */
1590 gen_repeat (pc
, ax
, value
);
1595 struct type
*type
= (*pc
)[1].type
;
1597 gen_expr (pc
, ax
, value
);
1598 gen_cast (ax
, value
, type
);
1604 struct type
*type
= check_typedef ((*pc
)[1].type
);
1606 gen_expr (pc
, ax
, value
);
1607 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1608 it's just a hack for dealing with minsyms; you take some
1609 integer constant, pretend it's the address of an lvalue of
1610 the given type, and dereference it. */
1611 if (value
->kind
!= axs_rvalue
)
1612 /* This would be weird. */
1613 internal_error ("ax-gdb.c (gen_expr): OP_MEMVAL operand isn't an rvalue???");
1615 value
->kind
= axs_lvalue_memory
;
1621 /* -FOO is equivalent to 0 - FOO. */
1622 gen_int_literal (ax
, &value1
, (LONGEST
) 0, builtin_type_int
);
1623 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
1624 gen_expr (pc
, ax
, &value2
);
1625 gen_usual_unary (ax
, &value2
);
1626 gen_usual_arithmetic (ax
, &value1
, &value2
);
1627 gen_sub (ax
, value
, &value1
, &value2
);
1630 case UNOP_LOGICAL_NOT
:
1632 gen_expr (pc
, ax
, value
);
1633 gen_logical_not (ax
, value
);
1636 case UNOP_COMPLEMENT
:
1638 gen_expr (pc
, ax
, value
);
1639 gen_complement (ax
, value
);
1644 gen_expr (pc
, ax
, value
);
1645 gen_usual_unary (ax
, value
);
1646 if (TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1647 error ("Argument of unary `*' is not a pointer.");
1648 gen_deref (ax
, value
);
1653 gen_expr (pc
, ax
, value
);
1654 gen_address_of (ax
, value
);
1659 /* Notice that gen_sizeof handles its own operand, unlike most
1660 of the other unary operator functions. This is because we
1661 have to throw away the code we generate. */
1662 gen_sizeof (pc
, ax
, value
);
1665 case STRUCTOP_STRUCT
:
1668 int length
= (*pc
)[1].longconst
;
1669 char *name
= &(*pc
)[2].string
;
1671 (*pc
) += 4 + BYTES_TO_EXP_ELEM (length
+ 1);
1672 gen_expr (pc
, ax
, value
);
1673 if (op
== STRUCTOP_STRUCT
)
1674 gen_struct_ref (ax
, value
, name
, ".", "structure or union");
1675 else if (op
== STRUCTOP_PTR
)
1676 gen_struct_ref (ax
, value
, name
, "->",
1677 "pointer to a structure or union");
1679 /* If this `if' chain doesn't handle it, then the case list
1680 shouldn't mention it, and we shouldn't be here. */
1681 internal_error ("ax-gdb.c (gen_expr): unhandled struct case");
1686 error ("Attempt to use a type name as an expression.");
1689 error ("Unsupported operator in expression.");
1695 /* Generating bytecode from GDB expressions: driver */
1697 /* Given a GDB expression EXPR, produce a string of agent bytecode
1698 which computes its value. Return the agent expression, and set
1699 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1701 expr_to_agent (struct expression
*expr
, struct axs_value
*value
)
1703 struct cleanup
*old_chain
= 0;
1704 struct agent_expr
*ax
= new_agent_expr (0);
1705 union exp_element
*pc
;
1707 old_chain
= make_cleanup_free_agent_expr (ax
);
1711 gen_expr (&pc
, ax
, value
);
1713 /* We have successfully built the agent expr, so cancel the cleanup
1714 request. If we add more cleanups that we always want done, this
1715 will have to get more complicated. */
1716 discard_cleanups (old_chain
);
1721 #if 0 /* not used */
1722 /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1723 string of agent bytecode which will leave its address and size on
1724 the top of stack. Return the agent expression.
1726 Not sure this function is useful at all. */
1728 expr_to_address_and_size (struct expression
*expr
)
1730 struct axs_value value
;
1731 struct agent_expr
*ax
= expr_to_agent (expr
, &value
);
1733 /* Complain if the result is not a memory lvalue. */
1734 if (value
.kind
!= axs_lvalue_memory
)
1736 free_agent_expr (ax
);
1737 error ("Expression does not denote an object in memory.");
1740 /* Push the object's size on the stack. */
1741 ax_const_l (ax
, TYPE_LENGTH (value
.type
));
1747 /* Given a GDB expression EXPR, return bytecode to trace its value.
1748 The result will use the `trace' and `trace_quick' bytecodes to
1749 record the value of all memory touched by the expression. The
1750 caller can then use the ax_reqs function to discover which
1751 registers it relies upon. */
1753 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
)
1755 struct cleanup
*old_chain
= 0;
1756 struct agent_expr
*ax
= new_agent_expr (scope
);
1757 union exp_element
*pc
;
1758 struct axs_value value
;
1760 old_chain
= make_cleanup_free_agent_expr (ax
);
1764 gen_expr (&pc
, ax
, &value
);
1766 /* Make sure we record the final object, and get rid of it. */
1767 gen_traced_pop (ax
, &value
);
1769 /* Oh, and terminate. */
1770 ax_simple (ax
, aop_end
);
1772 /* We have successfully built the agent expr, so cancel the cleanup
1773 request. If we add more cleanups that we always want done, this
1774 will have to get more complicated. */
1775 discard_cleanups (old_chain
);
1781 /* The "agent" command, for testing: compile and disassemble an expression. */
1784 print_axs_value (struct ui_file
*f
, struct axs_value
*value
)
1786 switch (value
->kind
)
1789 fputs_filtered ("rvalue", f
);
1792 case axs_lvalue_memory
:
1793 fputs_filtered ("memory lvalue", f
);
1796 case axs_lvalue_register
:
1797 fprintf_filtered (f
, "register %d lvalue", value
->u
.reg
);
1801 fputs_filtered (" : ", f
);
1802 type_print (value
->type
, "", f
, -1);
1807 agent_command (char *exp
, int from_tty
)
1809 struct cleanup
*old_chain
= 0;
1810 struct expression
*expr
;
1811 struct agent_expr
*agent
;
1812 struct frame_info
*fi
= get_current_frame (); /* need current scope */
1814 /* We don't deal with overlay debugging at the moment. We need to
1815 think more carefully about this. If you copy this code into
1816 another command, change the error message; the user shouldn't
1817 have to know anything about agent expressions. */
1818 if (overlay_debugging
)
1819 error ("GDB can't do agent expression translation with overlays.");
1822 error_no_arg ("expression to translate");
1824 expr
= parse_expression (exp
);
1825 old_chain
= make_cleanup (free_current_contents
, &expr
);
1826 agent
= gen_trace_for_expr (fi
->pc
, expr
);
1827 make_cleanup_free_agent_expr (agent
);
1828 ax_print (gdb_stdout
, agent
);
1830 /* It would be nice to call ax_reqs here to gather some general info
1831 about the expression, and then print out the result. */
1833 do_cleanups (old_chain
);
1838 /* Initialization code. */
1840 void _initialize_ax_gdb (void);
1842 _initialize_ax_gdb (void)
1844 add_cmd ("agent", class_maintenance
, agent_command
,
1845 "Translate an expression into remote agent bytecode.",