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"
36 #include "user-regs.h"
38 /* To make sense of this file, you should read doc/agentexpr.texi.
39 Then look at the types and enums in ax-gdb.h. For the code itself,
40 look at gen_expr, towards the bottom; that's the main function that
41 looks at the GDB expressions and calls everything else to generate
44 I'm beginning to wonder whether it wouldn't be nicer to internally
45 generate trees, with types, and then spit out the bytecode in
46 linear form afterwards; we could generate fewer `swap', `ext', and
47 `zero_ext' bytecodes that way; it would make good constant folding
48 easier, too. But at the moment, I think we should be willing to
49 pay for the simplicity of this code with less-than-optimal bytecode
52 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
56 /* Prototypes for local functions. */
58 /* There's a standard order to the arguments of these functions:
59 union exp_element ** --- pointer into expression
60 struct agent_expr * --- agent expression buffer to generate code into
61 struct axs_value * --- describes value left on top of stack */
63 static struct value
*const_var_ref (struct symbol
*var
);
64 static struct value
*const_expr (union exp_element
**pc
);
65 static struct value
*maybe_const_expr (union exp_element
**pc
);
67 static void gen_traced_pop (struct agent_expr
*, struct axs_value
*);
69 static void gen_sign_extend (struct agent_expr
*, struct type
*);
70 static void gen_extend (struct agent_expr
*, struct type
*);
71 static void gen_fetch (struct agent_expr
*, struct type
*);
72 static void gen_left_shift (struct agent_expr
*, int);
75 static void gen_frame_args_address (struct agent_expr
*);
76 static void gen_frame_locals_address (struct agent_expr
*);
77 static void gen_offset (struct agent_expr
*ax
, int offset
);
78 static void gen_sym_offset (struct agent_expr
*, struct symbol
*);
79 static void gen_var_ref (struct agent_expr
*ax
,
80 struct axs_value
*value
, struct symbol
*var
);
83 static void gen_int_literal (struct agent_expr
*ax
,
84 struct axs_value
*value
,
85 LONGEST k
, struct type
*type
);
88 static void require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
);
89 static void gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
);
90 static int type_wider_than (struct type
*type1
, struct type
*type2
);
91 static struct type
*max_type (struct type
*type1
, struct type
*type2
);
92 static void gen_conversion (struct agent_expr
*ax
,
93 struct type
*from
, struct type
*to
);
94 static int is_nontrivial_conversion (struct type
*from
, struct type
*to
);
95 static void gen_usual_arithmetic (struct agent_expr
*ax
,
96 struct axs_value
*value1
,
97 struct axs_value
*value2
);
98 static void gen_integral_promotions (struct agent_expr
*ax
,
99 struct axs_value
*value
);
100 static void gen_cast (struct agent_expr
*ax
,
101 struct axs_value
*value
, struct type
*type
);
102 static void gen_scale (struct agent_expr
*ax
,
103 enum agent_op op
, struct type
*type
);
104 static void gen_add (struct agent_expr
*ax
,
105 struct axs_value
*value
,
106 struct axs_value
*value1
,
107 struct axs_value
*value2
, char *name
);
108 static void gen_sub (struct agent_expr
*ax
,
109 struct axs_value
*value
,
110 struct axs_value
*value1
, struct axs_value
*value2
);
111 static void gen_binop (struct agent_expr
*ax
,
112 struct axs_value
*value
,
113 struct axs_value
*value1
,
114 struct axs_value
*value2
,
116 enum agent_op op_unsigned
, int may_carry
, char *name
);
117 static void gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
);
118 static void gen_complement (struct agent_expr
*ax
, struct axs_value
*value
);
119 static void gen_deref (struct agent_expr
*, struct axs_value
*);
120 static void gen_address_of (struct agent_expr
*, struct axs_value
*);
121 static int find_field (struct type
*type
, char *name
);
122 static void gen_bitfield_ref (struct agent_expr
*ax
,
123 struct axs_value
*value
,
124 struct type
*type
, int start
, int end
);
125 static void gen_struct_ref (struct agent_expr
*ax
,
126 struct axs_value
*value
,
128 char *operator_name
, char *operand_name
);
129 static void gen_repeat (union exp_element
**pc
,
130 struct agent_expr
*ax
, struct axs_value
*value
);
131 static void gen_sizeof (union exp_element
**pc
,
132 struct agent_expr
*ax
, struct axs_value
*value
);
133 static void gen_expr (union exp_element
**pc
,
134 struct agent_expr
*ax
, struct axs_value
*value
);
136 static void agent_command (char *exp
, int from_tty
);
139 /* Detecting constant expressions. */
141 /* If the variable reference at *PC is a constant, return its value.
142 Otherwise, return zero.
144 Hey, Wally! How can a variable reference be a constant?
146 Well, Beav, this function really handles the OP_VAR_VALUE operator,
147 not specifically variable references. GDB uses OP_VAR_VALUE to
148 refer to any kind of symbolic reference: function names, enum
149 elements, and goto labels are all handled through the OP_VAR_VALUE
150 operator, even though they're constants. It makes sense given the
153 Gee, Wally, don'cha wonder sometimes if data representations that
154 subvert commonly accepted definitions of terms in favor of heavily
155 context-specific interpretations are really just a tool of the
156 programming hegemony to preserve their power and exclude the
159 static struct value
*
160 const_var_ref (struct symbol
*var
)
162 struct type
*type
= SYMBOL_TYPE (var
);
164 switch (SYMBOL_CLASS (var
))
167 return value_from_longest (type
, (LONGEST
) SYMBOL_VALUE (var
));
170 return value_from_pointer (type
, (CORE_ADDR
) SYMBOL_VALUE_ADDRESS (var
));
178 /* If the expression starting at *PC has a constant value, return it.
179 Otherwise, return zero. If we return a value, then *PC will be
180 advanced to the end of it. If we return zero, *PC could be
182 static struct value
*
183 const_expr (union exp_element
**pc
)
185 enum exp_opcode op
= (*pc
)->opcode
;
192 struct type
*type
= (*pc
)[1].type
;
193 LONGEST k
= (*pc
)[2].longconst
;
195 return value_from_longest (type
, k
);
200 struct value
*v
= const_var_ref ((*pc
)[2].symbol
);
205 /* We could add more operators in here. */
209 v1
= const_expr (pc
);
211 return value_neg (v1
);
221 /* Like const_expr, but guarantee also that *PC is undisturbed if the
222 expression is not constant. */
223 static struct value
*
224 maybe_const_expr (union exp_element
**pc
)
226 union exp_element
*tentative_pc
= *pc
;
227 struct value
*v
= const_expr (&tentative_pc
);
229 /* If we got a value, then update the real PC. */
237 /* Generating bytecode from GDB expressions: general assumptions */
239 /* Here are a few general assumptions made throughout the code; if you
240 want to make a change that contradicts one of these, then you'd
241 better scan things pretty thoroughly.
243 - We assume that all values occupy one stack element. For example,
244 sometimes we'll swap to get at the left argument to a binary
245 operator. If we decide that void values should occupy no stack
246 elements, or that synthetic arrays (whose size is determined at
247 run time, created by the `@' operator) should occupy two stack
248 elements (address and length), then this will cause trouble.
250 - We assume the stack elements are infinitely wide, and that we
251 don't have to worry what happens if the user requests an
252 operation that is wider than the actual interpreter's stack.
253 That is, it's up to the interpreter to handle directly all the
254 integer widths the user has access to. (Woe betide the language
257 - We don't support side effects. Thus, we don't have to worry about
258 GCC's generalized lvalues, function calls, etc.
260 - We don't support floating point. Many places where we switch on
261 some type don't bother to include cases for floating point; there
262 may be even more subtle ways this assumption exists. For
263 example, the arguments to % must be integers.
265 - We assume all subexpressions have a static, unchanging type. If
266 we tried to support convenience variables, this would be a
269 - All values on the stack should always be fully zero- or
272 (I wasn't sure whether to choose this or its opposite --- that
273 only addresses are assumed extended --- but it turns out that
274 neither convention completely eliminates spurious extend
275 operations (if everything is always extended, then you have to
276 extend after add, because it could overflow; if nothing is
277 extended, then you end up producing extends whenever you change
278 sizes), and this is simpler.) */
281 /* Generating bytecode from GDB expressions: the `trace' kludge */
283 /* The compiler in this file is a general-purpose mechanism for
284 translating GDB expressions into bytecode. One ought to be able to
285 find a million and one uses for it.
287 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
288 of expediency. Let he who is without sin cast the first stone.
290 For the data tracing facility, we need to insert `trace' bytecodes
291 before each data fetch; this records all the memory that the
292 expression touches in the course of evaluation, so that memory will
293 be available when the user later tries to evaluate the expression
296 This should be done (I think) in a post-processing pass, that walks
297 an arbitrary agent expression and inserts `trace' operations at the
298 appropriate points. But it's much faster to just hack them
299 directly into the code. And since we're in a crunch, that's what
302 Setting the flag trace_kludge to non-zero enables the code that
303 emits the trace bytecodes at the appropriate points. */
304 static int trace_kludge
;
306 /* Trace the lvalue on the stack, if it needs it. In either case, pop
307 the value. Useful on the left side of a comma, and at the end of
308 an expression being used for tracing. */
310 gen_traced_pop (struct agent_expr
*ax
, struct axs_value
*value
)
316 /* We don't trace rvalues, just the lvalues necessary to
317 produce them. So just dispose of this value. */
318 ax_simple (ax
, aop_pop
);
321 case axs_lvalue_memory
:
323 int length
= TYPE_LENGTH (check_typedef (value
->type
));
325 /* There's no point in trying to use a trace_quick bytecode
326 here, since "trace_quick SIZE pop" is three bytes, whereas
327 "const8 SIZE trace" is also three bytes, does the same
328 thing, and the simplest code which generates that will also
329 work correctly for objects with large sizes. */
330 ax_const_l (ax
, length
);
331 ax_simple (ax
, aop_trace
);
335 case axs_lvalue_register
:
336 /* We need to mention the register somewhere in the bytecode,
337 so ax_reqs will pick it up and add it to the mask of
339 ax_reg (ax
, value
->u
.reg
);
340 ax_simple (ax
, aop_pop
);
344 /* If we're not tracing, just pop the value. */
345 ax_simple (ax
, aop_pop
);
350 /* Generating bytecode from GDB expressions: helper functions */
352 /* Assume that the lower bits of the top of the stack is a value of
353 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
355 gen_sign_extend (struct agent_expr
*ax
, struct type
*type
)
357 /* Do we need to sign-extend this? */
358 if (!TYPE_UNSIGNED (type
))
359 ax_ext (ax
, TYPE_LENGTH (type
) * TARGET_CHAR_BIT
);
363 /* Assume the lower bits of the top of the stack hold a value of type
364 TYPE, and the upper bits are garbage. Sign-extend or truncate as
367 gen_extend (struct agent_expr
*ax
, struct type
*type
)
369 int bits
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
371 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, bits
));
375 /* Assume that the top of the stack contains a value of type "pointer
376 to TYPE"; generate code to fetch its value. Note that TYPE is the
377 target type, not the pointer type. */
379 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
383 /* Record the area of memory we're about to fetch. */
384 ax_trace_quick (ax
, TYPE_LENGTH (type
));
387 switch (TYPE_CODE (type
))
393 /* It's a scalar value, so we know how to dereference it. How
394 many bytes long is it? */
395 switch (TYPE_LENGTH (type
))
397 case 8 / TARGET_CHAR_BIT
:
398 ax_simple (ax
, aop_ref8
);
400 case 16 / TARGET_CHAR_BIT
:
401 ax_simple (ax
, aop_ref16
);
403 case 32 / TARGET_CHAR_BIT
:
404 ax_simple (ax
, aop_ref32
);
406 case 64 / TARGET_CHAR_BIT
:
407 ax_simple (ax
, aop_ref64
);
410 /* Either our caller shouldn't have asked us to dereference
411 that pointer (other code's fault), or we're not
412 implementing something we should be (this code's fault).
413 In any case, it's a bug the user shouldn't see. */
415 internal_error (__FILE__
, __LINE__
,
416 _("gen_fetch: strange size"));
419 gen_sign_extend (ax
, type
);
423 /* Either our caller shouldn't have asked us to dereference that
424 pointer (other code's fault), or we're not implementing
425 something we should be (this code's fault). In any case,
426 it's a bug the user shouldn't see. */
427 internal_error (__FILE__
, __LINE__
,
428 _("gen_fetch: bad type code"));
433 /* Generate code to left shift the top of the stack by DISTANCE bits, or
434 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
435 unsigned (logical) right shifts. */
437 gen_left_shift (struct agent_expr
*ax
, int distance
)
441 ax_const_l (ax
, distance
);
442 ax_simple (ax
, aop_lsh
);
444 else if (distance
< 0)
446 ax_const_l (ax
, -distance
);
447 ax_simple (ax
, aop_rsh_unsigned
);
453 /* Generating bytecode from GDB expressions: symbol references */
455 /* Generate code to push the base address of the argument portion of
456 the top stack frame. */
458 gen_frame_args_address (struct agent_expr
*ax
)
461 LONGEST frame_offset
;
463 gdbarch_virtual_frame_pointer (current_gdbarch
,
464 ax
->scope
, &frame_reg
, &frame_offset
);
465 ax_reg (ax
, frame_reg
);
466 gen_offset (ax
, frame_offset
);
470 /* Generate code to push the base address of the locals portion of the
473 gen_frame_locals_address (struct agent_expr
*ax
)
476 LONGEST frame_offset
;
478 gdbarch_virtual_frame_pointer (current_gdbarch
,
479 ax
->scope
, &frame_reg
, &frame_offset
);
480 ax_reg (ax
, frame_reg
);
481 gen_offset (ax
, frame_offset
);
485 /* Generate code to add OFFSET to the top of the stack. Try to
486 generate short and readable code. We use this for getting to
487 variables on the stack, and structure members. If we were
488 programming in ML, it would be clearer why these are the same
491 gen_offset (struct agent_expr
*ax
, int offset
)
493 /* It would suffice to simply push the offset and add it, but this
494 makes it easier to read positive and negative offsets in the
498 ax_const_l (ax
, offset
);
499 ax_simple (ax
, aop_add
);
503 ax_const_l (ax
, -offset
);
504 ax_simple (ax
, aop_sub
);
509 /* In many cases, a symbol's value is the offset from some other
510 address (stack frame, base register, etc.) Generate code to add
511 VAR's value to the top of the stack. */
513 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
515 gen_offset (ax
, SYMBOL_VALUE (var
));
519 /* Generate code for a variable reference to AX. The variable is the
520 symbol VAR. Set VALUE to describe the result. */
523 gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
, struct symbol
*var
)
525 /* Dereference any typedefs. */
526 value
->type
= check_typedef (SYMBOL_TYPE (var
));
528 /* I'm imitating the code in read_var_value. */
529 switch (SYMBOL_CLASS (var
))
531 case LOC_CONST
: /* A constant, like an enum value. */
532 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE (var
));
533 value
->kind
= axs_rvalue
;
536 case LOC_LABEL
: /* A goto label, being used as a value. */
537 ax_const_l (ax
, (LONGEST
) SYMBOL_VALUE_ADDRESS (var
));
538 value
->kind
= axs_rvalue
;
541 case LOC_CONST_BYTES
:
542 internal_error (__FILE__
, __LINE__
,
543 _("gen_var_ref: LOC_CONST_BYTES symbols are not supported"));
545 /* Variable at a fixed location in memory. Easy. */
547 /* Push the address of the variable. */
548 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (var
));
549 value
->kind
= axs_lvalue_memory
;
552 case LOC_ARG
: /* var lives in argument area of frame */
553 gen_frame_args_address (ax
);
554 gen_sym_offset (ax
, var
);
555 value
->kind
= axs_lvalue_memory
;
558 case LOC_REF_ARG
: /* As above, but the frame slot really
559 holds the address of the variable. */
560 gen_frame_args_address (ax
);
561 gen_sym_offset (ax
, var
);
562 /* Don't assume any particular pointer size. */
563 gen_fetch (ax
, lookup_pointer_type (builtin_type_void
));
564 value
->kind
= axs_lvalue_memory
;
567 case LOC_LOCAL
: /* var lives in locals area of frame */
568 gen_frame_locals_address (ax
);
569 gen_sym_offset (ax
, var
);
570 value
->kind
= axs_lvalue_memory
;
574 error (_("Cannot compute value of typedef `%s'."),
575 SYMBOL_PRINT_NAME (var
));
579 ax_const_l (ax
, BLOCK_START (SYMBOL_BLOCK_VALUE (var
)));
580 value
->kind
= axs_rvalue
;
584 /* Don't generate any code at all; in the process of treating
585 this as an lvalue or rvalue, the caller will generate the
587 value
->kind
= axs_lvalue_register
;
588 value
->u
.reg
= SYMBOL_VALUE (var
);
591 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
592 register, not on the stack. Simpler than LOC_REGISTER
593 because it's just like any other case where the thing
594 has a real address. */
595 case LOC_REGPARM_ADDR
:
596 ax_reg (ax
, SYMBOL_VALUE (var
));
597 value
->kind
= axs_lvalue_memory
;
602 struct minimal_symbol
*msym
603 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var
), NULL
, NULL
);
605 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var
));
607 /* Push the address of the variable. */
608 ax_const_l (ax
, SYMBOL_VALUE_ADDRESS (msym
));
609 value
->kind
= axs_lvalue_memory
;
614 /* FIXME: cagney/2004-01-26: It should be possible to
615 unconditionally call the SYMBOL_OPS method when available.
616 Unfortunately DWARF 2 stores the frame-base (instead of the
617 function) location in a function's symbol. Oops! For the
618 moment enable this when/where applicable. */
619 SYMBOL_OPS (var
)->tracepoint_var_ref (var
, ax
, value
);
622 case LOC_OPTIMIZED_OUT
:
623 error (_("The variable `%s' has been optimized out."),
624 SYMBOL_PRINT_NAME (var
));
628 error (_("Cannot find value of botched symbol `%s'."),
629 SYMBOL_PRINT_NAME (var
));
636 /* Generating bytecode from GDB expressions: literals */
639 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
643 value
->kind
= axs_rvalue
;
644 value
->type
= check_typedef (type
);
649 /* Generating bytecode from GDB expressions: unary conversions, casts */
651 /* Take what's on the top of the stack (as described by VALUE), and
652 try to make an rvalue out of it. Signal an error if we can't do
655 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
660 /* It's already an rvalue. */
663 case axs_lvalue_memory
:
664 /* The top of stack is the address of the object. Dereference. */
665 gen_fetch (ax
, value
->type
);
668 case axs_lvalue_register
:
669 /* There's nothing on the stack, but value->u.reg is the
670 register number containing the value.
672 When we add floating-point support, this is going to have to
673 change. What about SPARC register pairs, for example? */
674 ax_reg (ax
, value
->u
.reg
);
675 gen_extend (ax
, value
->type
);
679 value
->kind
= axs_rvalue
;
683 /* Assume the top of the stack is described by VALUE, and perform the
684 usual unary conversions. This is motivated by ANSI 6.2.2, but of
685 course GDB expressions are not ANSI; they're the mishmash union of
686 a bunch of languages. Rah.
688 NOTE! This function promises to produce an rvalue only when the
689 incoming value is of an appropriate type. In other words, the
690 consumer of the value this function produces may assume the value
691 is an rvalue only after checking its type.
693 The immediate issue is that if the user tries to use a structure or
694 union as an operand of, say, the `+' operator, we don't want to try
695 to convert that structure to an rvalue; require_rvalue will bomb on
696 structs and unions. Rather, we want to simply pass the struct
697 lvalue through unchanged, and let `+' raise an error. */
700 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
702 /* We don't have to generate any code for the usual integral
703 conversions, since values are always represented as full-width on
704 the stack. Should we tweak the type? */
706 /* Some types require special handling. */
707 switch (TYPE_CODE (value
->type
))
709 /* Functions get converted to a pointer to the function. */
711 value
->type
= lookup_pointer_type (value
->type
);
712 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
715 /* Arrays get converted to a pointer to their first element, and
716 are no longer an lvalue. */
717 case TYPE_CODE_ARRAY
:
719 struct type
*elements
= TYPE_TARGET_TYPE (value
->type
);
720 value
->type
= lookup_pointer_type (elements
);
721 value
->kind
= axs_rvalue
;
722 /* We don't need to generate any code; the address of the array
723 is also the address of its first element. */
727 /* Don't try to convert structures and unions to rvalues. Let the
728 consumer signal an error. */
729 case TYPE_CODE_STRUCT
:
730 case TYPE_CODE_UNION
:
733 /* If the value is an enum, call it an integer. */
735 value
->type
= builtin_type_int
;
739 /* If the value is an lvalue, dereference it. */
740 require_rvalue (ax
, value
);
744 /* Return non-zero iff the type TYPE1 is considered "wider" than the
745 type TYPE2, according to the rules described in gen_usual_arithmetic. */
747 type_wider_than (struct type
*type1
, struct type
*type2
)
749 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)
750 || (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
751 && TYPE_UNSIGNED (type1
)
752 && !TYPE_UNSIGNED (type2
)));
756 /* Return the "wider" of the two types TYPE1 and TYPE2. */
758 max_type (struct type
*type1
, struct type
*type2
)
760 return type_wider_than (type1
, type2
) ? type1
: type2
;
764 /* Generate code to convert a scalar value of type FROM to type TO. */
766 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
768 /* Perhaps there is a more graceful way to state these rules. */
770 /* If we're converting to a narrower type, then we need to clear out
772 if (TYPE_LENGTH (to
) < TYPE_LENGTH (from
))
773 gen_extend (ax
, from
);
775 /* If the two values have equal width, but different signednesses,
776 then we need to extend. */
777 else if (TYPE_LENGTH (to
) == TYPE_LENGTH (from
))
779 if (TYPE_UNSIGNED (from
) != TYPE_UNSIGNED (to
))
783 /* If we're converting to a wider type, and becoming unsigned, then
784 we need to zero out any possible sign bits. */
785 else if (TYPE_LENGTH (to
) > TYPE_LENGTH (from
))
787 if (TYPE_UNSIGNED (to
))
793 /* Return non-zero iff the type FROM will require any bytecodes to be
794 emitted to be converted to the type TO. */
796 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
798 struct agent_expr
*ax
= new_agent_expr (0);
801 /* Actually generate the code, and see if anything came out. At the
802 moment, it would be trivial to replicate the code in
803 gen_conversion here, but in the future, when we're supporting
804 floating point and the like, it may not be. Doing things this
805 way allows this function to be independent of the logic in
807 gen_conversion (ax
, from
, to
);
808 nontrivial
= ax
->len
> 0;
809 free_agent_expr (ax
);
814 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
815 6.2.1.5) for the two operands of an arithmetic operator. This
816 effectively finds a "least upper bound" type for the two arguments,
817 and promotes each argument to that type. *VALUE1 and *VALUE2
818 describe the values as they are passed in, and as they are left. */
820 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
821 struct axs_value
*value2
)
823 /* Do the usual binary conversions. */
824 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
825 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
827 /* The ANSI integral promotions seem to work this way: Order the
828 integer types by size, and then by signedness: an n-bit
829 unsigned type is considered "wider" than an n-bit signed
830 type. Promote to the "wider" of the two types, and always
831 promote at least to int. */
832 struct type
*target
= max_type (builtin_type_int
,
833 max_type (value1
->type
, value2
->type
));
835 /* Deal with value2, on the top of the stack. */
836 gen_conversion (ax
, value2
->type
, target
);
838 /* Deal with value1, not on the top of the stack. Don't
839 generate the `swap' instructions if we're not actually going
841 if (is_nontrivial_conversion (value1
->type
, target
))
843 ax_simple (ax
, aop_swap
);
844 gen_conversion (ax
, value1
->type
, target
);
845 ax_simple (ax
, aop_swap
);
848 value1
->type
= value2
->type
= check_typedef (target
);
853 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
854 the value on the top of the stack, as described by VALUE. Assume
855 the value has integral type. */
857 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
859 if (!type_wider_than (value
->type
, builtin_type_int
))
861 gen_conversion (ax
, value
->type
, builtin_type_int
);
862 value
->type
= builtin_type_int
;
864 else if (!type_wider_than (value
->type
, builtin_type_unsigned_int
))
866 gen_conversion (ax
, value
->type
, builtin_type_unsigned_int
);
867 value
->type
= builtin_type_unsigned_int
;
872 /* Generate code for a cast to TYPE. */
874 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
876 /* GCC does allow casts to yield lvalues, so this should be fixed
877 before merging these changes into the trunk. */
878 require_rvalue (ax
, value
);
879 /* Dereference typedefs. */
880 type
= check_typedef (type
);
882 switch (TYPE_CODE (type
))
885 /* It's implementation-defined, and I'll bet this is what GCC
889 case TYPE_CODE_ARRAY
:
890 case TYPE_CODE_STRUCT
:
891 case TYPE_CODE_UNION
:
893 error (_("Invalid type cast: intended type must be scalar."));
896 /* We don't have to worry about the size of the value, because
897 all our integral values are fully sign-extended, and when
898 casting pointers we can do anything we like. Is there any
899 way for us to know what GCC actually does with a cast like
904 gen_conversion (ax
, value
->type
, type
);
908 /* We could pop the value, and rely on everyone else to check
909 the type and notice that this value doesn't occupy a stack
910 slot. But for now, leave the value on the stack, and
911 preserve the "value == stack element" assumption. */
915 error (_("Casts to requested type are not yet implemented."));
923 /* Generating bytecode from GDB expressions: arithmetic */
925 /* Scale the integer on the top of the stack by the size of the target
926 of the pointer type TYPE. */
928 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
930 struct type
*element
= TYPE_TARGET_TYPE (type
);
932 if (TYPE_LENGTH (element
) != 1)
934 ax_const_l (ax
, TYPE_LENGTH (element
));
940 /* Generate code for an addition; non-trivial because we deal with
941 pointer arithmetic. We set VALUE to describe the result value; we
942 assume VALUE1 and VALUE2 describe the two operands, and that
943 they've undergone the usual binary conversions. Used by both
944 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
946 gen_add (struct agent_expr
*ax
, struct axs_value
*value
,
947 struct axs_value
*value1
, struct axs_value
*value2
, char *name
)
950 if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
951 && TYPE_CODE (value2
->type
) == TYPE_CODE_PTR
)
953 /* Swap the values and proceed normally. */
954 ax_simple (ax
, aop_swap
);
955 gen_scale (ax
, aop_mul
, value2
->type
);
956 ax_simple (ax
, aop_add
);
957 gen_extend (ax
, value2
->type
); /* Catch overflow. */
958 value
->type
= value2
->type
;
962 else if (TYPE_CODE (value1
->type
) == TYPE_CODE_PTR
963 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
965 gen_scale (ax
, aop_mul
, value1
->type
);
966 ax_simple (ax
, aop_add
);
967 gen_extend (ax
, value1
->type
); /* Catch overflow. */
968 value
->type
= value1
->type
;
971 /* Must be number + number; the usual binary conversions will have
972 brought them both to the same width. */
973 else if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
974 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
976 ax_simple (ax
, aop_add
);
977 gen_extend (ax
, value1
->type
); /* Catch overflow. */
978 value
->type
= value1
->type
;
982 error (_("Invalid combination of types in %s."), name
);
984 value
->kind
= axs_rvalue
;
988 /* Generate code for an addition; non-trivial because we have to deal
989 with pointer arithmetic. We set VALUE to describe the result
990 value; we assume VALUE1 and VALUE2 describe the two operands, and
991 that they've undergone the usual binary conversions. */
993 gen_sub (struct agent_expr
*ax
, struct axs_value
*value
,
994 struct axs_value
*value1
, struct axs_value
*value2
)
996 if (TYPE_CODE (value1
->type
) == TYPE_CODE_PTR
)
998 /* Is it PTR - INT? */
999 if (TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
1001 gen_scale (ax
, aop_mul
, value1
->type
);
1002 ax_simple (ax
, aop_sub
);
1003 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1004 value
->type
= value1
->type
;
1007 /* Is it PTR - PTR? Strictly speaking, the types ought to
1008 match, but this is what the normal GDB expression evaluator
1010 else if (TYPE_CODE (value2
->type
) == TYPE_CODE_PTR
1011 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1
->type
))
1012 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2
->type
))))
1014 ax_simple (ax
, aop_sub
);
1015 gen_scale (ax
, aop_div_unsigned
, value1
->type
);
1016 value
->type
= builtin_type_long
; /* FIXME --- should be ptrdiff_t */
1020 First argument of `-' is a pointer, but second argument is neither\n\
1021 an integer nor a pointer of the same type."));
1024 /* Must be number + number. */
1025 else if (TYPE_CODE (value1
->type
) == TYPE_CODE_INT
1026 && TYPE_CODE (value2
->type
) == TYPE_CODE_INT
)
1028 ax_simple (ax
, aop_sub
);
1029 gen_extend (ax
, value1
->type
); /* Catch overflow. */
1030 value
->type
= value1
->type
;
1034 error (_("Invalid combination of types in subtraction."));
1036 value
->kind
= axs_rvalue
;
1039 /* Generate code for a binary operator that doesn't do pointer magic.
1040 We set VALUE to describe the result value; we assume VALUE1 and
1041 VALUE2 describe the two operands, and that they've undergone the
1042 usual binary conversions. MAY_CARRY should be non-zero iff the
1043 result needs to be extended. NAME is the English name of the
1044 operator, used in error messages */
1046 gen_binop (struct agent_expr
*ax
, struct axs_value
*value
,
1047 struct axs_value
*value1
, struct axs_value
*value2
, enum agent_op op
,
1048 enum agent_op op_unsigned
, int may_carry
, char *name
)
1050 /* We only handle INT op INT. */
1051 if ((TYPE_CODE (value1
->type
) != TYPE_CODE_INT
)
1052 || (TYPE_CODE (value2
->type
) != TYPE_CODE_INT
))
1053 error (_("Invalid combination of types in %s."), name
);
1056 TYPE_UNSIGNED (value1
->type
) ? op_unsigned
: op
);
1058 gen_extend (ax
, value1
->type
); /* catch overflow */
1059 value
->type
= value1
->type
;
1060 value
->kind
= axs_rvalue
;
1065 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
)
1067 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
1068 && TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1069 error (_("Invalid type of operand to `!'."));
1071 gen_usual_unary (ax
, value
);
1072 ax_simple (ax
, aop_log_not
);
1073 value
->type
= builtin_type_int
;
1078 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1080 if (TYPE_CODE (value
->type
) != TYPE_CODE_INT
)
1081 error (_("Invalid type of operand to `~'."));
1083 gen_usual_unary (ax
, value
);
1084 gen_integral_promotions (ax
, value
);
1085 ax_simple (ax
, aop_bit_not
);
1086 gen_extend (ax
, value
->type
);
1091 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1093 /* Dereference the value on the top of the stack. */
1095 gen_deref (struct agent_expr
*ax
, struct axs_value
*value
)
1097 /* The caller should check the type, because several operators use
1098 this, and we don't know what error message to generate. */
1099 if (TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1100 internal_error (__FILE__
, __LINE__
,
1101 _("gen_deref: expected a pointer"));
1103 /* We've got an rvalue now, which is a pointer. We want to yield an
1104 lvalue, whose address is exactly that pointer. So we don't
1105 actually emit any code; we just change the type from "Pointer to
1106 T" to "T", and mark the value as an lvalue in memory. Leave it
1107 to the consumer to actually dereference it. */
1108 value
->type
= check_typedef (TYPE_TARGET_TYPE (value
->type
));
1109 value
->kind
= ((TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1110 ? axs_rvalue
: axs_lvalue_memory
);
1114 /* Produce the address of the lvalue on the top of the stack. */
1116 gen_address_of (struct agent_expr
*ax
, struct axs_value
*value
)
1118 /* Special case for taking the address of a function. The ANSI
1119 standard describes this as a special case, too, so this
1120 arrangement is not without motivation. */
1121 if (TYPE_CODE (value
->type
) == TYPE_CODE_FUNC
)
1122 /* The value's already an rvalue on the stack, so we just need to
1124 value
->type
= lookup_pointer_type (value
->type
);
1126 switch (value
->kind
)
1129 error (_("Operand of `&' is an rvalue, which has no address."));
1131 case axs_lvalue_register
:
1132 error (_("Operand of `&' is in a register, and has no address."));
1134 case axs_lvalue_memory
:
1135 value
->kind
= axs_rvalue
;
1136 value
->type
= lookup_pointer_type (value
->type
);
1142 /* A lot of this stuff will have to change to support C++. But we're
1143 not going to deal with that at the moment. */
1145 /* Find the field in the structure type TYPE named NAME, and return
1146 its index in TYPE's field array. */
1148 find_field (struct type
*type
, char *name
)
1152 CHECK_TYPEDEF (type
);
1154 /* Make sure this isn't C++. */
1155 if (TYPE_N_BASECLASSES (type
) != 0)
1156 internal_error (__FILE__
, __LINE__
,
1157 _("find_field: derived classes supported"));
1159 for (i
= 0; i
< TYPE_NFIELDS (type
); i
++)
1161 char *this_name
= TYPE_FIELD_NAME (type
, i
);
1165 if (strcmp (name
, this_name
) == 0)
1168 if (this_name
[0] == '\0')
1169 internal_error (__FILE__
, __LINE__
,
1170 _("find_field: anonymous unions not supported"));
1174 error (_("Couldn't find member named `%s' in struct/union `%s'"),
1175 name
, TYPE_TAG_NAME (type
));
1181 /* Generate code to push the value of a bitfield of a structure whose
1182 address is on the top of the stack. START and END give the
1183 starting and one-past-ending *bit* numbers of the field within the
1186 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1187 struct type
*type
, int start
, int end
)
1189 /* Note that ops[i] fetches 8 << i bits. */
1190 static enum agent_op ops
[]
1192 {aop_ref8
, aop_ref16
, aop_ref32
, aop_ref64
};
1193 static int num_ops
= (sizeof (ops
) / sizeof (ops
[0]));
1195 /* We don't want to touch any byte that the bitfield doesn't
1196 actually occupy; we shouldn't make any accesses we're not
1197 explicitly permitted to. We rely here on the fact that the
1198 bytecode `ref' operators work on unaligned addresses.
1200 It takes some fancy footwork to get the stack to work the way
1201 we'd like. Say we're retrieving a bitfield that requires three
1202 fetches. Initially, the stack just contains the address:
1204 For the first fetch, we duplicate the address
1206 then add the byte offset, do the fetch, and shift and mask as
1207 needed, yielding a fragment of the value, properly aligned for
1208 the final bitwise or:
1210 then we swap, and repeat the process:
1211 frag1 addr --- address on top
1212 frag1 addr addr --- duplicate it
1213 frag1 addr frag2 --- get second fragment
1214 frag1 frag2 addr --- swap again
1215 frag1 frag2 frag3 --- get third fragment
1216 Notice that, since the third fragment is the last one, we don't
1217 bother duplicating the address this time. Now we have all the
1218 fragments on the stack, and we can simply `or' them together,
1219 yielding the final value of the bitfield. */
1221 /* The first and one-after-last bits in the field, but rounded down
1222 and up to byte boundaries. */
1223 int bound_start
= (start
/ TARGET_CHAR_BIT
) * TARGET_CHAR_BIT
;
1224 int bound_end
= (((end
+ TARGET_CHAR_BIT
- 1)
1228 /* current bit offset within the structure */
1231 /* The index in ops of the opcode we're considering. */
1234 /* The number of fragments we generated in the process. Probably
1235 equal to the number of `one' bits in bytesize, but who cares? */
1238 /* Dereference any typedefs. */
1239 type
= check_typedef (type
);
1241 /* Can we fetch the number of bits requested at all? */
1242 if ((end
- start
) > ((1 << num_ops
) * 8))
1243 internal_error (__FILE__
, __LINE__
,
1244 _("gen_bitfield_ref: bitfield too wide"));
1246 /* Note that we know here that we only need to try each opcode once.
1247 That may not be true on machines with weird byte sizes. */
1248 offset
= bound_start
;
1250 for (op
= num_ops
- 1; op
>= 0; op
--)
1252 /* number of bits that ops[op] would fetch */
1253 int op_size
= 8 << op
;
1255 /* The stack at this point, from bottom to top, contains zero or
1256 more fragments, then the address. */
1258 /* Does this fetch fit within the bitfield? */
1259 if (offset
+ op_size
<= bound_end
)
1261 /* Is this the last fragment? */
1262 int last_frag
= (offset
+ op_size
== bound_end
);
1265 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1267 /* Add the offset. */
1268 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1272 /* Record the area of memory we're about to fetch. */
1273 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1276 /* Perform the fetch. */
1277 ax_simple (ax
, ops
[op
]);
1279 /* Shift the bits we have to their proper position.
1280 gen_left_shift will generate right shifts when the operand
1283 A big-endian field diagram to ponder:
1284 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1285 +------++------++------++------++------++------++------++------+
1286 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1288 bit number 16 32 48 53
1289 These are bit numbers as supplied by GDB. Note that the
1290 bit numbers run from right to left once you've fetched the
1293 A little-endian field diagram to ponder:
1294 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1295 +------++------++------++------++------++------++------++------+
1296 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1298 bit number 48 32 16 4 0
1300 In both cases, the most significant end is on the left
1301 (i.e. normal numeric writing order), which means that you
1302 don't go crazy thinking about `left' and `right' shifts.
1304 We don't have to worry about masking yet:
1305 - If they contain garbage off the least significant end, then we
1306 must be looking at the low end of the field, and the right
1307 shift will wipe them out.
1308 - If they contain garbage off the most significant end, then we
1309 must be looking at the most significant end of the word, and
1310 the sign/zero extension will wipe them out.
1311 - If we're in the interior of the word, then there is no garbage
1312 on either end, because the ref operators zero-extend. */
1313 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
1314 gen_left_shift (ax
, end
- (offset
+ op_size
));
1316 gen_left_shift (ax
, offset
- start
);
1319 /* Bring the copy of the address up to the top. */
1320 ax_simple (ax
, aop_swap
);
1327 /* Generate enough bitwise `or' operations to combine all the
1328 fragments we left on the stack. */
1329 while (fragment_count
-- > 1)
1330 ax_simple (ax
, aop_bit_or
);
1332 /* Sign- or zero-extend the value as appropriate. */
1333 ((TYPE_UNSIGNED (type
) ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1335 /* This is *not* an lvalue. Ugh. */
1336 value
->kind
= axs_rvalue
;
1341 /* Generate code to reference the member named FIELD of a structure or
1342 union. The top of the stack, as described by VALUE, should have
1343 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1344 the operator being compiled, and OPERAND_NAME is the kind of thing
1345 it operates on; we use them in error messages. */
1347 gen_struct_ref (struct agent_expr
*ax
, struct axs_value
*value
, char *field
,
1348 char *operator_name
, char *operand_name
)
1353 /* Follow pointers until we reach a non-pointer. These aren't the C
1354 semantics, but they're what the normal GDB evaluator does, so we
1355 should at least be consistent. */
1356 while (TYPE_CODE (value
->type
) == TYPE_CODE_PTR
)
1358 gen_usual_unary (ax
, value
);
1359 gen_deref (ax
, value
);
1361 type
= check_typedef (value
->type
);
1363 /* This must yield a structure or a union. */
1364 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
1365 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
1366 error (_("The left operand of `%s' is not a %s."),
1367 operator_name
, operand_name
);
1369 /* And it must be in memory; we don't deal with structure rvalues,
1370 or structures living in registers. */
1371 if (value
->kind
!= axs_lvalue_memory
)
1372 error (_("Structure does not live in memory."));
1374 i
= find_field (type
, field
);
1376 /* Is this a bitfield? */
1377 if (TYPE_FIELD_PACKED (type
, i
))
1378 gen_bitfield_ref (ax
, value
, TYPE_FIELD_TYPE (type
, i
),
1379 TYPE_FIELD_BITPOS (type
, i
),
1380 (TYPE_FIELD_BITPOS (type
, i
)
1381 + TYPE_FIELD_BITSIZE (type
, i
)));
1384 gen_offset (ax
, TYPE_FIELD_BITPOS (type
, i
) / TARGET_CHAR_BIT
);
1385 value
->kind
= axs_lvalue_memory
;
1386 value
->type
= TYPE_FIELD_TYPE (type
, i
);
1391 /* Generate code for GDB's magical `repeat' operator.
1392 LVALUE @ INT creates an array INT elements long, and whose elements
1393 have the same type as LVALUE, located in memory so that LVALUE is
1394 its first element. For example, argv[0]@argc gives you the array
1395 of command-line arguments.
1397 Unfortunately, because we have to know the types before we actually
1398 have a value for the expression, we can't implement this perfectly
1399 without changing the type system, having values that occupy two
1400 stack slots, doing weird things with sizeof, etc. So we require
1401 the right operand to be a constant expression. */
1403 gen_repeat (union exp_element
**pc
, struct agent_expr
*ax
,
1404 struct axs_value
*value
)
1406 struct axs_value value1
;
1407 /* We don't want to turn this into an rvalue, so no conversions
1409 gen_expr (pc
, ax
, &value1
);
1410 if (value1
.kind
!= axs_lvalue_memory
)
1411 error (_("Left operand of `@' must be an object in memory."));
1413 /* Evaluate the length; it had better be a constant. */
1415 struct value
*v
= const_expr (pc
);
1419 error (_("Right operand of `@' must be a constant, in agent expressions."));
1420 if (TYPE_CODE (value_type (v
)) != TYPE_CODE_INT
)
1421 error (_("Right operand of `@' must be an integer."));
1422 length
= value_as_long (v
);
1424 error (_("Right operand of `@' must be positive."));
1426 /* The top of the stack is already the address of the object, so
1427 all we need to do is frob the type of the lvalue. */
1429 /* FIXME-type-allocation: need a way to free this type when we are
1432 = create_range_type (0, builtin_type_int
, 0, length
- 1);
1433 struct type
*array
= create_array_type (0, value1
.type
, range
);
1435 value
->kind
= axs_lvalue_memory
;
1436 value
->type
= array
;
1442 /* Emit code for the `sizeof' operator.
1443 *PC should point at the start of the operand expression; we advance it
1444 to the first instruction after the operand. */
1446 gen_sizeof (union exp_element
**pc
, struct agent_expr
*ax
,
1447 struct axs_value
*value
)
1449 /* We don't care about the value of the operand expression; we only
1450 care about its type. However, in the current arrangement, the
1451 only way to find an expression's type is to generate code for it.
1452 So we generate code for the operand, and then throw it away,
1453 replacing it with code that simply pushes its size. */
1454 int start
= ax
->len
;
1455 gen_expr (pc
, ax
, value
);
1457 /* Throw away the code we just generated. */
1460 ax_const_l (ax
, TYPE_LENGTH (value
->type
));
1461 value
->kind
= axs_rvalue
;
1462 value
->type
= builtin_type_int
;
1466 /* Generating bytecode from GDB expressions: general recursive thingy */
1469 /* A gen_expr function written by a Gen-X'er guy.
1470 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1472 gen_expr (union exp_element
**pc
, struct agent_expr
*ax
,
1473 struct axs_value
*value
)
1475 /* Used to hold the descriptions of operand expressions. */
1476 struct axs_value value1
, value2
;
1477 enum exp_opcode op
= (*pc
)[0].opcode
;
1479 /* If we're looking at a constant expression, just push its value. */
1481 struct value
*v
= maybe_const_expr (pc
);
1485 ax_const_l (ax
, value_as_long (v
));
1486 value
->kind
= axs_rvalue
;
1487 value
->type
= check_typedef (value_type (v
));
1492 /* Otherwise, go ahead and generate code for it. */
1495 /* Binary arithmetic operators. */
1501 case BINOP_SUBSCRIPT
:
1502 case BINOP_BITWISE_AND
:
1503 case BINOP_BITWISE_IOR
:
1504 case BINOP_BITWISE_XOR
:
1506 gen_expr (pc
, ax
, &value1
);
1507 gen_usual_unary (ax
, &value1
);
1508 gen_expr (pc
, ax
, &value2
);
1509 gen_usual_unary (ax
, &value2
);
1510 gen_usual_arithmetic (ax
, &value1
, &value2
);
1514 gen_add (ax
, value
, &value1
, &value2
, "addition");
1517 gen_sub (ax
, value
, &value1
, &value2
);
1520 gen_binop (ax
, value
, &value1
, &value2
,
1521 aop_mul
, aop_mul
, 1, "multiplication");
1524 gen_binop (ax
, value
, &value1
, &value2
,
1525 aop_div_signed
, aop_div_unsigned
, 1, "division");
1528 gen_binop (ax
, value
, &value1
, &value2
,
1529 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
1531 case BINOP_SUBSCRIPT
:
1532 gen_add (ax
, value
, &value1
, &value2
, "array subscripting");
1533 if (TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1534 error (_("Invalid combination of types in array subscripting."));
1535 gen_deref (ax
, value
);
1537 case BINOP_BITWISE_AND
:
1538 gen_binop (ax
, value
, &value1
, &value2
,
1539 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
1542 case BINOP_BITWISE_IOR
:
1543 gen_binop (ax
, value
, &value1
, &value2
,
1544 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
1547 case BINOP_BITWISE_XOR
:
1548 gen_binop (ax
, value
, &value1
, &value2
,
1549 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
1553 /* We should only list operators in the outer case statement
1554 that we actually handle in the inner case statement. */
1555 internal_error (__FILE__
, __LINE__
,
1556 _("gen_expr: op case sets don't match"));
1560 /* Note that we need to be a little subtle about generating code
1561 for comma. In C, we can do some optimizations here because
1562 we know the left operand is only being evaluated for effect.
1563 However, if the tracing kludge is in effect, then we always
1564 need to evaluate the left hand side fully, so that all the
1565 variables it mentions get traced. */
1568 gen_expr (pc
, ax
, &value1
);
1569 /* Don't just dispose of the left operand. We might be tracing,
1570 in which case we want to emit code to trace it if it's an
1572 gen_traced_pop (ax
, &value1
);
1573 gen_expr (pc
, ax
, value
);
1574 /* It's the consumer's responsibility to trace the right operand. */
1577 case OP_LONG
: /* some integer constant */
1579 struct type
*type
= (*pc
)[1].type
;
1580 LONGEST k
= (*pc
)[2].longconst
;
1582 gen_int_literal (ax
, value
, k
, type
);
1587 gen_var_ref (ax
, value
, (*pc
)[2].symbol
);
1593 const char *name
= &(*pc
)[2].string
;
1595 (*pc
) += 4 + BYTES_TO_EXP_ELEM ((*pc
)[1].longconst
+ 1);
1596 reg
= user_reg_map_name_to_regnum (current_gdbarch
,
1597 name
, strlen (name
));
1599 internal_error (__FILE__
, __LINE__
,
1600 _("Register $%s not available"), name
);
1601 if (reg
>= gdbarch_num_regs (current_gdbarch
))
1602 error (_("'%s' is a pseudo-register; "
1603 "GDB cannot yet trace pseudoregister contents."),
1605 value
->kind
= axs_lvalue_register
;
1607 value
->type
= register_type (current_gdbarch
, reg
);
1611 case OP_INTERNALVAR
:
1612 error (_("GDB agent expressions cannot use convenience variables."));
1614 /* Weirdo operator: see comments for gen_repeat for details. */
1616 /* Note that gen_repeat handles its own argument evaluation. */
1618 gen_repeat (pc
, ax
, value
);
1623 struct type
*type
= (*pc
)[1].type
;
1625 gen_expr (pc
, ax
, value
);
1626 gen_cast (ax
, value
, type
);
1632 struct type
*type
= check_typedef ((*pc
)[1].type
);
1634 gen_expr (pc
, ax
, value
);
1635 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1636 it's just a hack for dealing with minsyms; you take some
1637 integer constant, pretend it's the address of an lvalue of
1638 the given type, and dereference it. */
1639 if (value
->kind
!= axs_rvalue
)
1640 /* This would be weird. */
1641 internal_error (__FILE__
, __LINE__
,
1642 _("gen_expr: OP_MEMVAL operand isn't an rvalue???"));
1644 value
->kind
= axs_lvalue_memory
;
1650 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
1651 gen_expr (pc
, ax
, value
);
1652 gen_usual_unary (ax
, value
);
1657 /* -FOO is equivalent to 0 - FOO. */
1658 gen_int_literal (ax
, &value1
, (LONGEST
) 0, builtin_type_int
);
1659 gen_usual_unary (ax
, &value1
); /* shouldn't do much */
1660 gen_expr (pc
, ax
, &value2
);
1661 gen_usual_unary (ax
, &value2
);
1662 gen_usual_arithmetic (ax
, &value1
, &value2
);
1663 gen_sub (ax
, value
, &value1
, &value2
);
1666 case UNOP_LOGICAL_NOT
:
1668 gen_expr (pc
, ax
, value
);
1669 gen_logical_not (ax
, value
);
1672 case UNOP_COMPLEMENT
:
1674 gen_expr (pc
, ax
, value
);
1675 gen_complement (ax
, value
);
1680 gen_expr (pc
, ax
, value
);
1681 gen_usual_unary (ax
, value
);
1682 if (TYPE_CODE (value
->type
) != TYPE_CODE_PTR
)
1683 error (_("Argument of unary `*' is not a pointer."));
1684 gen_deref (ax
, value
);
1689 gen_expr (pc
, ax
, value
);
1690 gen_address_of (ax
, value
);
1695 /* Notice that gen_sizeof handles its own operand, unlike most
1696 of the other unary operator functions. This is because we
1697 have to throw away the code we generate. */
1698 gen_sizeof (pc
, ax
, value
);
1701 case STRUCTOP_STRUCT
:
1704 int length
= (*pc
)[1].longconst
;
1705 char *name
= &(*pc
)[2].string
;
1707 (*pc
) += 4 + BYTES_TO_EXP_ELEM (length
+ 1);
1708 gen_expr (pc
, ax
, value
);
1709 if (op
== STRUCTOP_STRUCT
)
1710 gen_struct_ref (ax
, value
, name
, ".", "structure or union");
1711 else if (op
== STRUCTOP_PTR
)
1712 gen_struct_ref (ax
, value
, name
, "->",
1713 "pointer to a structure or union");
1715 /* If this `if' chain doesn't handle it, then the case list
1716 shouldn't mention it, and we shouldn't be here. */
1717 internal_error (__FILE__
, __LINE__
,
1718 _("gen_expr: unhandled struct case"));
1723 error (_("Attempt to use a type name as an expression."));
1726 error (_("Unsupported operator in expression."));
1732 /* Generating bytecode from GDB expressions: driver */
1734 /* Given a GDB expression EXPR, return bytecode to trace its value.
1735 The result will use the `trace' and `trace_quick' bytecodes to
1736 record the value of all memory touched by the expression. The
1737 caller can then use the ax_reqs function to discover which
1738 registers it relies upon. */
1740 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
)
1742 struct cleanup
*old_chain
= 0;
1743 struct agent_expr
*ax
= new_agent_expr (scope
);
1744 union exp_element
*pc
;
1745 struct axs_value value
;
1747 old_chain
= make_cleanup_free_agent_expr (ax
);
1751 gen_expr (&pc
, ax
, &value
);
1753 /* Make sure we record the final object, and get rid of it. */
1754 gen_traced_pop (ax
, &value
);
1756 /* Oh, and terminate. */
1757 ax_simple (ax
, aop_end
);
1759 /* We have successfully built the agent expr, so cancel the cleanup
1760 request. If we add more cleanups that we always want done, this
1761 will have to get more complicated. */
1762 discard_cleanups (old_chain
);
1767 agent_command (char *exp
, int from_tty
)
1769 struct cleanup
*old_chain
= 0;
1770 struct expression
*expr
;
1771 struct agent_expr
*agent
;
1772 struct frame_info
*fi
= get_current_frame (); /* need current scope */
1774 /* We don't deal with overlay debugging at the moment. We need to
1775 think more carefully about this. If you copy this code into
1776 another command, change the error message; the user shouldn't
1777 have to know anything about agent expressions. */
1778 if (overlay_debugging
)
1779 error (_("GDB can't do agent expression translation with overlays."));
1782 error_no_arg (_("expression to translate"));
1784 expr
= parse_expression (exp
);
1785 old_chain
= make_cleanup (free_current_contents
, &expr
);
1786 agent
= gen_trace_for_expr (get_frame_pc (fi
), expr
);
1787 make_cleanup_free_agent_expr (agent
);
1788 ax_print (gdb_stdout
, agent
);
1790 /* It would be nice to call ax_reqs here to gather some general info
1791 about the expression, and then print out the result. */
1793 do_cleanups (old_chain
);
1798 /* Initialization code. */
1800 void _initialize_ax_gdb (void);
1802 _initialize_ax_gdb (void)
1804 add_cmd ("agent", class_maintenance
, agent_command
,
1805 _("Translate an expression into remote agent bytecode."),