1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "expression.h"
33 #include "gdb_string.h"
37 /* To make sense of this file, you should read doc/agentexpr.texi.
38 Then look at the types and enums in ax-gdb.h. For the code itself,
39 look at gen_expr, towards the bottom; that's the main function that
40 looks at the GDB expressions and calls everything else to generate
43 I'm beginning to wonder whether it wouldn't be nicer to internally
44 generate trees, with types, and then spit out the bytecode in
45 linear form afterwards; we could generate fewer `swap', `ext', and
46 `zero_ext' bytecodes that way; it would make good constant folding
47 easier, too. But at the moment, I think we should be willing to
48 pay for the simplicity of this code with less-than-optimal bytecode
51 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
55 /* Prototypes for local functions. */
57 /* There's a standard order to the arguments of these functions:
58 union exp_element ** --- pointer into expression
59 struct agent_expr * --- agent expression buffer to generate code into
60 struct axs_value * --- describes value left on top of stack */
62 static struct value
*const_var_ref (struct symbol
*var
);
63 static struct value
*const_expr (union exp_element
**pc
);
64 static struct value
*maybe_const_expr (union exp_element
**pc
);
66 static void gen_traced_pop (struct agent_expr
*, struct axs_value
*);
68 static void gen_sign_extend (struct agent_expr
*, struct type
*);
69 static void gen_extend (struct agent_expr
*, struct type
*);
70 static void gen_fetch (struct agent_expr
*, struct type
*);
71 static void gen_left_shift (struct agent_expr
*, int);
74 static void gen_frame_args_address (struct agent_expr
*);
75 static void gen_frame_locals_address (struct agent_expr
*);
76 static void gen_offset (struct agent_expr
*ax
, int offset
);
77 static void gen_sym_offset (struct agent_expr
*, struct symbol
*);
78 static void gen_var_ref (struct agent_expr
*ax
,
79 struct axs_value
*value
, struct symbol
*var
);
82 static void gen_int_literal (struct agent_expr
*ax
,
83 struct axs_value
*value
,
84 LONGEST k
, struct type
*type
);
87 static void require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
);
88 static void gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
);
89 static int type_wider_than (struct type
*type1
, struct type
*type2
);
90 static struct type
*max_type (struct type
*type1
, struct type
*type2
);
91 static void gen_conversion (struct agent_expr
*ax
,
92 struct type
*from
, struct type
*to
);
93 static int is_nontrivial_conversion (struct type
*from
, struct type
*to
);
94 static void gen_usual_arithmetic (struct agent_expr
*ax
,
95 struct axs_value
*value1
,
96 struct axs_value
*value2
);
97 static void gen_integral_promotions (struct agent_expr
*ax
,
98 struct axs_value
*value
);
99 static void gen_cast (struct agent_expr
*ax
,
100 struct axs_value
*value
, struct type
*type
);
101 static void gen_scale (struct agent_expr
*ax
,
102 enum agent_op op
, struct type
*type
);
103 static void gen_add (struct agent_expr
*ax
,
104 struct axs_value
*value
,
105 struct axs_value
*value1
,
106 struct axs_value
*value2
, char *name
);
107 static void gen_sub (struct agent_expr
*ax
,
108 struct axs_value
*value
,
109 struct axs_value
*value1
, struct axs_value
*value2
);
110 static void gen_binop (struct agent_expr
*ax
,
111 struct axs_value
*value
,
112 struct axs_value
*value1
,
113 struct axs_value
*value2
,
115 enum agent_op op_unsigned
, int may_carry
, char *name
);
116 static void gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
);
117 static void gen_complement (struct agent_expr
*ax
, struct axs_value
*value
);
118 static void gen_deref (struct agent_expr
*, struct axs_value
*);
119 static void gen_address_of (struct agent_expr
*, struct axs_value
*);
120 static int find_field (struct type
*type
, char *name
);
121 static void gen_bitfield_ref (struct agent_expr
*ax
,
122 struct axs_value
*value
,
123 struct type
*type
, int start
, int end
);
124 static void gen_struct_ref (struct agent_expr
*ax
,
125 struct axs_value
*value
,
127 char *operator_name
, char *operand_name
);
128 static void gen_repeat (union exp_element
**pc
,
129 struct agent_expr
*ax
, struct axs_value
*value
);
130 static void gen_sizeof (union exp_element
**pc
,
131 struct agent_expr
*ax
, struct axs_value
*value
);
132 static void gen_expr (union exp_element
**pc
,
133 struct agent_expr
*ax
, struct axs_value
*value
);
135 static void agent_command (char *exp
, int from_tty
);
138 /* Detecting constant expressions. */
140 /* If the variable reference at *PC is a constant, return its value.
141 Otherwise, return zero.
143 Hey, Wally! How can a variable reference be a constant?
145 Well, Beav, this function really handles the OP_VAR_VALUE operator,
146 not specifically variable references. GDB uses OP_VAR_VALUE to
147 refer to any kind of symbolic reference: function names, enum
148 elements, and goto labels are all handled through the OP_VAR_VALUE
149 operator, even though they're constants. It makes sense given the
152 Gee, Wally, don'cha wonder sometimes if data representations that
153 subvert commonly accepted definitions of terms in favor of heavily
154 context-specific interpretations are really just a tool of the
155 programming hegemony to preserve their power and exclude the
158 static struct value
*
159 const_var_ref (struct symbol
*var
)
161 struct type
*type
= SYMBOL_TYPE (var
);
163 switch (SYMBOL_CLASS (var
))
166 return value_from_longest (type
, (LONGEST
) SYMBOL_VALUE (var
));
169 return value_from_pointer (type
, (CORE_ADDR
) SYMBOL_VALUE_ADDRESS (var
));
177 /* If the expression starting at *PC has a constant value, return it.
178 Otherwise, return zero. If we return a value, then *PC will be
179 advanced to the end of it. If we return zero, *PC could be
181 static struct value
*
182 const_expr (union exp_element
**pc
)
184 enum exp_opcode op
= (*pc
)->opcode
;
191 struct type
*type
= (*pc
)[1].type
;
192 LONGEST k
= (*pc
)[2].longconst
;
194 return value_from_longest (type
, k
);
199 struct value
*v
= const_var_ref ((*pc
)[2].symbol
);
204 /* We could add more operators in here. */
208 v1
= const_expr (pc
);
210 return value_neg (v1
);
220 /* Like const_expr, but guarantee also that *PC is undisturbed if the
221 expression is not constant. */
222 static struct value
*
223 maybe_const_expr (union exp_element
**pc
)
225 union exp_element
*tentative_pc
= *pc
;
226 struct value
*v
= const_expr (&tentative_pc
);
228 /* If we got a value, then update the real PC. */
236 /* Generating bytecode from GDB expressions: general assumptions */
238 /* Here are a few general assumptions made throughout the code; if you
239 want to make a change that contradicts one of these, then you'd
240 better scan things pretty thoroughly.
242 - We assume that all values occupy one stack element. For example,
243 sometimes we'll swap to get at the left argument to a binary
244 operator. If we decide that void values should occupy no stack
245 elements, or that synthetic arrays (whose size is determined at
246 run time, created by the `@' operator) should occupy two stack
247 elements (address and length), then this will cause trouble.
249 - We assume the stack elements are infinitely wide, and that we
250 don't have to worry what happens if the user requests an
251 operation that is wider than the actual interpreter's stack.
252 That is, it's up to the interpreter to handle directly all the
253 integer widths the user has access to. (Woe betide the language
256 - We don't support side effects. Thus, we don't have to worry about
257 GCC's generalized lvalues, function calls, etc.
259 - We don't support floating point. Many places where we switch on
260 some type don't bother to include cases for floating point; there
261 may be even more subtle ways this assumption exists. For
262 example, the arguments to % must be integers.
264 - We assume all subexpressions have a static, unchanging type. If
265 we tried to support convenience variables, this would be a
268 - All values on the stack should always be fully zero- or
271 (I wasn't sure whether to choose this or its opposite --- that
272 only addresses are assumed extended --- but it turns out that
273 neither convention completely eliminates spurious extend
274 operations (if everything is always extended, then you have to
275 extend after add, because it could overflow; if nothing is
276 extended, then you end up producing extends whenever you change
277 sizes), and this is simpler.) */
280 /* Generating bytecode from GDB expressions: the `trace' kludge */
282 /* The compiler in this file is a general-purpose mechanism for
283 translating GDB expressions into bytecode. One ought to be able to
284 find a million and one uses for it.
286 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
287 of expediency. Let he who is without sin cast the first stone.
289 For the data tracing facility, we need to insert `trace' bytecodes
290 before each data fetch; this records all the memory that the
291 expression touches in the course of evaluation, so that memory will
292 be available when the user later tries to evaluate the expression
295 This should be done (I think) in a post-processing pass, that walks
296 an arbitrary agent expression and inserts `trace' operations at the
297 appropriate points. But it's much faster to just hack them
298 directly into the code. And since we're in a crunch, that's what
301 Setting the flag trace_kludge to non-zero enables the code that
302 emits the trace bytecodes at the appropriate points. */
303 static int trace_kludge
;
305 /* Trace the lvalue on the stack, if it needs it. In either case, pop
306 the value. Useful on the left side of a comma, and at the end of
307 an expression being used for tracing. */
309 gen_traced_pop (struct agent_expr
*ax
, struct axs_value
*value
)
315 /* We don't trace rvalues, just the lvalues necessary to
316 produce them. So just dispose of this value. */
317 ax_simple (ax
, aop_pop
);
320 case axs_lvalue_memory
:
322 int length
= TYPE_LENGTH (check_typedef (value
->type
));
324 /* There's no point in trying to use a trace_quick bytecode
325 here, since "trace_quick SIZE pop" is three bytes, whereas
326 "const8 SIZE trace" is also three bytes, does the same
327 thing, and the simplest code which generates that will also
328 work correctly for objects with large sizes. */
329 ax_const_l (ax
, length
);
330 ax_simple (ax
, aop_trace
);
334 case axs_lvalue_register
:
335 /* We need to mention the register somewhere in the bytecode,
336 so ax_reqs will pick it up and add it to the mask of
338 ax_reg (ax
, value
->u
.reg
);
339 ax_simple (ax
, aop_pop
);
343 /* If we're not tracing, just pop the value. */
344 ax_simple (ax
, aop_pop
);
349 /* Generating bytecode from GDB expressions: helper functions */
351 /* Assume that the lower bits of the top of the stack is a value of
352 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
354 gen_sign_extend (struct agent_expr
*ax
, struct type
*type
)
356 /* Do we need to sign-extend this? */
357 if (!TYPE_UNSIGNED (type
))
358 ax_ext (ax
, TYPE_LENGTH (type
) * TARGET_CHAR_BIT
);
362 /* Assume the lower bits of the top of the stack hold a value of type
363 TYPE, and the upper bits are garbage. Sign-extend or truncate as
366 gen_extend (struct agent_expr
*ax
, struct type
*type
)
368 int bits
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
370 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, bits
));
374 /* Assume that the top of the stack contains a value of type "pointer
375 to TYPE"; generate code to fetch its value. Note that TYPE is the
376 target type, not the pointer type. */
378 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
382 /* Record the area of memory we're about to fetch. */
383 ax_trace_quick (ax
, TYPE_LENGTH (type
));
386 switch (TYPE_CODE (type
))
392 /* It's a scalar value, so we know how to dereference it. How
393 many bytes long is it? */
394 switch (TYPE_LENGTH (type
))
396 case 8 / TARGET_CHAR_BIT
:
397 ax_simple (ax
, aop_ref8
);
399 case 16 / TARGET_CHAR_BIT
:
400 ax_simple (ax
, aop_ref16
);
402 case 32 / TARGET_CHAR_BIT
:
403 ax_simple (ax
, aop_ref32
);
405 case 64 / TARGET_CHAR_BIT
:
406 ax_simple (ax
, aop_ref64
);
409 /* Either our caller shouldn't have asked us to dereference
410 that pointer (other code's fault), or we're not
411 implementing something we should be (this code's fault).
412 In any case, it's a bug the user shouldn't see. */
414 internal_error (__FILE__
, __LINE__
,
415 _("gen_fetch: strange size"));
418 gen_sign_extend (ax
, type
);
422 /* Either our caller shouldn't have asked us to dereference that
423 pointer (other code's fault), or we're not implementing
424 something we should be (this code's fault). In any case,
425 it's a bug the user shouldn't see. */
426 internal_error (__FILE__
, __LINE__
,
427 _("gen_fetch: bad type code"));
432 /* Generate code to left shift the top of the stack by DISTANCE bits, or
433 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
434 unsigned (logical) right shifts. */
436 gen_left_shift (struct agent_expr
*ax
, int distance
)
440 ax_const_l (ax
, distance
);
441 ax_simple (ax
, aop_lsh
);
443 else if (distance
< 0)
445 ax_const_l (ax
, -distance
);
446 ax_simple (ax
, aop_rsh_unsigned
);
452 /* Generating bytecode from GDB expressions: symbol references */
454 /* Generate code to push the base address of the argument portion of
455 the top stack frame. */
457 gen_frame_args_address (struct agent_expr
*ax
)
460 LONGEST frame_offset
;
462 gdbarch_virtual_frame_pointer (current_gdbarch
,
463 ax
->scope
, &frame_reg
, &frame_offset
);
464 ax_reg (ax
, frame_reg
);
465 gen_offset (ax
, frame_offset
);
469 /* Generate code to push the base address of the locals portion of the
472 gen_frame_locals_address (struct agent_expr
*ax
)
475 LONGEST frame_offset
;
477 gdbarch_virtual_frame_pointer (current_gdbarch
,
478 ax
->scope
, &frame_reg
, &frame_offset
);
479 ax_reg (ax
, frame_reg
);
480 gen_offset (ax
, frame_offset
);
484 /* Generate code to add OFFSET to the top of the stack. Try to
485 generate short and readable code. We use this for getting to
486 variables on the stack, and structure members. If we were
487 programming in ML, it would be clearer why these are the same
490 gen_offset (struct agent_expr
*ax
, int offset
)
492 /* It would suffice to simply push the offset and add it, but this
493 makes it easier to read positive and negative offsets in the
497 ax_const_l (ax
, offset
);
498 ax_simple (ax
, aop_add
);
502 ax_const_l (ax
, -offset
);
503 ax_simple (ax
, aop_sub
);
508 /* In many cases, a symbol's value is the offset from some other
509 address (stack frame, base register, etc.) Generate code to add
510 VAR's value to the top of the stack. */
512 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
514 gen_offset (ax
, SYMBOL_VALUE (var
));
518 /* Generate code for a variable reference to AX. The variable is the
519 symbol VAR. Set VALUE to describe the result. */
522 gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
, struct symbol
*var
)
524 /* Dereference any typedefs. */
525 value
->type
= check_typedef (SYMBOL_TYPE (var
));
527 /* I'm imitating the code in read_var_value. */
528 switch (SYMBOL_CLASS (var
))
530 case LOC_CONST
: /* A constant, like an enum value. */
531 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE (var
));
532 value
->kind
= axs_rvalue
;
535 case LOC_LABEL
: /* A goto label, being used as a value. */
536 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
537 value
->kind
= axs_rvalue
;
540 case LOC_CONST_BYTES
:
541 internal_error (__FILE__
, __LINE__
,
542 _("gen_var_ref: LOC_CONST_BYTES symbols are not supported"));
544 /* Variable at a fixed location in memory. Easy. */
546 /* Push the address of the variable. */
547 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (var
));
548 value
->kind
= axs_lvalue_memory
;
551 case LOC_ARG
: /* var lives in argument area of frame */
552 gen_frame_args_address (ax
);
553 gen_sym_offset (ax
, var
);
554 value
->kind
= axs_lvalue_memory
;
557 case LOC_REF_ARG
: /* As above, but the frame slot really
558 holds the address of the variable. */
559 gen_frame_args_address (ax
);
560 gen_sym_offset (ax
, var
);
561 /* Don't assume any particular pointer size. */
562 gen_fetch (ax
, lookup_pointer_type (builtin_type_void
));
563 value
->kind
= axs_lvalue_memory
;
566 case LOC_LOCAL
: /* var lives in locals area of frame */
567 gen_frame_locals_address (ax
);
568 gen_sym_offset (ax
, var
);
569 value
->kind
= axs_lvalue_memory
;
573 error (_("Cannot compute value of typedef `%s'."),
574 SYMBOL_PRINT_NAME (var
));
578 ax_const_l (ax
, BLOCK_START (SYMBOL_BLOCK_VALUE (var
)));
579 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
592 because it's just like any other case where the thing
593 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 (DEPRECATED_SYMBOL_NAME (var
), NULL
, NULL
);
604 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var
));
606 /* Push the address of the variable. */
607 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (msym
));
608 value
->kind
= axs_lvalue_memory
;
613 /* FIXME: cagney/2004-01-26: It should be possible to
614 unconditionally call the SYMBOL_OPS method when available.
615 Unfortunately DWARF 2 stores the frame-base (instead of the
616 function) location in a function's symbol. Oops! For the
617 moment enable this when/where applicable. */
618 SYMBOL_OPS (var
)->tracepoint_var_ref (var
, ax
, value
);
621 case LOC_OPTIMIZED_OUT
:
622 error (_("The variable `%s' has been optimized out."),
623 SYMBOL_PRINT_NAME (var
));
627 error (_("Cannot find value of botched symbol `%s'."),
628 SYMBOL_PRINT_NAME (var
));
635 /* Generating bytecode from GDB expressions: literals */
638 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
642 value
->kind
= axs_rvalue
;
643 value
->type
= check_typedef (type
);
648 /* Generating bytecode from GDB expressions: unary conversions, casts */
650 /* Take what's on the top of the stack (as described by VALUE), and
651 try to make an rvalue out of it. Signal an error if we can't do
654 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
659 /* It's already an rvalue. */
662 case axs_lvalue_memory
:
663 /* The top of stack is the address of the object. Dereference. */
664 gen_fetch (ax
, value
->type
);
667 case axs_lvalue_register
:
668 /* There's nothing on the stack, but value->u.reg is the
669 register number containing the value.
671 When we add floating-point support, this is going to have to
672 change. What about SPARC register pairs, for example? */
673 ax_reg (ax
, value
->u
.reg
);
674 gen_extend (ax
, value
->type
);
678 value
->kind
= axs_rvalue
;
682 /* Assume the top of the stack is described by VALUE, and perform the
683 usual unary conversions. This is motivated by ANSI 6.2.2, but of
684 course GDB expressions are not ANSI; they're the mishmash union of
685 a bunch of languages. Rah.
687 NOTE! This function promises to produce an rvalue only when the
688 incoming value is of an appropriate type. In other words, the
689 consumer of the value this function produces may assume the value
690 is an rvalue only after checking its type.
692 The immediate issue is that if the user tries to use a structure or
693 union as an operand of, say, the `+' operator, we don't want to try
694 to convert that structure to an rvalue; require_rvalue will bomb on
695 structs and unions. Rather, we want to simply pass the struct
696 lvalue through unchanged, and let `+' raise an error. */
699 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
701 /* We don't have to generate any code for the usual integral
702 conversions, since values are always represented as full-width on
703 the stack. Should we tweak the type? */
705 /* Some types require special handling. */
706 switch (TYPE_CODE (value
->type
))
708 /* Functions get converted to a pointer to the function. */
710 value
->type
= lookup_pointer_type (value
->type
);
711 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
714 /* Arrays get converted to a pointer to their first element, and
715 are no longer an lvalue. */
716 case TYPE_CODE_ARRAY
:
718 struct type
*elements
= TYPE_TARGET_TYPE (value
->type
);
719 value
->type
= lookup_pointer_type (elements
);
720 value
->kind
= axs_rvalue
;
721 /* We don't need to generate any code; the address of the array
722 is also the address of its first element. */
726 /* Don't try to convert structures and unions to rvalues. Let the
727 consumer signal an error. */
728 case TYPE_CODE_STRUCT
:
729 case TYPE_CODE_UNION
:
732 /* If the value is an enum, call it an integer. */
734 value
->type
= builtin_type_int
;
738 /* If the value is an lvalue, dereference it. */
739 require_rvalue (ax
, value
);
743 /* Return non-zero iff the type TYPE1 is considered "wider" than the
744 type TYPE2, according to the rules described in gen_usual_arithmetic. */
746 type_wider_than (struct type
*type1
, struct type
*type2
)
748 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)
749 || (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
750 && TYPE_UNSIGNED (type1
)
751 && !TYPE_UNSIGNED (type2
)));
755 /* Return the "wider" of the two types TYPE1 and TYPE2. */
757 max_type (struct type
*type1
, struct type
*type2
)
759 return type_wider_than (type1
, type2
) ? type1
: type2
;
763 /* Generate code to convert a scalar value of type FROM to type TO. */
765 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
767 /* Perhaps there is a more graceful way to state these rules. */
769 /* If we're converting to a narrower type, then we need to clear out
771 if (TYPE_LENGTH (to
) < TYPE_LENGTH (from
))
772 gen_extend (ax
, from
);
774 /* If the two values have equal width, but different signednesses,
775 then we need to extend. */
776 else if (TYPE_LENGTH (to
) == TYPE_LENGTH (from
))
778 if (TYPE_UNSIGNED (from
) != TYPE_UNSIGNED (to
))
782 /* If we're converting to a wider type, and becoming unsigned, then
783 we need to zero out any possible sign bits. */
784 else if (TYPE_LENGTH (to
) > TYPE_LENGTH (from
))
786 if (TYPE_UNSIGNED (to
))
792 /* Return non-zero iff the type FROM will require any bytecodes to be
793 emitted to be converted to the type TO. */
795 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
797 struct agent_expr
*ax
= new_agent_expr (0);
800 /* Actually generate the code, and see if anything came out. At the
801 moment, it would be trivial to replicate the code in
802 gen_conversion here, but in the future, when we're supporting
803 floating point and the like, it may not be. Doing things this
804 way allows this function to be independent of the logic in
806 gen_conversion (ax
, from
, to
);
807 nontrivial
= ax
->len
> 0;
808 free_agent_expr (ax
);
813 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
814 6.2.1.5) for the two operands of an arithmetic operator. This
815 effectively finds a "least upper bound" type for the two arguments,
816 and promotes each argument to that type. *VALUE1 and *VALUE2
817 describe the values as they are passed in, and as they are left. */
819 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
820 struct axs_value
*value2
)
822 /* Do the usual binary conversions. */
823 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
824 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
826 /* The ANSI integral promotions seem to work this way: Order the
827 integer types by size, and then by signedness: an n-bit
828 unsigned type is considered "wider" than an n-bit signed
829 type. Promote to the "wider" of the two types, and always
830 promote at least to int. */
831 struct type
*target
= max_type (builtin_type_int
,
832 max_type (value1
->type
, value2
->type
));
834 /* Deal with value2, on the top of the stack. */
835 gen_conversion (ax
, value2
->type
, target
);
837 /* Deal with value1, not on the top of the stack. Don't
838 generate the `swap' instructions if we're not actually going
840 if (is_nontrivial_conversion (value1
->type
, target
))
842 ax_simple (ax
, aop_swap
);
843 gen_conversion (ax
, value1
->type
, target
);
844 ax_simple (ax
, aop_swap
);
847 value1
->type
= value2
->type
= check_typedef (target
);
852 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
853 the value on the top of the stack, as described by VALUE. Assume
854 the value has integral type. */
856 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
858 if (!type_wider_than (value
->type
, builtin_type_int
))
860 gen_conversion (ax
, value
->type
, builtin_type_int
);
861 value
->type
= builtin_type_int
;
863 else if (!type_wider_than (value
->type
, builtin_type_unsigned_int
))
865 gen_conversion (ax
, value
->type
, builtin_type_unsigned_int
);
866 value
->type
= builtin_type_unsigned_int
;
871 /* Generate code for a cast to TYPE. */
873 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
875 /* GCC does allow casts to yield lvalues, so this should be fixed
876 before merging these changes into the trunk. */
877 require_rvalue (ax
, value
);
878 /* Dereference typedefs. */
879 type
= check_typedef (type
);
881 switch (TYPE_CODE (type
))
884 /* It's implementation-defined, and I'll bet this is what GCC
888 case TYPE_CODE_ARRAY
:
889 case TYPE_CODE_STRUCT
:
890 case TYPE_CODE_UNION
:
892 error (_("Invalid type cast: intended type must be scalar."));
895 /* We don't have to worry about the size of the value, because
896 all our integral values are fully sign-extended, and when
897 casting pointers we can do anything we like. Is there any
898 way for us to know what GCC actually does with a cast like
903 gen_conversion (ax
, value
->type
, type
);
907 /* We could pop the value, and rely on everyone else to check
908 the type and notice that this value doesn't occupy a stack
909 slot. But for now, leave the value on the stack, and
910 preserve the "value == stack element" assumption. */
914 error (_("Casts to requested type are not yet implemented."));
922 /* Generating bytecode from GDB expressions: arithmetic */
924 /* Scale the integer on the top of the stack by the size of the target
925 of the pointer type TYPE. */
927 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
929 struct type
*element
= TYPE_TARGET_TYPE (type
);
931 if (TYPE_LENGTH (element
) != 1)
933 ax_const_l (ax
, TYPE_LENGTH (element
));
939 /* Generate code for an addition; non-trivial because we deal with
940 pointer arithmetic. We set VALUE to describe the result value; we
941 assume VALUE1 and VALUE2 describe the two operands, and that
942 they've undergone the usual binary conversions. Used by both
943 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
945 gen_add (struct agent_expr
*ax
, struct axs_value
*value
,
946 struct axs_value
*value1
, struct axs_value
*value2
, char *name
)
949 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
950 && TYPE_CODE (value2
->type
) == TYPE_CODE_PTR
)
952 /* Swap the values and proceed normally. */
953 ax_simple (ax
, aop_swap
);
954 gen_scale (ax
, aop_mul
, value2
->type
);
955 ax_simple (ax
, aop_add
);
956 gen_extend (ax
, value2
->type
); /* Catch overflow. */
957 value
->type
= value2
->type
;
961 else if (TYPE_CODE (value1
->type
) == TYPE_CODE_PTR
962 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
964 gen_scale (ax
, aop_mul
, value1
->type
);
965 ax_simple (ax
, aop_add
);
966 gen_extend (ax
, value1
->type
); /* Catch overflow. */
967 value
->type
= value1
->type
;
970 /* Must be number + number; the usual binary conversions will have
971 brought them both to the same width. */
972 else if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
973 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
975 ax_simple (ax
, aop_add
);
976 gen_extend (ax
, value1
->type
); /* Catch overflow. */
977 value
->type
= value1
->type
;
981 error (_("Invalid combination of types in %s."), name
);
983 value
->kind
= axs_rvalue
;
987 /* Generate code for an addition; non-trivial because we have to deal
988 with pointer arithmetic. We set VALUE to describe the result
989 value; we assume VALUE1 and VALUE2 describe the two operands, and
990 that they've undergone the usual binary conversions. */
992 gen_sub (struct agent_expr
*ax
, struct axs_value
*value
,
993 struct axs_value
*value1
, struct axs_value
*value2
)
995 if (TYPE_CODE (value1
->type
) == TYPE_CODE_PTR
)
997 /* Is it PTR - INT? */
998 if (TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
1000 gen_scale (ax
, aop_mul
, value1
->type
);
1001 ax_simple (ax
, aop_sub
);
1002 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1003 value
->type
= value1
->type
;
1006 /* Is it PTR - PTR? Strictly speaking, the types ought to
1007 match, but this is what the normal GDB expression evaluator
1009 else if (TYPE_CODE (value2
->type
) == TYPE_CODE_PTR
1010 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1
->type
))
1011 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2
->type
))))
1013 ax_simple (ax
, aop_sub
);
1014 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1015 value
->type
= builtin_type_long
; /* FIXME --- should be ptrdiff_t */
1019 First argument of `-' is a pointer, but second argument is neither\n\
1020 an integer nor a pointer of the same type."));
1023 /* Must be number + number. */
1024 else if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
1025 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
1027 ax_simple (ax
, aop_sub
);
1028 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1029 value
->type
= value1
->type
;
1033 error (_("Invalid combination of types in subtraction."));
1035 value
->kind
= axs_rvalue
;
1038 /* Generate code for a binary operator that doesn't do pointer magic.
1039 We set VALUE to describe the result value; we assume VALUE1 and
1040 VALUE2 describe the two operands, and that they've undergone the
1041 usual binary conversions. MAY_CARRY should be non-zero iff the
1042 result needs to be extended. NAME is the English name of the
1043 operator, used in error messages */
1045 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1046 struct axs_value
*value1
, struct axs_value
*value2
, enum agent_op op
,
1047 enum agent_op op_unsigned
, int may_carry
, char *name
)
1049 /* We only handle INT op INT. */
1050 if ((TYPE_CODE (value1
->type
) != TYPE_CODE_INT
)
1051 || (TYPE_CODE (value2
->type
) != TYPE_CODE_INT
))
1052 error (_("Invalid combination of types in %s."), name
);
1055 TYPE_UNSIGNED (value1
->type
) ? op_unsigned
: op
);
1057 gen_extend (ax
, value1
->type
); /* catch overflow */
1058 value
->type
= value1
->type
;
1059 value
->kind
= axs_rvalue
;
1064 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
)
1066 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
1067 && TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1068 error (_("Invalid type of operand to `!'."));
1070 gen_usual_unary (ax
, value
);
1071 ax_simple (ax
, aop_log_not
);
1072 value
->type
= builtin_type_int
;
1077 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1079 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
)
1080 error (_("Invalid type of operand to `~'."));
1082 gen_usual_unary (ax
, value
);
1083 gen_integral_promotions (ax
, value
);
1084 ax_simple (ax
, aop_bit_not
);
1085 gen_extend (ax
, value
->type
);
1090 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1092 /* Dereference the value on the top of the stack. */
1094 gen_deref (struct agent_expr
*ax
, struct axs_value
*value
)
1096 /* The caller should check the type, because several operators use
1097 this, and we don't know what error message to generate. */
1098 if (TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1099 internal_error (__FILE__
, __LINE__
,
1100 _("gen_deref: expected a pointer"));
1102 /* We've got an rvalue now, which is a pointer. We want to yield an
1103 lvalue, whose address is exactly that pointer. So we don't
1104 actually emit any code; we just change the type from "Pointer to
1105 T" to "T", and mark the value as an lvalue in memory. Leave it
1106 to the consumer to actually dereference it. */
1107 value
->type
= check_typedef (TYPE_TARGET_TYPE (value
->type
));
1108 value
->kind
= ((TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1109 ? axs_rvalue
: axs_lvalue_memory
);
1113 /* Produce the address of the lvalue on the top of the stack. */
1115 gen_address_of (struct agent_expr
*ax
, struct axs_value
*value
)
1117 /* Special case for taking the address of a function. The ANSI
1118 standard describes this as a special case, too, so this
1119 arrangement is not without motivation. */
1120 if (TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1121 /* The value's already an rvalue on the stack, so we just need to
1123 value
->type
= lookup_pointer_type (value
->type
);
1125 switch (value
->kind
)
1128 error (_("Operand of `&' is an rvalue, which has no address."));
1130 case axs_lvalue_register
:
1131 error (_("Operand of `&' is in a register, and has no address."));
1133 case axs_lvalue_memory
:
1134 value
->kind
= axs_rvalue
;
1135 value
->type
= lookup_pointer_type (value
->type
);
1141 /* A lot of this stuff will have to change to support C++. But we're
1142 not going to deal with that at the moment. */
1144 /* Find the field in the structure type TYPE named NAME, and return
1145 its index in TYPE's field array. */
1147 find_field (struct type
*type
, char *name
)
1151 CHECK_TYPEDEF (type
);
1153 /* Make sure this isn't C++. */
1154 if (TYPE_N_BASECLASSES (type
) != 0)
1155 internal_error (__FILE__
, __LINE__
,
1156 _("find_field: derived classes supported"));
1158 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1160 char *this_name
= TYPE_FIELD_NAME (type
, i
);
1164 if (strcmp (name
, this_name
) == 0)
1167 if (this_name
[0] == '\0')
1168 internal_error (__FILE__
, __LINE__
,
1169 _("find_field: anonymous unions not supported"));
1173 error (_("Couldn't find member named `%s' in struct/union `%s'"),
1174 name
, TYPE_TAG_NAME (type
));
1180 /* Generate code to push the value of a bitfield of a structure whose
1181 address is on the top of the stack. START and END give the
1182 starting and one-past-ending *bit* numbers of the field within the
1185 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1186 struct type
*type
, int start
, int end
)
1188 /* Note that ops[i] fetches 8 << i bits. */
1189 static enum agent_op ops
[]
1191 {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1192 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1194 /* We don't want to touch any byte that the bitfield doesn't
1195 actually occupy; we shouldn't make any accesses we're not
1196 explicitly permitted to. We rely here on the fact that the
1197 bytecode `ref' operators work on unaligned addresses.
1199 It takes some fancy footwork to get the stack to work the way
1200 we'd like. Say we're retrieving a bitfield that requires three
1201 fetches. Initially, the stack just contains the address:
1203 For the first fetch, we duplicate the address
1205 then add the byte offset, do the fetch, and shift and mask as
1206 needed, yielding a fragment of the value, properly aligned for
1207 the final bitwise or:
1209 then we swap, and repeat the process:
1210 frag1 addr --- address on top
1211 frag1 addr addr --- duplicate it
1212 frag1 addr frag2 --- get second fragment
1213 frag1 frag2 addr --- swap again
1214 frag1 frag2 frag3 --- get third fragment
1215 Notice that, since the third fragment is the last one, we don't
1216 bother duplicating the address this time. Now we have all the
1217 fragments on the stack, and we can simply `or' them together,
1218 yielding the final value of the bitfield. */
1220 /* The first and one-after-last bits in the field, but rounded down
1221 and up to byte boundaries. */
1222 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1223 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1227 /* current bit offset within the structure */
1230 /* The index in ops of the opcode we're considering. */
1233 /* The number of fragments we generated in the process. Probably
1234 equal to the number of `one' bits in bytesize, but who cares? */
1237 /* Dereference any typedefs. */
1238 type
= check_typedef (type
);
1240 /* Can we fetch the number of bits requested at all? */
1241 if ((end
- start
) > ((1 << num_ops
) * 8))
1242 internal_error (__FILE__
, __LINE__
,
1243 _("gen_bitfield_ref: bitfield too wide"));
1245 /* Note that we know here that we only need to try each opcode once.
1246 That may not be true on machines with weird byte sizes. */
1247 offset
= bound_start
;
1249 for (op
= num_ops
- 1; op
>= 0; op
--)
1251 /* number of bits that ops[op] would fetch */
1252 int op_size
= 8 << op
;
1254 /* The stack at this point, from bottom to top, contains zero or
1255 more fragments, then the address. */
1257 /* Does this fetch fit within the bitfield? */
1258 if (offset
+ op_size
<= bound_end
)
1260 /* Is this the last fragment? */
1261 int last_frag
= (offset
+ op_size
== bound_end
);
1264 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1266 /* Add the offset. */
1267 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1271 /* Record the area of memory we're about to fetch. */
1272 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1275 /* Perform the fetch. */
1276 ax_simple (ax
, ops
[op
]);
1278 /* Shift the bits we have to their proper position.
1279 gen_left_shift will generate right shifts when the operand
1282 A big-endian field diagram to ponder:
1283 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1284 +------++------++------++------++------++------++------++------+
1285 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1287 bit number 16 32 48 53
1288 These are bit numbers as supplied by GDB. Note that the
1289 bit numbers run from right to left once you've fetched the
1292 A little-endian field diagram to ponder:
1293 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1294 +------++------++------++------++------++------++------++------+
1295 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1297 bit number 48 32 16 4 0
1299 In both cases, the most significant end is on the left
1300 (i.e. normal numeric writing order), which means that you
1301 don't go crazy thinking about `left' and `right' shifts.
1303 We don't have to worry about masking yet:
1304 - If they contain garbage off the least significant end, then we
1305 must be looking at the low end of the field, and the right
1306 shift will wipe them out.
1307 - If they contain garbage off the most significant end, then we
1308 must be looking at the most significant end of the word, and
1309 the sign/zero extension will wipe them out.
1310 - If we're in the interior of the word, then there is no garbage
1311 on either end, because the ref operators zero-extend. */
1312 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
1313 gen_left_shift (ax
, end
- (offset
+ op_size
));
1315 gen_left_shift (ax
, offset
- start
);
1318 /* Bring the copy of the address up to the top. */
1319 ax_simple (ax
, aop_swap
);
1326 /* Generate enough bitwise `or' operations to combine all the
1327 fragments we left on the stack. */
1328 while (fragment_count
-- > 1)
1329 ax_simple (ax
, aop_bit_or
);
1331 /* Sign- or zero-extend the value as appropriate. */
1332 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1334 /* This is *not* an lvalue. Ugh. */
1335 value
->kind
= axs_rvalue
;
1340 /* Generate code to reference the member named FIELD of a structure or
1341 union. The top of the stack, as described by VALUE, should have
1342 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1343 the operator being compiled, and OPERAND_NAME is the kind of thing
1344 it operates on; we use them in error messages. */
1346 gen_struct_ref (struct agent_expr
*ax
, struct axs_value
*value
, char *field
,
1347 char *operator_name
, char *operand_name
)
1352 /* Follow pointers until we reach a non-pointer. These aren't the C
1353 semantics, but they're what the normal GDB evaluator does, so we
1354 should at least be consistent. */
1355 while (TYPE_CODE (value
->type
) == TYPE_CODE_PTR
)
1357 gen_usual_unary (ax
, value
);
1358 gen_deref (ax
, value
);
1360 type
= check_typedef (value
->type
);
1362 /* This must yield a structure or a union. */
1363 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
1364 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
1365 error (_("The left operand of `%s' is not a %s."),
1366 operator_name
, operand_name
);
1368 /* And it must be in memory; we don't deal with structure rvalues,
1369 or structures living in registers. */
1370 if (value
->kind
!= axs_lvalue_memory
)
1371 error (_("Structure does not live in memory."));
1373 i
= find_field (type
, field
);
1375 /* Is this a bitfield? */
1376 if (TYPE_FIELD_PACKED (type
, i
))
1377 gen_bitfield_ref (ax
, value
, TYPE_FIELD_TYPE (type
, i
),
1378 TYPE_FIELD_BITPOS (type
, i
),
1379 (TYPE_FIELD_BITPOS (type
, i
)
1380 + TYPE_FIELD_BITSIZE (type
, i
)));
1383 gen_offset (ax
, TYPE_FIELD_BITPOS (type
, i
) / TARGET_CHAR_BIT
);
1384 value
->kind
= axs_lvalue_memory
;
1385 value
->type
= TYPE_FIELD_TYPE (type
, i
);
1390 /* Generate code for GDB's magical `repeat' operator.
1391 LVALUE @ INT creates an array INT elements long, and whose elements
1392 have the same type as LVALUE, located in memory so that LVALUE is
1393 its first element. For example, argv[0]@argc gives you the array
1394 of command-line arguments.
1396 Unfortunately, because we have to know the types before we actually
1397 have a value for the expression, we can't implement this perfectly
1398 without changing the type system, having values that occupy two
1399 stack slots, doing weird things with sizeof, etc. So we require
1400 the right operand to be a constant expression. */
1402 gen_repeat (union exp_element
**pc
, struct agent_expr
*ax
,
1403 struct axs_value
*value
)
1405 struct axs_value value1
;
1406 /* We don't want to turn this into an rvalue, so no conversions
1408 gen_expr (pc
, ax
, &value1
);
1409 if (value1
.kind
!= axs_lvalue_memory
)
1410 error (_("Left operand of `@' must be an object in memory."));
1412 /* Evaluate the length; it had better be a constant. */
1414 struct value
*v
= const_expr (pc
);
1418 error (_("Right operand of `@' must be a constant, in agent expressions."));
1419 if (TYPE_CODE (value_type (v
)) != TYPE_CODE_INT
)
1420 error (_("Right operand of `@' must be an integer."));
1421 length
= value_as_long (v
);
1423 error (_("Right operand of `@' must be positive."));
1425 /* The top of the stack is already the address of the object, so
1426 all we need to do is frob the type of the lvalue. */
1428 /* FIXME-type-allocation: need a way to free this type when we are
1431 = create_range_type (0, builtin_type_int
, 0, length
- 1);
1432 struct type
*array
= create_array_type (0, value1
.type
, range
);
1434 value
->kind
= axs_lvalue_memory
;
1435 value
->type
= array
;
1441 /* Emit code for the `sizeof' operator.
1442 *PC should point at the start of the operand expression; we advance it
1443 to the first instruction after the operand. */
1445 gen_sizeof (union exp_element
**pc
, struct agent_expr
*ax
,
1446 struct axs_value
*value
)
1448 /* We don't care about the value of the operand expression; we only
1449 care about its type. However, in the current arrangement, the
1450 only way to find an expression's type is to generate code for it.
1451 So we generate code for the operand, and then throw it away,
1452 replacing it with code that simply pushes its size. */
1453 int start
= ax
->len
;
1454 gen_expr (pc
, ax
, value
);
1456 /* Throw away the code we just generated. */
1459 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
1460 value
->kind
= axs_rvalue
;
1461 value
->type
= builtin_type_int
;
1465 /* Generating bytecode from GDB expressions: general recursive thingy */
1468 /* A gen_expr function written by a Gen-X'er guy.
1469 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1471 gen_expr (union exp_element
**pc
, struct agent_expr
*ax
,
1472 struct axs_value
*value
)
1474 /* Used to hold the descriptions of operand expressions. */
1475 struct axs_value value1
, value2
;
1476 enum exp_opcode op
= (*pc
)[0].opcode
;
1478 /* If we're looking at a constant expression, just push its value. */
1480 struct value
*v
= maybe_const_expr (pc
);
1484 ax_const_l (ax
, value_as_long (v
));
1485 value
->kind
= axs_rvalue
;
1486 value
->type
= check_typedef (value_type (v
));
1491 /* Otherwise, go ahead and generate code for it. */
1494 /* Binary arithmetic operators. */
1500 case BINOP_SUBSCRIPT
:
1501 case BINOP_BITWISE_AND
:
1502 case BINOP_BITWISE_IOR
:
1503 case BINOP_BITWISE_XOR
:
1505 gen_expr (pc
, ax
, &value1
);
1506 gen_usual_unary (ax
, &value1
);
1507 gen_expr (pc
, ax
, &value2
);
1508 gen_usual_unary (ax
, &value2
);
1509 gen_usual_arithmetic (ax
, &value1
, &value2
);
1513 gen_add (ax
, value
, &value1
, &value2
, "addition");
1516 gen_sub (ax
, value
, &value1
, &value2
);
1519 gen_binop (ax
, value
, &value1
, &value2
,
1520 aop_mul
, aop_mul
, 1, "multiplication");
1523 gen_binop (ax
, value
, &value1
, &value2
,
1524 aop_div_signed
, aop_div_unsigned
, 1, "division");
1527 gen_binop (ax
, value
, &value1
, &value2
,
1528 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
1530 case BINOP_SUBSCRIPT
:
1531 gen_add (ax
, value
, &value1
, &value2
, "array subscripting");
1532 if (TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1533 error (_("Invalid combination of types in array subscripting."));
1534 gen_deref (ax
, value
);
1536 case BINOP_BITWISE_AND
:
1537 gen_binop (ax
, value
, &value1
, &value2
,
1538 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
1541 case BINOP_BITWISE_IOR
:
1542 gen_binop (ax
, value
, &value1
, &value2
,
1543 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
1546 case BINOP_BITWISE_XOR
:
1547 gen_binop (ax
, value
, &value1
, &value2
,
1548 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
1552 /* We should only list operators in the outer case statement
1553 that we actually handle in the inner case statement. */
1554 internal_error (__FILE__
, __LINE__
,
1555 _("gen_expr: op case sets don't match"));
1559 /* Note that we need to be a little subtle about generating code
1560 for comma. In C, we can do some optimizations here because
1561 we know the left operand is only being evaluated for effect.
1562 However, if the tracing kludge is in effect, then we always
1563 need to evaluate the left hand side fully, so that all the
1564 variables it mentions get traced. */
1567 gen_expr (pc
, ax
, &value1
);
1568 /* Don't just dispose of the left operand. We might be tracing,
1569 in which case we want to emit code to trace it if it's an
1571 gen_traced_pop (ax
, &value1
);
1572 gen_expr (pc
, ax
, value
);
1573 /* It's the consumer's responsibility to trace the right operand. */
1576 case OP_LONG
: /* some integer constant */
1578 struct type
*type
= (*pc
)[1].type
;
1579 LONGEST k
= (*pc
)[2].longconst
;
1581 gen_int_literal (ax
, value
, k
, type
);
1586 gen_var_ref (ax
, value
, (*pc
)[2].symbol
);
1592 const char *name
= &(*pc
)[2].string
;
1594 (*pc
) += 4 + BYTES_TO_EXP_ELEM ((*pc
)[1].longconst
+ 1);
1595 reg
= frame_map_name_to_regnum (deprecated_safe_get_selected_frame (),
1596 name
, strlen (name
));
1598 internal_error (__FILE__
, __LINE__
,
1599 _("Register $%s not available"), name
);
1600 if (reg
>= gdbarch_num_regs (current_gdbarch
))
1601 error (_("'%s' is a pseudo-register; "
1602 "GDB cannot yet trace pseudoregister contents."),
1604 value
->kind
= axs_lvalue_register
;
1606 value
->type
= register_type (current_gdbarch
, reg
);
1610 case OP_INTERNALVAR
:
1611 error (_("GDB agent expressions cannot use convenience variables."));
1613 /* Weirdo operator: see comments for gen_repeat for details. */
1615 /* Note that gen_repeat handles its own argument evaluation. */
1617 gen_repeat (pc
, ax
, value
);
1622 struct type
*type
= (*pc
)[1].type
;
1624 gen_expr (pc
, ax
, value
);
1625 gen_cast (ax
, value
, type
);
1631 struct type
*type
= check_typedef ((*pc
)[1].type
);
1633 gen_expr (pc
, ax
, value
);
1634 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1635 it's just a hack for dealing with minsyms; you take some
1636 integer constant, pretend it's the address of an lvalue of
1637 the given type, and dereference it. */
1638 if (value
->kind
!= axs_rvalue
)
1639 /* This would be weird. */
1640 internal_error (__FILE__
, __LINE__
,
1641 _("gen_expr: OP_MEMVAL operand isn't an rvalue???"));
1643 value
->kind
= axs_lvalue_memory
;
1649 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
1650 gen_expr (pc
, ax
, value
);
1651 gen_usual_unary (ax
, value
);
1656 /* -FOO is equivalent to 0 - FOO. */
1657 gen_int_literal (ax
, &value1
, (LONGEST
) 0, builtin_type_int
);
1658 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
1659 gen_expr (pc
, ax
, &value2
);
1660 gen_usual_unary (ax
, &value2
);
1661 gen_usual_arithmetic (ax
, &value1
, &value2
);
1662 gen_sub (ax
, value
, &value1
, &value2
);
1665 case UNOP_LOGICAL_NOT
:
1667 gen_expr (pc
, ax
, value
);
1668 gen_logical_not (ax
, value
);
1671 case UNOP_COMPLEMENT
:
1673 gen_expr (pc
, ax
, value
);
1674 gen_complement (ax
, value
);
1679 gen_expr (pc
, ax
, value
);
1680 gen_usual_unary (ax
, value
);
1681 if (TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1682 error (_("Argument of unary `*' is not a pointer."));
1683 gen_deref (ax
, value
);
1688 gen_expr (pc
, ax
, value
);
1689 gen_address_of (ax
, value
);
1694 /* Notice that gen_sizeof handles its own operand, unlike most
1695 of the other unary operator functions. This is because we
1696 have to throw away the code we generate. */
1697 gen_sizeof (pc
, ax
, value
);
1700 case STRUCTOP_STRUCT
:
1703 int length
= (*pc
)[1].longconst
;
1704 char *name
= &(*pc
)[2].string
;
1706 (*pc
) += 4 + BYTES_TO_EXP_ELEM (length
+ 1);
1707 gen_expr (pc
, ax
, value
);
1708 if (op
== STRUCTOP_STRUCT
)
1709 gen_struct_ref (ax
, value
, name
, ".", "structure or union");
1710 else if (op
== STRUCTOP_PTR
)
1711 gen_struct_ref (ax
, value
, name
, "->",
1712 "pointer to a structure or union");
1714 /* If this `if' chain doesn't handle it, then the case list
1715 shouldn't mention it, and we shouldn't be here. */
1716 internal_error (__FILE__
, __LINE__
,
1717 _("gen_expr: unhandled struct case"));
1722 error (_("Attempt to use a type name as an expression."));
1725 error (_("Unsupported operator in expression."));
1731 /* Generating bytecode from GDB expressions: driver */
1733 /* Given a GDB expression EXPR, return bytecode to trace its value.
1734 The result will use the `trace' and `trace_quick' bytecodes to
1735 record the value of all memory touched by the expression. The
1736 caller can then use the ax_reqs function to discover which
1737 registers it relies upon. */
1739 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
)
1741 struct cleanup
*old_chain
= 0;
1742 struct agent_expr
*ax
= new_agent_expr (scope
);
1743 union exp_element
*pc
;
1744 struct axs_value value
;
1746 old_chain
= make_cleanup_free_agent_expr (ax
);
1750 gen_expr (&pc
, ax
, &value
);
1752 /* Make sure we record the final object, and get rid of it. */
1753 gen_traced_pop (ax
, &value
);
1755 /* Oh, and terminate. */
1756 ax_simple (ax
, aop_end
);
1758 /* We have successfully built the agent expr, so cancel the cleanup
1759 request. If we add more cleanups that we always want done, this
1760 will have to get more complicated. */
1761 discard_cleanups (old_chain
);
1766 agent_command (char *exp
, int from_tty
)
1768 struct cleanup
*old_chain
= 0;
1769 struct expression
*expr
;
1770 struct agent_expr
*agent
;
1771 struct frame_info
*fi
= get_current_frame (); /* need current scope */
1773 /* We don't deal with overlay debugging at the moment. We need to
1774 think more carefully about this. If you copy this code into
1775 another command, change the error message; the user shouldn't
1776 have to know anything about agent expressions. */
1777 if (overlay_debugging
)
1778 error (_("GDB can't do agent expression translation with overlays."));
1781 error_no_arg (_("expression to translate"));
1783 expr
= parse_expression (exp
);
1784 old_chain
= make_cleanup (free_current_contents
, &expr
);
1785 agent
= gen_trace_for_expr (get_frame_pc (fi
), expr
);
1786 make_cleanup_free_agent_expr (agent
);
1787 ax_print (gdb_stdout
, agent
);
1789 /* It would be nice to call ax_reqs here to gather some general info
1790 about the expression, and then print out the result. */
1792 do_cleanups (old_chain
);
1797 /* Initialization code. */
1799 void _initialize_ax_gdb (void);
1801 _initialize_ax_gdb (void)
1803 add_cmd ("agent", class_maintenance
, agent_command
,
1804 _("Translate an expression into remote agent bytecode."),