1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2007
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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
28 #include "expression.h"
35 #include "gdb_string.h"
39 /* To make sense of this file, you should read doc/agentexpr.texi.
40 Then look at the types and enums in ax-gdb.h. For the code itself,
41 look at gen_expr, towards the bottom; that's the main function that
42 looks at the GDB expressions and calls everything else to generate
45 I'm beginning to wonder whether it wouldn't be nicer to internally
46 generate trees, with types, and then spit out the bytecode in
47 linear form afterwards; we could generate fewer `swap', `ext', and
48 `zero_ext' bytecodes that way; it would make good constant folding
49 easier, too. But at the moment, I think we should be willing to
50 pay for the simplicity of this code with less-than-optimal bytecode
53 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
57 /* Prototypes for local functions. */
59 /* There's a standard order to the arguments of these functions:
60 union exp_element ** --- pointer into expression
61 struct agent_expr * --- agent expression buffer to generate code into
62 struct axs_value * --- describes value left on top of stack */
64 static struct value *const_var_ref (struct symbol *var);
65 static struct value *const_expr (union exp_element **pc);
66 static struct value *maybe_const_expr (union exp_element **pc);
68 static void gen_traced_pop (struct agent_expr *, struct axs_value *);
70 static void gen_sign_extend (struct agent_expr *, struct type *);
71 static void gen_extend (struct agent_expr *, struct type *);
72 static void gen_fetch (struct agent_expr *, struct type *);
73 static void gen_left_shift (struct agent_expr *, int);
76 static void gen_frame_args_address (struct agent_expr *);
77 static void gen_frame_locals_address (struct agent_expr *);
78 static void gen_offset (struct agent_expr *ax, int offset);
79 static void gen_sym_offset (struct agent_expr *, struct symbol *);
80 static void gen_var_ref (struct agent_expr *ax,
81 struct axs_value *value, struct symbol *var);
84 static void gen_int_literal (struct agent_expr *ax,
85 struct axs_value *value,
86 LONGEST k, struct type *type);
89 static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
90 static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
91 static int type_wider_than (struct type *type1, struct type *type2);
92 static struct type *max_type (struct type *type1, struct type *type2);
93 static void gen_conversion (struct agent_expr *ax,
94 struct type *from, struct type *to);
95 static int is_nontrivial_conversion (struct type *from, struct type *to);
96 static void gen_usual_arithmetic (struct agent_expr *ax,
97 struct axs_value *value1,
98 struct axs_value *value2);
99 static void gen_integral_promotions (struct agent_expr *ax,
100 struct axs_value *value);
101 static void gen_cast (struct agent_expr *ax,
102 struct axs_value *value, struct type *type);
103 static void gen_scale (struct agent_expr *ax,
104 enum agent_op op, struct type *type);
105 static void gen_add (struct agent_expr *ax,
106 struct axs_value *value,
107 struct axs_value *value1,
108 struct axs_value *value2, char *name);
109 static void gen_sub (struct agent_expr *ax,
110 struct axs_value *value,
111 struct axs_value *value1, struct axs_value *value2);
112 static void gen_binop (struct agent_expr *ax,
113 struct axs_value *value,
114 struct axs_value *value1,
115 struct axs_value *value2,
117 enum agent_op op_unsigned, int may_carry, char *name);
118 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value);
119 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
120 static void gen_deref (struct agent_expr *, struct axs_value *);
121 static void gen_address_of (struct agent_expr *, struct axs_value *);
122 static int find_field (struct type *type, char *name);
123 static void gen_bitfield_ref (struct agent_expr *ax,
124 struct axs_value *value,
125 struct type *type, int start, int end);
126 static void gen_struct_ref (struct agent_expr *ax,
127 struct axs_value *value,
129 char *operator_name, char *operand_name);
130 static void gen_repeat (union exp_element **pc,
131 struct agent_expr *ax, struct axs_value *value);
132 static void gen_sizeof (union exp_element **pc,
133 struct agent_expr *ax, struct axs_value *value);
134 static void gen_expr (union exp_element **pc,
135 struct agent_expr *ax, struct axs_value *value);
137 static void agent_command (char *exp, int from_tty);
140 /* Detecting constant expressions. */
142 /* If the variable reference at *PC is a constant, return its value.
143 Otherwise, return zero.
145 Hey, Wally! How can a variable reference be a constant?
147 Well, Beav, this function really handles the OP_VAR_VALUE operator,
148 not specifically variable references. GDB uses OP_VAR_VALUE to
149 refer to any kind of symbolic reference: function names, enum
150 elements, and goto labels are all handled through the OP_VAR_VALUE
151 operator, even though they're constants. It makes sense given the
154 Gee, Wally, don'cha wonder sometimes if data representations that
155 subvert commonly accepted definitions of terms in favor of heavily
156 context-specific interpretations are really just a tool of the
157 programming hegemony to preserve their power and exclude the
160 static struct value *
161 const_var_ref (struct symbol *var)
163 struct type *type = SYMBOL_TYPE (var);
165 switch (SYMBOL_CLASS (var))
168 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
171 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
179 /* If the expression starting at *PC has a constant value, return it.
180 Otherwise, return zero. If we return a value, then *PC will be
181 advanced to the end of it. If we return zero, *PC could be
183 static struct value *
184 const_expr (union exp_element **pc)
186 enum exp_opcode op = (*pc)->opcode;
193 struct type *type = (*pc)[1].type;
194 LONGEST k = (*pc)[2].longconst;
196 return value_from_longest (type, k);
201 struct value *v = const_var_ref ((*pc)[2].symbol);
206 /* We could add more operators in here. */
210 v1 = const_expr (pc);
212 return value_neg (v1);
222 /* Like const_expr, but guarantee also that *PC is undisturbed if the
223 expression is not constant. */
224 static struct value *
225 maybe_const_expr (union exp_element **pc)
227 union exp_element *tentative_pc = *pc;
228 struct value *v = const_expr (&tentative_pc);
230 /* If we got a value, then update the real PC. */
238 /* Generating bytecode from GDB expressions: general assumptions */
240 /* Here are a few general assumptions made throughout the code; if you
241 want to make a change that contradicts one of these, then you'd
242 better scan things pretty thoroughly.
244 - We assume that all values occupy one stack element. For example,
245 sometimes we'll swap to get at the left argument to a binary
246 operator. If we decide that void values should occupy no stack
247 elements, or that synthetic arrays (whose size is determined at
248 run time, created by the `@' operator) should occupy two stack
249 elements (address and length), then this will cause trouble.
251 - We assume the stack elements are infinitely wide, and that we
252 don't have to worry what happens if the user requests an
253 operation that is wider than the actual interpreter's stack.
254 That is, it's up to the interpreter to handle directly all the
255 integer widths the user has access to. (Woe betide the language
258 - We don't support side effects. Thus, we don't have to worry about
259 GCC's generalized lvalues, function calls, etc.
261 - We don't support floating point. Many places where we switch on
262 some type don't bother to include cases for floating point; there
263 may be even more subtle ways this assumption exists. For
264 example, the arguments to % must be integers.
266 - We assume all subexpressions have a static, unchanging type. If
267 we tried to support convenience variables, this would be a
270 - All values on the stack should always be fully zero- or
273 (I wasn't sure whether to choose this or its opposite --- that
274 only addresses are assumed extended --- but it turns out that
275 neither convention completely eliminates spurious extend
276 operations (if everything is always extended, then you have to
277 extend after add, because it could overflow; if nothing is
278 extended, then you end up producing extends whenever you change
279 sizes), and this is simpler.) */
282 /* Generating bytecode from GDB expressions: the `trace' kludge */
284 /* The compiler in this file is a general-purpose mechanism for
285 translating GDB expressions into bytecode. One ought to be able to
286 find a million and one uses for it.
288 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
289 of expediency. Let he who is without sin cast the first stone.
291 For the data tracing facility, we need to insert `trace' bytecodes
292 before each data fetch; this records all the memory that the
293 expression touches in the course of evaluation, so that memory will
294 be available when the user later tries to evaluate the expression
297 This should be done (I think) in a post-processing pass, that walks
298 an arbitrary agent expression and inserts `trace' operations at the
299 appropriate points. But it's much faster to just hack them
300 directly into the code. And since we're in a crunch, that's what
303 Setting the flag trace_kludge to non-zero enables the code that
304 emits the trace bytecodes at the appropriate points. */
305 static int trace_kludge;
307 /* Trace the lvalue on the stack, if it needs it. In either case, pop
308 the value. Useful on the left side of a comma, and at the end of
309 an expression being used for tracing. */
311 gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
317 /* We don't trace rvalues, just the lvalues necessary to
318 produce them. So just dispose of this value. */
319 ax_simple (ax, aop_pop);
322 case axs_lvalue_memory:
324 int length = TYPE_LENGTH (value->type);
326 /* There's no point in trying to use a trace_quick bytecode
327 here, since "trace_quick SIZE pop" is three bytes, whereas
328 "const8 SIZE trace" is also three bytes, does the same
329 thing, and the simplest code which generates that will also
330 work correctly for objects with large sizes. */
331 ax_const_l (ax, length);
332 ax_simple (ax, aop_trace);
336 case axs_lvalue_register:
337 /* We need to mention the register somewhere in the bytecode,
338 so ax_reqs will pick it up and add it to the mask of
340 ax_reg (ax, value->u.reg);
341 ax_simple (ax, aop_pop);
345 /* If we're not tracing, just pop the value. */
346 ax_simple (ax, aop_pop);
351 /* Generating bytecode from GDB expressions: helper functions */
353 /* Assume that the lower bits of the top of the stack is a value of
354 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
356 gen_sign_extend (struct agent_expr *ax, struct type *type)
358 /* Do we need to sign-extend this? */
359 if (!TYPE_UNSIGNED (type))
360 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
364 /* Assume the lower bits of the top of the stack hold a value of type
365 TYPE, and the upper bits are garbage. Sign-extend or truncate as
368 gen_extend (struct agent_expr *ax, struct type *type)
370 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
372 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
376 /* Assume that the top of the stack contains a value of type "pointer
377 to TYPE"; generate code to fetch its value. Note that TYPE is the
378 target type, not the pointer type. */
380 gen_fetch (struct agent_expr *ax, struct type *type)
384 /* Record the area of memory we're about to fetch. */
385 ax_trace_quick (ax, TYPE_LENGTH (type));
388 switch (TYPE_CODE (type))
394 /* It's a scalar value, so we know how to dereference it. How
395 many bytes long is it? */
396 switch (TYPE_LENGTH (type))
398 case 8 / TARGET_CHAR_BIT:
399 ax_simple (ax, aop_ref8);
401 case 16 / TARGET_CHAR_BIT:
402 ax_simple (ax, aop_ref16);
404 case 32 / TARGET_CHAR_BIT:
405 ax_simple (ax, aop_ref32);
407 case 64 / TARGET_CHAR_BIT:
408 ax_simple (ax, aop_ref64);
411 /* Either our caller shouldn't have asked us to dereference
412 that pointer (other code's fault), or we're not
413 implementing something we should be (this code's fault).
414 In any case, it's a bug the user shouldn't see. */
416 internal_error (__FILE__, __LINE__,
417 _("gen_fetch: strange size"));
420 gen_sign_extend (ax, type);
424 /* Either our caller shouldn't have asked us to dereference that
425 pointer (other code's fault), or we're not implementing
426 something we should be (this code's fault). In any case,
427 it's a bug the user shouldn't see. */
428 internal_error (__FILE__, __LINE__,
429 _("gen_fetch: bad type code"));
434 /* Generate code to left shift the top of the stack by DISTANCE bits, or
435 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
436 unsigned (logical) right shifts. */
438 gen_left_shift (struct agent_expr *ax, int distance)
442 ax_const_l (ax, distance);
443 ax_simple (ax, aop_lsh);
445 else if (distance < 0)
447 ax_const_l (ax, -distance);
448 ax_simple (ax, aop_rsh_unsigned);
454 /* Generating bytecode from GDB expressions: symbol references */
456 /* Generate code to push the base address of the argument portion of
457 the top stack frame. */
459 gen_frame_args_address (struct agent_expr *ax)
462 LONGEST frame_offset;
464 gdbarch_virtual_frame_pointer (current_gdbarch,
465 ax->scope, &frame_reg, &frame_offset);
466 ax_reg (ax, frame_reg);
467 gen_offset (ax, frame_offset);
471 /* Generate code to push the base address of the locals portion of the
474 gen_frame_locals_address (struct agent_expr *ax)
477 LONGEST frame_offset;
479 gdbarch_virtual_frame_pointer (current_gdbarch,
480 ax->scope, &frame_reg, &frame_offset);
481 ax_reg (ax, frame_reg);
482 gen_offset (ax, frame_offset);
486 /* Generate code to add OFFSET to the top of the stack. Try to
487 generate short and readable code. We use this for getting to
488 variables on the stack, and structure members. If we were
489 programming in ML, it would be clearer why these are the same
492 gen_offset (struct agent_expr *ax, int offset)
494 /* It would suffice to simply push the offset and add it, but this
495 makes it easier to read positive and negative offsets in the
499 ax_const_l (ax, offset);
500 ax_simple (ax, aop_add);
504 ax_const_l (ax, -offset);
505 ax_simple (ax, aop_sub);
510 /* In many cases, a symbol's value is the offset from some other
511 address (stack frame, base register, etc.) Generate code to add
512 VAR's value to the top of the stack. */
514 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
516 gen_offset (ax, SYMBOL_VALUE (var));
520 /* Generate code for a variable reference to AX. The variable is the
521 symbol VAR. Set VALUE to describe the result. */
524 gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
526 /* Dereference any typedefs. */
527 value->type = check_typedef (SYMBOL_TYPE (var));
529 /* I'm imitating the code in read_var_value. */
530 switch (SYMBOL_CLASS (var))
532 case LOC_CONST: /* A constant, like an enum value. */
533 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
534 value->kind = axs_rvalue;
537 case LOC_LABEL: /* A goto label, being used as a value. */
538 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
539 value->kind = axs_rvalue;
542 case LOC_CONST_BYTES:
543 internal_error (__FILE__, __LINE__,
544 _("gen_var_ref: LOC_CONST_BYTES symbols are not supported"));
546 /* Variable at a fixed location in memory. Easy. */
548 /* Push the address of the variable. */
549 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
550 value->kind = axs_lvalue_memory;
553 case LOC_ARG: /* var lives in argument area of frame */
554 gen_frame_args_address (ax);
555 gen_sym_offset (ax, var);
556 value->kind = axs_lvalue_memory;
559 case LOC_REF_ARG: /* As above, but the frame slot really
560 holds the address of the variable. */
561 gen_frame_args_address (ax);
562 gen_sym_offset (ax, var);
563 /* Don't assume any particular pointer size. */
564 gen_fetch (ax, lookup_pointer_type (builtin_type_void));
565 value->kind = axs_lvalue_memory;
568 case LOC_LOCAL: /* var lives in locals area of frame */
570 gen_frame_locals_address (ax);
571 gen_sym_offset (ax, var);
572 value->kind = axs_lvalue_memory;
575 case LOC_BASEREG: /* relative to some base register */
576 case LOC_BASEREG_ARG:
577 ax_reg (ax, SYMBOL_BASEREG (var));
578 gen_sym_offset (ax, var);
579 value->kind = axs_lvalue_memory;
583 error (_("Cannot compute value of typedef `%s'."),
584 SYMBOL_PRINT_NAME (var));
588 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
589 value->kind = axs_rvalue;
594 /* Don't generate any code at all; in the process of treating
595 this as an lvalue or rvalue, the caller will generate the
597 value->kind = axs_lvalue_register;
598 value->u.reg = SYMBOL_VALUE (var);
601 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
602 register, not on the stack. Simpler than LOC_REGISTER and
603 LOC_REGPARM, because it's just like any other case where the
604 thing has a real address. */
605 case LOC_REGPARM_ADDR:
606 ax_reg (ax, SYMBOL_VALUE (var));
607 value->kind = axs_lvalue_memory;
612 struct minimal_symbol *msym
613 = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (var), NULL, NULL);
615 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
617 /* Push the address of the variable. */
618 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
619 value->kind = axs_lvalue_memory;
624 case LOC_COMPUTED_ARG:
625 /* FIXME: cagney/2004-01-26: It should be possible to
626 unconditionally call the SYMBOL_OPS method when available.
627 Unfortunately DWARF 2 stores the frame-base (instead of the
628 function) location in a function's symbol. Oops! For the
629 moment enable this when/where applicable. */
630 SYMBOL_OPS (var)->tracepoint_var_ref (var, ax, value);
633 case LOC_OPTIMIZED_OUT:
634 error (_("The variable `%s' has been optimized out."),
635 SYMBOL_PRINT_NAME (var));
639 error (_("Cannot find value of botched symbol `%s'."),
640 SYMBOL_PRINT_NAME (var));
647 /* Generating bytecode from GDB expressions: literals */
650 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
654 value->kind = axs_rvalue;
660 /* Generating bytecode from GDB expressions: unary conversions, casts */
662 /* Take what's on the top of the stack (as described by VALUE), and
663 try to make an rvalue out of it. Signal an error if we can't do
666 require_rvalue (struct agent_expr *ax, struct axs_value *value)
671 /* It's already an rvalue. */
674 case axs_lvalue_memory:
675 /* The top of stack is the address of the object. Dereference. */
676 gen_fetch (ax, value->type);
679 case axs_lvalue_register:
680 /* There's nothing on the stack, but value->u.reg is the
681 register number containing the value.
683 When we add floating-point support, this is going to have to
684 change. What about SPARC register pairs, for example? */
685 ax_reg (ax, value->u.reg);
686 gen_extend (ax, value->type);
690 value->kind = axs_rvalue;
694 /* Assume the top of the stack is described by VALUE, and perform the
695 usual unary conversions. This is motivated by ANSI 6.2.2, but of
696 course GDB expressions are not ANSI; they're the mishmash union of
697 a bunch of languages. Rah.
699 NOTE! This function promises to produce an rvalue only when the
700 incoming value is of an appropriate type. In other words, the
701 consumer of the value this function produces may assume the value
702 is an rvalue only after checking its type.
704 The immediate issue is that if the user tries to use a structure or
705 union as an operand of, say, the `+' operator, we don't want to try
706 to convert that structure to an rvalue; require_rvalue will bomb on
707 structs and unions. Rather, we want to simply pass the struct
708 lvalue through unchanged, and let `+' raise an error. */
711 gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
713 /* We don't have to generate any code for the usual integral
714 conversions, since values are always represented as full-width on
715 the stack. Should we tweak the type? */
717 /* Some types require special handling. */
718 switch (TYPE_CODE (value->type))
720 /* Functions get converted to a pointer to the function. */
722 value->type = lookup_pointer_type (value->type);
723 value->kind = axs_rvalue; /* Should always be true, but just in case. */
726 /* Arrays get converted to a pointer to their first element, and
727 are no longer an lvalue. */
728 case TYPE_CODE_ARRAY:
730 struct type *elements = TYPE_TARGET_TYPE (value->type);
731 value->type = lookup_pointer_type (elements);
732 value->kind = axs_rvalue;
733 /* We don't need to generate any code; the address of the array
734 is also the address of its first element. */
738 /* Don't try to convert structures and unions to rvalues. Let the
739 consumer signal an error. */
740 case TYPE_CODE_STRUCT:
741 case TYPE_CODE_UNION:
744 /* If the value is an enum, call it an integer. */
746 value->type = builtin_type_int;
750 /* If the value is an lvalue, dereference it. */
751 require_rvalue (ax, value);
755 /* Return non-zero iff the type TYPE1 is considered "wider" than the
756 type TYPE2, according to the rules described in gen_usual_arithmetic. */
758 type_wider_than (struct type *type1, struct type *type2)
760 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
761 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
762 && TYPE_UNSIGNED (type1)
763 && !TYPE_UNSIGNED (type2)));
767 /* Return the "wider" of the two types TYPE1 and TYPE2. */
769 max_type (struct type *type1, struct type *type2)
771 return type_wider_than (type1, type2) ? type1 : type2;
775 /* Generate code to convert a scalar value of type FROM to type TO. */
777 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
779 /* Perhaps there is a more graceful way to state these rules. */
781 /* If we're converting to a narrower type, then we need to clear out
783 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
784 gen_extend (ax, from);
786 /* If the two values have equal width, but different signednesses,
787 then we need to extend. */
788 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
790 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
794 /* If we're converting to a wider type, and becoming unsigned, then
795 we need to zero out any possible sign bits. */
796 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
798 if (TYPE_UNSIGNED (to))
804 /* Return non-zero iff the type FROM will require any bytecodes to be
805 emitted to be converted to the type TO. */
807 is_nontrivial_conversion (struct type *from, struct type *to)
809 struct agent_expr *ax = new_agent_expr (0);
812 /* Actually generate the code, and see if anything came out. At the
813 moment, it would be trivial to replicate the code in
814 gen_conversion here, but in the future, when we're supporting
815 floating point and the like, it may not be. Doing things this
816 way allows this function to be independent of the logic in
818 gen_conversion (ax, from, to);
819 nontrivial = ax->len > 0;
820 free_agent_expr (ax);
825 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
826 6.2.1.5) for the two operands of an arithmetic operator. This
827 effectively finds a "least upper bound" type for the two arguments,
828 and promotes each argument to that type. *VALUE1 and *VALUE2
829 describe the values as they are passed in, and as they are left. */
831 gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
832 struct axs_value *value2)
834 /* Do the usual binary conversions. */
835 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
836 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
838 /* The ANSI integral promotions seem to work this way: Order the
839 integer types by size, and then by signedness: an n-bit
840 unsigned type is considered "wider" than an n-bit signed
841 type. Promote to the "wider" of the two types, and always
842 promote at least to int. */
843 struct type *target = max_type (builtin_type_int,
844 max_type (value1->type, value2->type));
846 /* Deal with value2, on the top of the stack. */
847 gen_conversion (ax, value2->type, target);
849 /* Deal with value1, not on the top of the stack. Don't
850 generate the `swap' instructions if we're not actually going
852 if (is_nontrivial_conversion (value1->type, target))
854 ax_simple (ax, aop_swap);
855 gen_conversion (ax, value1->type, target);
856 ax_simple (ax, aop_swap);
859 value1->type = value2->type = target;
864 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
865 the value on the top of the stack, as described by VALUE. Assume
866 the value has integral type. */
868 gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
870 if (!type_wider_than (value->type, builtin_type_int))
872 gen_conversion (ax, value->type, builtin_type_int);
873 value->type = builtin_type_int;
875 else if (!type_wider_than (value->type, builtin_type_unsigned_int))
877 gen_conversion (ax, value->type, builtin_type_unsigned_int);
878 value->type = builtin_type_unsigned_int;
883 /* Generate code for a cast to TYPE. */
885 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
887 /* GCC does allow casts to yield lvalues, so this should be fixed
888 before merging these changes into the trunk. */
889 require_rvalue (ax, value);
890 /* Dereference typedefs. */
891 type = check_typedef (type);
893 switch (TYPE_CODE (type))
896 /* It's implementation-defined, and I'll bet this is what GCC
900 case TYPE_CODE_ARRAY:
901 case TYPE_CODE_STRUCT:
902 case TYPE_CODE_UNION:
904 error (_("Invalid type cast: intended type must be scalar."));
907 /* We don't have to worry about the size of the value, because
908 all our integral values are fully sign-extended, and when
909 casting pointers we can do anything we like. Is there any
910 way for us to actually know what GCC actually does with a
916 gen_conversion (ax, value->type, type);
920 /* We could pop the value, and rely on everyone else to check
921 the type and notice that this value doesn't occupy a stack
922 slot. But for now, leave the value on the stack, and
923 preserve the "value == stack element" assumption. */
927 error (_("Casts to requested type are not yet implemented."));
935 /* Generating bytecode from GDB expressions: arithmetic */
937 /* Scale the integer on the top of the stack by the size of the target
938 of the pointer type TYPE. */
940 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
942 struct type *element = TYPE_TARGET_TYPE (type);
944 if (TYPE_LENGTH (element) != 1)
946 ax_const_l (ax, TYPE_LENGTH (element));
952 /* Generate code for an addition; non-trivial because we deal with
953 pointer arithmetic. We set VALUE to describe the result value; we
954 assume VALUE1 and VALUE2 describe the two operands, and that
955 they've undergone the usual binary conversions. Used by both
956 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
958 gen_add (struct agent_expr *ax, struct axs_value *value,
959 struct axs_value *value1, struct axs_value *value2, char *name)
962 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
963 && TYPE_CODE (value2->type) == TYPE_CODE_PTR)
965 /* Swap the values and proceed normally. */
966 ax_simple (ax, aop_swap);
967 gen_scale (ax, aop_mul, value2->type);
968 ax_simple (ax, aop_add);
969 gen_extend (ax, value2->type); /* Catch overflow. */
970 value->type = value2->type;
974 else if (TYPE_CODE (value1->type) == TYPE_CODE_PTR
975 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
977 gen_scale (ax, aop_mul, value1->type);
978 ax_simple (ax, aop_add);
979 gen_extend (ax, value1->type); /* Catch overflow. */
980 value->type = value1->type;
983 /* Must be number + number; the usual binary conversions will have
984 brought them both to the same width. */
985 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
986 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
988 ax_simple (ax, aop_add);
989 gen_extend (ax, value1->type); /* Catch overflow. */
990 value->type = value1->type;
994 error (_("Invalid combination of types in %s."), name);
996 value->kind = axs_rvalue;
1000 /* Generate code for an addition; non-trivial because we have to deal
1001 with pointer arithmetic. We set VALUE to describe the result
1002 value; we assume VALUE1 and VALUE2 describe the two operands, and
1003 that they've undergone the usual binary conversions. */
1005 gen_sub (struct agent_expr *ax, struct axs_value *value,
1006 struct axs_value *value1, struct axs_value *value2)
1008 if (TYPE_CODE (value1->type) == TYPE_CODE_PTR)
1010 /* Is it PTR - INT? */
1011 if (TYPE_CODE (value2->type) == TYPE_CODE_INT)
1013 gen_scale (ax, aop_mul, value1->type);
1014 ax_simple (ax, aop_sub);
1015 gen_extend (ax, value1->type); /* Catch overflow. */
1016 value->type = value1->type;
1019 /* Is it PTR - PTR? Strictly speaking, the types ought to
1020 match, but this is what the normal GDB expression evaluator
1022 else if (TYPE_CODE (value2->type) == TYPE_CODE_PTR
1023 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1024 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1026 ax_simple (ax, aop_sub);
1027 gen_scale (ax, aop_div_unsigned, value1->type);
1028 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
1032 First argument of `-' is a pointer, but second argument is neither\n\
1033 an integer nor a pointer of the same type."));
1036 /* Must be number + number. */
1037 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
1038 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
1040 ax_simple (ax, aop_sub);
1041 gen_extend (ax, value1->type); /* Catch overflow. */
1042 value->type = value1->type;
1046 error (_("Invalid combination of types in subtraction."));
1048 value->kind = axs_rvalue;
1051 /* Generate code for a binary operator that doesn't do pointer magic.
1052 We set VALUE to describe the result value; we assume VALUE1 and
1053 VALUE2 describe the two operands, and that they've undergone the
1054 usual binary conversions. MAY_CARRY should be non-zero iff the
1055 result needs to be extended. NAME is the English name of the
1056 operator, used in error messages */
1058 gen_binop (struct agent_expr *ax, struct axs_value *value,
1059 struct axs_value *value1, struct axs_value *value2, enum agent_op op,
1060 enum agent_op op_unsigned, int may_carry, char *name)
1062 /* We only handle INT op INT. */
1063 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1064 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1065 error (_("Invalid combination of types in %s."), name);
1068 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1070 gen_extend (ax, value1->type); /* catch overflow */
1071 value->type = value1->type;
1072 value->kind = axs_rvalue;
1077 gen_logical_not (struct agent_expr *ax, struct axs_value *value)
1079 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1080 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1081 error (_("Invalid type of operand to `!'."));
1083 gen_usual_unary (ax, value);
1084 ax_simple (ax, aop_log_not);
1085 value->type = builtin_type_int;
1090 gen_complement (struct agent_expr *ax, struct axs_value *value)
1092 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1093 error (_("Invalid type of operand to `~'."));
1095 gen_usual_unary (ax, value);
1096 gen_integral_promotions (ax, value);
1097 ax_simple (ax, aop_bit_not);
1098 gen_extend (ax, value->type);
1103 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1105 /* Dereference the value on the top of the stack. */
1107 gen_deref (struct agent_expr *ax, struct axs_value *value)
1109 /* The caller should check the type, because several operators use
1110 this, and we don't know what error message to generate. */
1111 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1112 internal_error (__FILE__, __LINE__,
1113 _("gen_deref: expected a pointer"));
1115 /* We've got an rvalue now, which is a pointer. We want to yield an
1116 lvalue, whose address is exactly that pointer. So we don't
1117 actually emit any code; we just change the type from "Pointer to
1118 T" to "T", and mark the value as an lvalue in memory. Leave it
1119 to the consumer to actually dereference it. */
1120 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1121 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1122 ? axs_rvalue : axs_lvalue_memory);
1126 /* Produce the address of the lvalue on the top of the stack. */
1128 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1130 /* Special case for taking the address of a function. The ANSI
1131 standard describes this as a special case, too, so this
1132 arrangement is not without motivation. */
1133 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1134 /* The value's already an rvalue on the stack, so we just need to
1136 value->type = lookup_pointer_type (value->type);
1138 switch (value->kind)
1141 error (_("Operand of `&' is an rvalue, which has no address."));
1143 case axs_lvalue_register:
1144 error (_("Operand of `&' is in a register, and has no address."));
1146 case axs_lvalue_memory:
1147 value->kind = axs_rvalue;
1148 value->type = lookup_pointer_type (value->type);
1154 /* A lot of this stuff will have to change to support C++. But we're
1155 not going to deal with that at the moment. */
1157 /* Find the field in the structure type TYPE named NAME, and return
1158 its index in TYPE's field array. */
1160 find_field (struct type *type, char *name)
1164 CHECK_TYPEDEF (type);
1166 /* Make sure this isn't C++. */
1167 if (TYPE_N_BASECLASSES (type) != 0)
1168 internal_error (__FILE__, __LINE__,
1169 _("find_field: derived classes supported"));
1171 for (i = 0; i < TYPE_NFIELDS (type); i++)
1173 char *this_name = TYPE_FIELD_NAME (type, i);
1175 if (this_name && strcmp (name, this_name) == 0)
1178 if (this_name[0] == '\0')
1179 internal_error (__FILE__, __LINE__,
1180 _("find_field: anonymous unions not supported"));
1183 error (_("Couldn't find member named `%s' in struct/union `%s'"),
1184 name, TYPE_TAG_NAME (type));
1190 /* Generate code to push the value of a bitfield of a structure whose
1191 address is on the top of the stack. START and END give the
1192 starting and one-past-ending *bit* numbers of the field within the
1195 gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1196 struct type *type, int start, int end)
1198 /* Note that ops[i] fetches 8 << i bits. */
1199 static enum agent_op ops[]
1201 {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1202 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1204 /* We don't want to touch any byte that the bitfield doesn't
1205 actually occupy; we shouldn't make any accesses we're not
1206 explicitly permitted to. We rely here on the fact that the
1207 bytecode `ref' operators work on unaligned addresses.
1209 It takes some fancy footwork to get the stack to work the way
1210 we'd like. Say we're retrieving a bitfield that requires three
1211 fetches. Initially, the stack just contains the address:
1213 For the first fetch, we duplicate the address
1215 then add the byte offset, do the fetch, and shift and mask as
1216 needed, yielding a fragment of the value, properly aligned for
1217 the final bitwise or:
1219 then we swap, and repeat the process:
1220 frag1 addr --- address on top
1221 frag1 addr addr --- duplicate it
1222 frag1 addr frag2 --- get second fragment
1223 frag1 frag2 addr --- swap again
1224 frag1 frag2 frag3 --- get third fragment
1225 Notice that, since the third fragment is the last one, we don't
1226 bother duplicating the address this time. Now we have all the
1227 fragments on the stack, and we can simply `or' them together,
1228 yielding the final value of the bitfield. */
1230 /* The first and one-after-last bits in the field, but rounded down
1231 and up to byte boundaries. */
1232 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1233 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1237 /* current bit offset within the structure */
1240 /* The index in ops of the opcode we're considering. */
1243 /* The number of fragments we generated in the process. Probably
1244 equal to the number of `one' bits in bytesize, but who cares? */
1247 /* Dereference any typedefs. */
1248 type = check_typedef (type);
1250 /* Can we fetch the number of bits requested at all? */
1251 if ((end - start) > ((1 << num_ops) * 8))
1252 internal_error (__FILE__, __LINE__,
1253 _("gen_bitfield_ref: bitfield too wide"));
1255 /* Note that we know here that we only need to try each opcode once.
1256 That may not be true on machines with weird byte sizes. */
1257 offset = bound_start;
1259 for (op = num_ops - 1; op >= 0; op--)
1261 /* number of bits that ops[op] would fetch */
1262 int op_size = 8 << op;
1264 /* The stack at this point, from bottom to top, contains zero or
1265 more fragments, then the address. */
1267 /* Does this fetch fit within the bitfield? */
1268 if (offset + op_size <= bound_end)
1270 /* Is this the last fragment? */
1271 int last_frag = (offset + op_size == bound_end);
1274 ax_simple (ax, aop_dup); /* keep a copy of the address */
1276 /* Add the offset. */
1277 gen_offset (ax, offset / TARGET_CHAR_BIT);
1281 /* Record the area of memory we're about to fetch. */
1282 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1285 /* Perform the fetch. */
1286 ax_simple (ax, ops[op]);
1288 /* Shift the bits we have to their proper position.
1289 gen_left_shift will generate right shifts when the operand
1292 A big-endian field diagram to ponder:
1293 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1294 +------++------++------++------++------++------++------++------+
1295 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1297 bit number 16 32 48 53
1298 These are bit numbers as supplied by GDB. Note that the
1299 bit numbers run from right to left once you've fetched the
1302 A little-endian field diagram to ponder:
1303 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1304 +------++------++------++------++------++------++------++------+
1305 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1307 bit number 48 32 16 4 0
1309 In both cases, the most significant end is on the left
1310 (i.e. normal numeric writing order), which means that you
1311 don't go crazy thinking about `left' and `right' shifts.
1313 We don't have to worry about masking yet:
1314 - If they contain garbage off the least significant end, then we
1315 must be looking at the low end of the field, and the right
1316 shift will wipe them out.
1317 - If they contain garbage off the most significant end, then we
1318 must be looking at the most significant end of the word, and
1319 the sign/zero extension will wipe them out.
1320 - If we're in the interior of the word, then there is no garbage
1321 on either end, because the ref operators zero-extend. */
1322 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
1323 gen_left_shift (ax, end - (offset + op_size));
1325 gen_left_shift (ax, offset - start);
1328 /* Bring the copy of the address up to the top. */
1329 ax_simple (ax, aop_swap);
1336 /* Generate enough bitwise `or' operations to combine all the
1337 fragments we left on the stack. */
1338 while (fragment_count-- > 1)
1339 ax_simple (ax, aop_bit_or);
1341 /* Sign- or zero-extend the value as appropriate. */
1342 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1344 /* This is *not* an lvalue. Ugh. */
1345 value->kind = axs_rvalue;
1350 /* Generate code to reference the member named FIELD of a structure or
1351 union. The top of the stack, as described by VALUE, should have
1352 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1353 the operator being compiled, and OPERAND_NAME is the kind of thing
1354 it operates on; we use them in error messages. */
1356 gen_struct_ref (struct agent_expr *ax, struct axs_value *value, char *field,
1357 char *operator_name, char *operand_name)
1362 /* Follow pointers until we reach a non-pointer. These aren't the C
1363 semantics, but they're what the normal GDB evaluator does, so we
1364 should at least be consistent. */
1365 while (TYPE_CODE (value->type) == TYPE_CODE_PTR)
1367 gen_usual_unary (ax, value);
1368 gen_deref (ax, value);
1370 type = check_typedef (value->type);
1372 /* This must yield a structure or a union. */
1373 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1374 && TYPE_CODE (type) != TYPE_CODE_UNION)
1375 error (_("The left operand of `%s' is not a %s."),
1376 operator_name, operand_name);
1378 /* And it must be in memory; we don't deal with structure rvalues,
1379 or structures living in registers. */
1380 if (value->kind != axs_lvalue_memory)
1381 error (_("Structure does not live in memory."));
1383 i = find_field (type, field);
1385 /* Is this a bitfield? */
1386 if (TYPE_FIELD_PACKED (type, i))
1387 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1388 TYPE_FIELD_BITPOS (type, i),
1389 (TYPE_FIELD_BITPOS (type, i)
1390 + TYPE_FIELD_BITSIZE (type, i)));
1393 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1394 value->kind = axs_lvalue_memory;
1395 value->type = TYPE_FIELD_TYPE (type, i);
1400 /* Generate code for GDB's magical `repeat' operator.
1401 LVALUE @ INT creates an array INT elements long, and whose elements
1402 have the same type as LVALUE, located in memory so that LVALUE is
1403 its first element. For example, argv[0]@argc gives you the array
1404 of command-line arguments.
1406 Unfortunately, because we have to know the types before we actually
1407 have a value for the expression, we can't implement this perfectly
1408 without changing the type system, having values that occupy two
1409 stack slots, doing weird things with sizeof, etc. So we require
1410 the right operand to be a constant expression. */
1412 gen_repeat (union exp_element **pc, struct agent_expr *ax,
1413 struct axs_value *value)
1415 struct axs_value value1;
1416 /* We don't want to turn this into an rvalue, so no conversions
1418 gen_expr (pc, ax, &value1);
1419 if (value1.kind != axs_lvalue_memory)
1420 error (_("Left operand of `@' must be an object in memory."));
1422 /* Evaluate the length; it had better be a constant. */
1424 struct value *v = const_expr (pc);
1428 error (_("Right operand of `@' must be a constant, in agent expressions."));
1429 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1430 error (_("Right operand of `@' must be an integer."));
1431 length = value_as_long (v);
1433 error (_("Right operand of `@' must be positive."));
1435 /* The top of the stack is already the address of the object, so
1436 all we need to do is frob the type of the lvalue. */
1438 /* FIXME-type-allocation: need a way to free this type when we are
1441 = create_range_type (0, builtin_type_int, 0, length - 1);
1442 struct type *array = create_array_type (0, value1.type, range);
1444 value->kind = axs_lvalue_memory;
1445 value->type = array;
1451 /* Emit code for the `sizeof' operator.
1452 *PC should point at the start of the operand expression; we advance it
1453 to the first instruction after the operand. */
1455 gen_sizeof (union exp_element **pc, struct agent_expr *ax,
1456 struct axs_value *value)
1458 /* We don't care about the value of the operand expression; we only
1459 care about its type. However, in the current arrangement, the
1460 only way to find an expression's type is to generate code for it.
1461 So we generate code for the operand, and then throw it away,
1462 replacing it with code that simply pushes its size. */
1463 int start = ax->len;
1464 gen_expr (pc, ax, value);
1466 /* Throw away the code we just generated. */
1469 ax_const_l (ax, TYPE_LENGTH (value->type));
1470 value->kind = axs_rvalue;
1471 value->type = builtin_type_int;
1475 /* Generating bytecode from GDB expressions: general recursive thingy */
1478 /* A gen_expr function written by a Gen-X'er guy.
1479 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1481 gen_expr (union exp_element **pc, struct agent_expr *ax,
1482 struct axs_value *value)
1484 /* Used to hold the descriptions of operand expressions. */
1485 struct axs_value value1, value2;
1486 enum exp_opcode op = (*pc)[0].opcode;
1488 /* If we're looking at a constant expression, just push its value. */
1490 struct value *v = maybe_const_expr (pc);
1494 ax_const_l (ax, value_as_long (v));
1495 value->kind = axs_rvalue;
1496 value->type = check_typedef (value_type (v));
1501 /* Otherwise, go ahead and generate code for it. */
1504 /* Binary arithmetic operators. */
1510 case BINOP_SUBSCRIPT:
1511 case BINOP_BITWISE_AND:
1512 case BINOP_BITWISE_IOR:
1513 case BINOP_BITWISE_XOR:
1515 gen_expr (pc, ax, &value1);
1516 gen_usual_unary (ax, &value1);
1517 gen_expr (pc, ax, &value2);
1518 gen_usual_unary (ax, &value2);
1519 gen_usual_arithmetic (ax, &value1, &value2);
1523 gen_add (ax, value, &value1, &value2, "addition");
1526 gen_sub (ax, value, &value1, &value2);
1529 gen_binop (ax, value, &value1, &value2,
1530 aop_mul, aop_mul, 1, "multiplication");
1533 gen_binop (ax, value, &value1, &value2,
1534 aop_div_signed, aop_div_unsigned, 1, "division");
1537 gen_binop (ax, value, &value1, &value2,
1538 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1540 case BINOP_SUBSCRIPT:
1541 gen_add (ax, value, &value1, &value2, "array subscripting");
1542 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1543 error (_("Invalid combination of types in array subscripting."));
1544 gen_deref (ax, value);
1546 case BINOP_BITWISE_AND:
1547 gen_binop (ax, value, &value1, &value2,
1548 aop_bit_and, aop_bit_and, 0, "bitwise and");
1551 case BINOP_BITWISE_IOR:
1552 gen_binop (ax, value, &value1, &value2,
1553 aop_bit_or, aop_bit_or, 0, "bitwise or");
1556 case BINOP_BITWISE_XOR:
1557 gen_binop (ax, value, &value1, &value2,
1558 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1562 /* We should only list operators in the outer case statement
1563 that we actually handle in the inner case statement. */
1564 internal_error (__FILE__, __LINE__,
1565 _("gen_expr: op case sets don't match"));
1569 /* Note that we need to be a little subtle about generating code
1570 for comma. In C, we can do some optimizations here because
1571 we know the left operand is only being evaluated for effect.
1572 However, if the tracing kludge is in effect, then we always
1573 need to evaluate the left hand side fully, so that all the
1574 variables it mentions get traced. */
1577 gen_expr (pc, ax, &value1);
1578 /* Don't just dispose of the left operand. We might be tracing,
1579 in which case we want to emit code to trace it if it's an
1581 gen_traced_pop (ax, &value1);
1582 gen_expr (pc, ax, value);
1583 /* It's the consumer's responsibility to trace the right operand. */
1586 case OP_LONG: /* some integer constant */
1588 struct type *type = (*pc)[1].type;
1589 LONGEST k = (*pc)[2].longconst;
1591 gen_int_literal (ax, value, k, type);
1596 gen_var_ref (ax, value, (*pc)[2].symbol);
1602 int reg = (int) (*pc)[1].longconst;
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, produce a string of agent bytecode
1734 which computes its value. Return the agent expression, and set
1735 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1737 expr_to_agent (struct expression *expr, struct axs_value *value)
1739 struct cleanup *old_chain = 0;
1740 struct agent_expr *ax = new_agent_expr (0);
1741 union exp_element *pc;
1743 old_chain = make_cleanup_free_agent_expr (ax);
1747 gen_expr (&pc, ax, value);
1749 /* We have successfully built the agent expr, so cancel the cleanup
1750 request. If we add more cleanups that we always want done, this
1751 will have to get more complicated. */
1752 discard_cleanups (old_chain);
1757 #if 0 /* not used */
1758 /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1759 string of agent bytecode which will leave its address and size on
1760 the top of stack. Return the agent expression.
1762 Not sure this function is useful at all. */
1764 expr_to_address_and_size (struct expression *expr)
1766 struct axs_value value;
1767 struct agent_expr *ax = expr_to_agent (expr, &value);
1769 /* Complain if the result is not a memory lvalue. */
1770 if (value.kind != axs_lvalue_memory)
1772 free_agent_expr (ax);
1773 error (_("Expression does not denote an object in memory."));
1776 /* Push the object's size on the stack. */
1777 ax_const_l (ax, TYPE_LENGTH (value.type));
1783 /* Given a GDB expression EXPR, return bytecode to trace its value.
1784 The result will use the `trace' and `trace_quick' bytecodes to
1785 record the value of all memory touched by the expression. The
1786 caller can then use the ax_reqs function to discover which
1787 registers it relies upon. */
1789 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
1791 struct cleanup *old_chain = 0;
1792 struct agent_expr *ax = new_agent_expr (scope);
1793 union exp_element *pc;
1794 struct axs_value value;
1796 old_chain = make_cleanup_free_agent_expr (ax);
1800 gen_expr (&pc, ax, &value);
1802 /* Make sure we record the final object, and get rid of it. */
1803 gen_traced_pop (ax, &value);
1805 /* Oh, and terminate. */
1806 ax_simple (ax, aop_end);
1808 /* We have successfully built the agent expr, so cancel the cleanup
1809 request. If we add more cleanups that we always want done, this
1810 will have to get more complicated. */
1811 discard_cleanups (old_chain);
1816 agent_command (char *exp, int from_tty)
1818 struct cleanup *old_chain = 0;
1819 struct expression *expr;
1820 struct agent_expr *agent;
1821 struct frame_info *fi = get_current_frame (); /* need current scope */
1823 /* We don't deal with overlay debugging at the moment. We need to
1824 think more carefully about this. If you copy this code into
1825 another command, change the error message; the user shouldn't
1826 have to know anything about agent expressions. */
1827 if (overlay_debugging)
1828 error (_("GDB can't do agent expression translation with overlays."));
1831 error_no_arg (_("expression to translate"));
1833 expr = parse_expression (exp);
1834 old_chain = make_cleanup (free_current_contents, &expr);
1835 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
1836 make_cleanup_free_agent_expr (agent);
1837 ax_print (gdb_stdout, agent);
1839 /* It would be nice to call ax_reqs here to gather some general info
1840 about the expression, and then print out the result. */
1842 do_cleanups (old_chain);
1847 /* Initialization code. */
1849 void _initialize_ax_gdb (void);
1851 _initialize_ax_gdb (void)
1853 add_cmd ("agent", class_maintenance, agent_command,
1854 _("Translate an expression into remote agent bytecode."),