1 /* GDB-specific functions for operating on agent expressions
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include "expression.h"
33 #include "gdb_string.h"
35 /* To make sense of this file, you should read doc/agentexpr.texi.
36 Then look at the types and enums in ax-gdb.h. For the code itself,
37 look at gen_expr, towards the bottom; that's the main function that
38 looks at the GDB expressions and calls everything else to generate
41 I'm beginning to wonder whether it wouldn't be nicer to internally
42 generate trees, with types, and then spit out the bytecode in
43 linear form afterwards; we could generate fewer `swap', `ext', and
44 `zero_ext' bytecodes that way; it would make good constant folding
45 easier, too. But at the moment, I think we should be willing to
46 pay for the simplicity of this code with less-than-optimal bytecode
49 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
53 /* Prototypes for local functions. */
55 /* There's a standard order to the arguments of these functions:
56 union exp_element ** --- pointer into expression
57 struct agent_expr * --- agent expression buffer to generate code into
58 struct axs_value * --- describes value left on top of stack */
60 static struct value *const_var_ref (struct symbol *var);
61 static struct value *const_expr (union exp_element **pc);
62 static struct value *maybe_const_expr (union exp_element **pc);
64 static void gen_traced_pop (struct agent_expr *, struct axs_value *);
66 static void gen_sign_extend (struct agent_expr *, struct type *);
67 static void gen_extend (struct agent_expr *, struct type *);
68 static void gen_fetch (struct agent_expr *, struct type *);
69 static void gen_left_shift (struct agent_expr *, int);
72 static void gen_frame_args_address (struct agent_expr *);
73 static void gen_frame_locals_address (struct agent_expr *);
74 static void gen_offset (struct agent_expr *ax, int offset);
75 static void gen_sym_offset (struct agent_expr *, struct symbol *);
76 static void gen_var_ref (struct agent_expr *ax,
77 struct axs_value *value, struct symbol *var);
80 static void gen_int_literal (struct agent_expr *ax,
81 struct axs_value *value,
82 LONGEST k, struct type *type);
85 static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
86 static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
87 static int type_wider_than (struct type *type1, struct type *type2);
88 static struct type *max_type (struct type *type1, struct type *type2);
89 static void gen_conversion (struct agent_expr *ax,
90 struct type *from, struct type *to);
91 static int is_nontrivial_conversion (struct type *from, struct type *to);
92 static void gen_usual_arithmetic (struct agent_expr *ax,
93 struct axs_value *value1,
94 struct axs_value *value2);
95 static void gen_integral_promotions (struct agent_expr *ax,
96 struct axs_value *value);
97 static void gen_cast (struct agent_expr *ax,
98 struct axs_value *value, struct type *type);
99 static void gen_scale (struct agent_expr *ax,
100 enum agent_op op, struct type *type);
101 static void gen_add (struct agent_expr *ax,
102 struct axs_value *value,
103 struct axs_value *value1,
104 struct axs_value *value2, char *name);
105 static void gen_sub (struct agent_expr *ax,
106 struct axs_value *value,
107 struct axs_value *value1, struct axs_value *value2);
108 static void gen_binop (struct agent_expr *ax,
109 struct axs_value *value,
110 struct axs_value *value1,
111 struct axs_value *value2,
113 enum agent_op op_unsigned, int may_carry, char *name);
114 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value);
115 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
116 static void gen_deref (struct agent_expr *, struct axs_value *);
117 static void gen_address_of (struct agent_expr *, struct axs_value *);
118 static int find_field (struct type *type, char *name);
119 static void gen_bitfield_ref (struct agent_expr *ax,
120 struct axs_value *value,
121 struct type *type, int start, int end);
122 static void gen_struct_ref (struct agent_expr *ax,
123 struct axs_value *value,
125 char *operator_name, char *operand_name);
126 static void gen_repeat (union exp_element **pc,
127 struct agent_expr *ax, struct axs_value *value);
128 static void gen_sizeof (union exp_element **pc,
129 struct agent_expr *ax, struct axs_value *value);
130 static void gen_expr (union exp_element **pc,
131 struct agent_expr *ax, struct axs_value *value);
133 static void print_axs_value (struct ui_file *f, struct axs_value * value);
134 static void agent_command (char *exp, int from_tty);
137 /* Detecting constant expressions. */
139 /* If the variable reference at *PC is a constant, return its value.
140 Otherwise, return zero.
142 Hey, Wally! How can a variable reference be a constant?
144 Well, Beav, this function really handles the OP_VAR_VALUE operator,
145 not specifically variable references. GDB uses OP_VAR_VALUE to
146 refer to any kind of symbolic reference: function names, enum
147 elements, and goto labels are all handled through the OP_VAR_VALUE
148 operator, even though they're constants. It makes sense given the
151 Gee, Wally, don'cha wonder sometimes if data representations that
152 subvert commonly accepted definitions of terms in favor of heavily
153 context-specific interpretations are really just a tool of the
154 programming hegemony to preserve their power and exclude the
157 static struct value *
158 const_var_ref (struct symbol *var)
160 struct type *type = SYMBOL_TYPE (var);
162 switch (SYMBOL_CLASS (var))
165 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
168 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
176 /* If the expression starting at *PC has a constant value, return it.
177 Otherwise, return zero. If we return a value, then *PC will be
178 advanced to the end of it. If we return zero, *PC could be
180 static struct value *
181 const_expr (union exp_element **pc)
183 enum exp_opcode op = (*pc)->opcode;
190 struct type *type = (*pc)[1].type;
191 LONGEST k = (*pc)[2].longconst;
193 return value_from_longest (type, k);
198 struct value *v = const_var_ref ((*pc)[2].symbol);
203 /* We could add more operators in here. */
207 v1 = const_expr (pc);
209 return value_neg (v1);
219 /* Like const_expr, but guarantee also that *PC is undisturbed if the
220 expression is not constant. */
221 static struct value *
222 maybe_const_expr (union exp_element **pc)
224 union exp_element *tentative_pc = *pc;
225 struct value *v = const_expr (&tentative_pc);
227 /* If we got a value, then update the real PC. */
235 /* Generating bytecode from GDB expressions: general assumptions */
237 /* Here are a few general assumptions made throughout the code; if you
238 want to make a change that contradicts one of these, then you'd
239 better scan things pretty thoroughly.
241 - We assume that all values occupy one stack element. For example,
242 sometimes we'll swap to get at the left argument to a binary
243 operator. If we decide that void values should occupy no stack
244 elements, or that synthetic arrays (whose size is determined at
245 run time, created by the `@' operator) should occupy two stack
246 elements (address and length), then this will cause trouble.
248 - We assume the stack elements are infinitely wide, and that we
249 don't have to worry what happens if the user requests an
250 operation that is wider than the actual interpreter's stack.
251 That is, it's up to the interpreter to handle directly all the
252 integer widths the user has access to. (Woe betide the language
255 - We don't support side effects. Thus, we don't have to worry about
256 GCC's generalized lvalues, function calls, etc.
258 - We don't support floating point. Many places where we switch on
259 some type don't bother to include cases for floating point; there
260 may be even more subtle ways this assumption exists. For
261 example, the arguments to % must be integers.
263 - We assume all subexpressions have a static, unchanging type. If
264 we tried to support convenience variables, this would be a
267 - All values on the stack should always be fully zero- or
270 (I wasn't sure whether to choose this or its opposite --- that
271 only addresses are assumed extended --- but it turns out that
272 neither convention completely eliminates spurious extend
273 operations (if everything is always extended, then you have to
274 extend after add, because it could overflow; if nothing is
275 extended, then you end up producing extends whenever you change
276 sizes), and this is simpler.) */
279 /* Generating bytecode from GDB expressions: the `trace' kludge */
281 /* The compiler in this file is a general-purpose mechanism for
282 translating GDB expressions into bytecode. One ought to be able to
283 find a million and one uses for it.
285 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
286 of expediency. Let he who is without sin cast the first stone.
288 For the data tracing facility, we need to insert `trace' bytecodes
289 before each data fetch; this records all the memory that the
290 expression touches in the course of evaluation, so that memory will
291 be available when the user later tries to evaluate the expression
294 This should be done (I think) in a post-processing pass, that walks
295 an arbitrary agent expression and inserts `trace' operations at the
296 appropriate points. But it's much faster to just hack them
297 directly into the code. And since we're in a crunch, that's what
300 Setting the flag trace_kludge to non-zero enables the code that
301 emits the trace bytecodes at the appropriate points. */
302 static int trace_kludge;
304 /* Trace the lvalue on the stack, if it needs it. In either case, pop
305 the value. Useful on the left side of a comma, and at the end of
306 an expression being used for tracing. */
308 gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
314 /* We don't trace rvalues, just the lvalues necessary to
315 produce them. So just dispose of this value. */
316 ax_simple (ax, aop_pop);
319 case axs_lvalue_memory:
321 int length = TYPE_LENGTH (value->type);
323 /* There's no point in trying to use a trace_quick bytecode
324 here, since "trace_quick SIZE pop" is three bytes, whereas
325 "const8 SIZE trace" is also three bytes, does the same
326 thing, and the simplest code which generates that will also
327 work correctly for objects with large sizes. */
328 ax_const_l (ax, length);
329 ax_simple (ax, aop_trace);
333 case axs_lvalue_register:
334 /* We need to mention the register somewhere in the bytecode,
335 so ax_reqs will pick it up and add it to the mask of
337 ax_reg (ax, value->u.reg);
338 ax_simple (ax, aop_pop);
342 /* If we're not tracing, just pop the value. */
343 ax_simple (ax, aop_pop);
348 /* Generating bytecode from GDB expressions: helper functions */
350 /* Assume that the lower bits of the top of the stack is a value of
351 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
353 gen_sign_extend (struct agent_expr *ax, struct type *type)
355 /* Do we need to sign-extend this? */
356 if (!TYPE_UNSIGNED (type))
357 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
361 /* Assume the lower bits of the top of the stack hold a value of type
362 TYPE, and the upper bits are garbage. Sign-extend or truncate as
365 gen_extend (struct agent_expr *ax, struct type *type)
367 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
369 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
373 /* Assume that the top of the stack contains a value of type "pointer
374 to TYPE"; generate code to fetch its value. Note that TYPE is the
375 target type, not the pointer type. */
377 gen_fetch (struct agent_expr *ax, struct type *type)
381 /* Record the area of memory we're about to fetch. */
382 ax_trace_quick (ax, TYPE_LENGTH (type));
385 switch (TYPE_CODE (type))
391 /* It's a scalar value, so we know how to dereference it. How
392 many bytes long is it? */
393 switch (TYPE_LENGTH (type))
395 case 8 / TARGET_CHAR_BIT:
396 ax_simple (ax, aop_ref8);
398 case 16 / TARGET_CHAR_BIT:
399 ax_simple (ax, aop_ref16);
401 case 32 / TARGET_CHAR_BIT:
402 ax_simple (ax, aop_ref32);
404 case 64 / TARGET_CHAR_BIT:
405 ax_simple (ax, aop_ref64);
408 /* Either our caller shouldn't have asked us to dereference
409 that pointer (other code's fault), or we're not
410 implementing something we should be (this code's fault).
411 In any case, it's a bug the user shouldn't see. */
413 internal_error (__FILE__, __LINE__,
414 "gen_fetch: strange size");
417 gen_sign_extend (ax, type);
421 /* Either our caller shouldn't have asked us to dereference that
422 pointer (other code's fault), or we're not implementing
423 something we should be (this code's fault). In any case,
424 it's a bug the user shouldn't see. */
425 internal_error (__FILE__, __LINE__,
426 "gen_fetch: bad type code");
431 /* Generate code to left shift the top of the stack by DISTANCE bits, or
432 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
433 unsigned (logical) right shifts. */
435 gen_left_shift (struct agent_expr *ax, int distance)
439 ax_const_l (ax, distance);
440 ax_simple (ax, aop_lsh);
442 else if (distance < 0)
444 ax_const_l (ax, -distance);
445 ax_simple (ax, aop_rsh_unsigned);
451 /* Generating bytecode from GDB expressions: symbol references */
453 /* Generate code to push the base address of the argument portion of
454 the top stack frame. */
456 gen_frame_args_address (struct agent_expr *ax)
459 LONGEST frame_offset;
461 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
462 ax_reg (ax, frame_reg);
463 gen_offset (ax, frame_offset);
467 /* Generate code to push the base address of the locals portion of the
470 gen_frame_locals_address (struct agent_expr *ax)
473 LONGEST frame_offset;
475 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
476 ax_reg (ax, frame_reg);
477 gen_offset (ax, frame_offset);
481 /* Generate code to add OFFSET to the top of the stack. Try to
482 generate short and readable code. We use this for getting to
483 variables on the stack, and structure members. If we were
484 programming in ML, it would be clearer why these are the same
487 gen_offset (struct agent_expr *ax, int offset)
489 /* It would suffice to simply push the offset and add it, but this
490 makes it easier to read positive and negative offsets in the
494 ax_const_l (ax, offset);
495 ax_simple (ax, aop_add);
499 ax_const_l (ax, -offset);
500 ax_simple (ax, aop_sub);
505 /* In many cases, a symbol's value is the offset from some other
506 address (stack frame, base register, etc.) Generate code to add
507 VAR's value to the top of the stack. */
509 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
511 gen_offset (ax, SYMBOL_VALUE (var));
515 /* Generate code for a variable reference to AX. The variable is the
516 symbol VAR. Set VALUE to describe the result. */
519 gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
521 /* Dereference any typedefs. */
522 value->type = check_typedef (SYMBOL_TYPE (var));
524 /* I'm imitating the code in read_var_value. */
525 switch (SYMBOL_CLASS (var))
527 case LOC_CONST: /* A constant, like an enum value. */
528 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
529 value->kind = axs_rvalue;
532 case LOC_LABEL: /* A goto label, being used as a value. */
533 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
534 value->kind = axs_rvalue;
537 case LOC_CONST_BYTES:
538 internal_error (__FILE__, __LINE__,
539 "gen_var_ref: LOC_CONST_BYTES symbols are not supported");
541 /* Variable at a fixed location in memory. Easy. */
543 /* Push the address of the variable. */
544 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
545 value->kind = axs_lvalue_memory;
548 case LOC_ARG: /* var lives in argument area of frame */
549 gen_frame_args_address (ax);
550 gen_sym_offset (ax, var);
551 value->kind = axs_lvalue_memory;
554 case LOC_REF_ARG: /* As above, but the frame slot really
555 holds the address of the variable. */
556 gen_frame_args_address (ax);
557 gen_sym_offset (ax, var);
558 /* Don't assume any particular pointer size. */
559 gen_fetch (ax, lookup_pointer_type (builtin_type_void));
560 value->kind = axs_lvalue_memory;
563 case LOC_LOCAL: /* var lives in locals area of frame */
565 gen_frame_locals_address (ax);
566 gen_sym_offset (ax, var);
567 value->kind = axs_lvalue_memory;
570 case LOC_BASEREG: /* relative to some base register */
571 case LOC_BASEREG_ARG:
572 ax_reg (ax, SYMBOL_BASEREG (var));
573 gen_sym_offset (ax, var);
574 value->kind = axs_lvalue_memory;
578 error ("Cannot compute value of typedef `%s'.",
579 SYMBOL_SOURCE_NAME (var));
583 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
584 value->kind = axs_rvalue;
589 /* Don't generate any code at all; in the process of treating
590 this as an lvalue or rvalue, the caller will generate the
592 value->kind = axs_lvalue_register;
593 value->u.reg = SYMBOL_VALUE (var);
596 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
597 register, not on the stack. Simpler than LOC_REGISTER and
598 LOC_REGPARM, because it's just like any other case where the
599 thing has a real address. */
600 case LOC_REGPARM_ADDR:
601 ax_reg (ax, SYMBOL_VALUE (var));
602 value->kind = axs_lvalue_memory;
607 struct minimal_symbol *msym
608 = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
610 error ("Couldn't resolve symbol `%s'.", SYMBOL_SOURCE_NAME (var));
612 /* Push the address of the variable. */
613 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
614 value->kind = axs_lvalue_memory;
618 case LOC_OPTIMIZED_OUT:
619 error ("The variable `%s' has been optimized out.",
620 SYMBOL_SOURCE_NAME (var));
624 error ("Cannot find value of botched symbol `%s'.",
625 SYMBOL_SOURCE_NAME (var));
632 /* Generating bytecode from GDB expressions: literals */
635 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
639 value->kind = axs_rvalue;
645 /* Generating bytecode from GDB expressions: unary conversions, casts */
647 /* Take what's on the top of the stack (as described by VALUE), and
648 try to make an rvalue out of it. Signal an error if we can't do
651 require_rvalue (struct agent_expr *ax, struct axs_value *value)
656 /* It's already an rvalue. */
659 case axs_lvalue_memory:
660 /* The top of stack is the address of the object. Dereference. */
661 gen_fetch (ax, value->type);
664 case axs_lvalue_register:
665 /* There's nothing on the stack, but value->u.reg is the
666 register number containing the value.
668 When we add floating-point support, this is going to have to
669 change. What about SPARC register pairs, for example? */
670 ax_reg (ax, value->u.reg);
671 gen_extend (ax, value->type);
675 value->kind = axs_rvalue;
679 /* Assume the top of the stack is described by VALUE, and perform the
680 usual unary conversions. This is motivated by ANSI 6.2.2, but of
681 course GDB expressions are not ANSI; they're the mishmash union of
682 a bunch of languages. Rah.
684 NOTE! This function promises to produce an rvalue only when the
685 incoming value is of an appropriate type. In other words, the
686 consumer of the value this function produces may assume the value
687 is an rvalue only after checking its type.
689 The immediate issue is that if the user tries to use a structure or
690 union as an operand of, say, the `+' operator, we don't want to try
691 to convert that structure to an rvalue; require_rvalue will bomb on
692 structs and unions. Rather, we want to simply pass the struct
693 lvalue through unchanged, and let `+' raise an error. */
696 gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
698 /* We don't have to generate any code for the usual integral
699 conversions, since values are always represented as full-width on
700 the stack. Should we tweak the type? */
702 /* Some types require special handling. */
703 switch (TYPE_CODE (value->type))
705 /* Functions get converted to a pointer to the function. */
707 value->type = lookup_pointer_type (value->type);
708 value->kind = axs_rvalue; /* Should always be true, but just in case. */
711 /* Arrays get converted to a pointer to their first element, and
712 are no longer an lvalue. */
713 case TYPE_CODE_ARRAY:
715 struct type *elements = TYPE_TARGET_TYPE (value->type);
716 value->type = lookup_pointer_type (elements);
717 value->kind = axs_rvalue;
718 /* We don't need to generate any code; the address of the array
719 is also the address of its first element. */
723 /* Don't try to convert structures and unions to rvalues. Let the
724 consumer signal an error. */
725 case TYPE_CODE_STRUCT:
726 case TYPE_CODE_UNION:
729 /* If the value is an enum, call it an integer. */
731 value->type = builtin_type_int;
735 /* If the value is an lvalue, dereference it. */
736 require_rvalue (ax, value);
740 /* Return non-zero iff the type TYPE1 is considered "wider" than the
741 type TYPE2, according to the rules described in gen_usual_arithmetic. */
743 type_wider_than (struct type *type1, struct type *type2)
745 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
746 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
747 && TYPE_UNSIGNED (type1)
748 && !TYPE_UNSIGNED (type2)));
752 /* Return the "wider" of the two types TYPE1 and TYPE2. */
754 max_type (struct type *type1, struct type *type2)
756 return type_wider_than (type1, type2) ? type1 : type2;
760 /* Generate code to convert a scalar value of type FROM to type TO. */
762 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
764 /* Perhaps there is a more graceful way to state these rules. */
766 /* If we're converting to a narrower type, then we need to clear out
768 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
769 gen_extend (ax, from);
771 /* If the two values have equal width, but different signednesses,
772 then we need to extend. */
773 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
775 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
779 /* If we're converting to a wider type, and becoming unsigned, then
780 we need to zero out any possible sign bits. */
781 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
783 if (TYPE_UNSIGNED (to))
789 /* Return non-zero iff the type FROM will require any bytecodes to be
790 emitted to be converted to the type TO. */
792 is_nontrivial_conversion (struct type *from, struct type *to)
794 struct agent_expr *ax = new_agent_expr (0);
797 /* Actually generate the code, and see if anything came out. At the
798 moment, it would be trivial to replicate the code in
799 gen_conversion here, but in the future, when we're supporting
800 floating point and the like, it may not be. Doing things this
801 way allows this function to be independent of the logic in
803 gen_conversion (ax, from, to);
804 nontrivial = ax->len > 0;
805 free_agent_expr (ax);
810 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
811 6.2.1.5) for the two operands of an arithmetic operator. This
812 effectively finds a "least upper bound" type for the two arguments,
813 and promotes each argument to that type. *VALUE1 and *VALUE2
814 describe the values as they are passed in, and as they are left. */
816 gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
817 struct axs_value *value2)
819 /* Do the usual binary conversions. */
820 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
821 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
823 /* The ANSI integral promotions seem to work this way: Order the
824 integer types by size, and then by signedness: an n-bit
825 unsigned type is considered "wider" than an n-bit signed
826 type. Promote to the "wider" of the two types, and always
827 promote at least to int. */
828 struct type *target = max_type (builtin_type_int,
829 max_type (value1->type, value2->type));
831 /* Deal with value2, on the top of the stack. */
832 gen_conversion (ax, value2->type, target);
834 /* Deal with value1, not on the top of the stack. Don't
835 generate the `swap' instructions if we're not actually going
837 if (is_nontrivial_conversion (value1->type, target))
839 ax_simple (ax, aop_swap);
840 gen_conversion (ax, value1->type, target);
841 ax_simple (ax, aop_swap);
844 value1->type = value2->type = target;
849 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
850 the value on the top of the stack, as described by VALUE. Assume
851 the value has integral type. */
853 gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
855 if (!type_wider_than (value->type, builtin_type_int))
857 gen_conversion (ax, value->type, builtin_type_int);
858 value->type = builtin_type_int;
860 else if (!type_wider_than (value->type, builtin_type_unsigned_int))
862 gen_conversion (ax, value->type, builtin_type_unsigned_int);
863 value->type = builtin_type_unsigned_int;
868 /* Generate code for a cast to TYPE. */
870 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
872 /* GCC does allow casts to yield lvalues, so this should be fixed
873 before merging these changes into the trunk. */
874 require_rvalue (ax, value);
875 /* Dereference typedefs. */
876 type = check_typedef (type);
878 switch (TYPE_CODE (type))
881 /* It's implementation-defined, and I'll bet this is what GCC
885 case TYPE_CODE_ARRAY:
886 case TYPE_CODE_STRUCT:
887 case TYPE_CODE_UNION:
889 error ("Illegal type cast: intended type must be scalar.");
892 /* We don't have to worry about the size of the value, because
893 all our integral values are fully sign-extended, and when
894 casting pointers we can do anything we like. Is there any
895 way for us to actually know what GCC actually does with a
901 gen_conversion (ax, value->type, type);
905 /* We could pop the value, and rely on everyone else to check
906 the type and notice that this value doesn't occupy a stack
907 slot. But for now, leave the value on the stack, and
908 preserve the "value == stack element" assumption. */
912 error ("Casts to requested type are not yet implemented.");
920 /* Generating bytecode from GDB expressions: arithmetic */
922 /* Scale the integer on the top of the stack by the size of the target
923 of the pointer type TYPE. */
925 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
927 struct type *element = TYPE_TARGET_TYPE (type);
929 if (TYPE_LENGTH (element) != 1)
931 ax_const_l (ax, TYPE_LENGTH (element));
937 /* Generate code for an addition; non-trivial because we deal with
938 pointer arithmetic. We set VALUE to describe the result value; we
939 assume VALUE1 and VALUE2 describe the two operands, and that
940 they've undergone the usual binary conversions. Used by both
941 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
943 gen_add (struct agent_expr *ax, struct axs_value *value,
944 struct axs_value *value1, struct axs_value *value2, char *name)
947 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
948 && TYPE_CODE (value2->type) == TYPE_CODE_PTR)
950 /* Swap the values and proceed normally. */
951 ax_simple (ax, aop_swap);
952 gen_scale (ax, aop_mul, value2->type);
953 ax_simple (ax, aop_add);
954 gen_extend (ax, value2->type); /* Catch overflow. */
955 value->type = value2->type;
959 else if (TYPE_CODE (value1->type) == TYPE_CODE_PTR
960 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
962 gen_scale (ax, aop_mul, value1->type);
963 ax_simple (ax, aop_add);
964 gen_extend (ax, value1->type); /* Catch overflow. */
965 value->type = value1->type;
968 /* Must be number + number; the usual binary conversions will have
969 brought them both to the same width. */
970 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
971 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
973 ax_simple (ax, aop_add);
974 gen_extend (ax, value1->type); /* Catch overflow. */
975 value->type = value1->type;
979 error ("Illegal combination of types in %s.", name);
981 value->kind = axs_rvalue;
985 /* Generate code for an addition; non-trivial because we have to deal
986 with pointer arithmetic. We set VALUE to describe the result
987 value; we assume VALUE1 and VALUE2 describe the two operands, and
988 that they've undergone the usual binary conversions. */
990 gen_sub (struct agent_expr *ax, struct axs_value *value,
991 struct axs_value *value1, struct axs_value *value2)
993 if (TYPE_CODE (value1->type) == TYPE_CODE_PTR)
995 /* Is it PTR - INT? */
996 if (TYPE_CODE (value2->type) == TYPE_CODE_INT)
998 gen_scale (ax, aop_mul, value1->type);
999 ax_simple (ax, aop_sub);
1000 gen_extend (ax, value1->type); /* Catch overflow. */
1001 value->type = value1->type;
1004 /* Is it PTR - PTR? Strictly speaking, the types ought to
1005 match, but this is what the normal GDB expression evaluator
1007 else if (TYPE_CODE (value2->type) == TYPE_CODE_PTR
1008 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1009 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1011 ax_simple (ax, aop_sub);
1012 gen_scale (ax, aop_div_unsigned, value1->type);
1013 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
1017 First argument of `-' is a pointer, but second argument is neither\n\
1018 an integer nor a pointer of the same type.");
1021 /* Must be number + number. */
1022 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
1023 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
1025 ax_simple (ax, aop_sub);
1026 gen_extend (ax, value1->type); /* Catch overflow. */
1027 value->type = value1->type;
1031 error ("Illegal combination of types in subtraction.");
1033 value->kind = axs_rvalue;
1036 /* Generate code for a binary operator that doesn't do pointer magic.
1037 We set VALUE to describe the result value; we assume VALUE1 and
1038 VALUE2 describe the two operands, and that they've undergone the
1039 usual binary conversions. MAY_CARRY should be non-zero iff the
1040 result needs to be extended. NAME is the English name of the
1041 operator, used in error messages */
1043 gen_binop (struct agent_expr *ax, struct axs_value *value,
1044 struct axs_value *value1, struct axs_value *value2, enum agent_op op,
1045 enum agent_op op_unsigned, int may_carry, char *name)
1047 /* We only handle INT op INT. */
1048 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1049 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1050 error ("Illegal combination of types in %s.", name);
1053 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1055 gen_extend (ax, value1->type); /* catch overflow */
1056 value->type = value1->type;
1057 value->kind = axs_rvalue;
1062 gen_logical_not (struct agent_expr *ax, struct axs_value *value)
1064 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1065 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1066 error ("Illegal type of operand to `!'.");
1068 gen_usual_unary (ax, value);
1069 ax_simple (ax, aop_log_not);
1070 value->type = builtin_type_int;
1075 gen_complement (struct agent_expr *ax, struct axs_value *value)
1077 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1078 error ("Illegal type of operand to `~'.");
1080 gen_usual_unary (ax, value);
1081 gen_integral_promotions (ax, value);
1082 ax_simple (ax, aop_bit_not);
1083 gen_extend (ax, value->type);
1088 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1090 /* Dereference the value on the top of the stack. */
1092 gen_deref (struct agent_expr *ax, struct axs_value *value)
1094 /* The caller should check the type, because several operators use
1095 this, and we don't know what error message to generate. */
1096 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1097 internal_error (__FILE__, __LINE__,
1098 "gen_deref: expected a pointer");
1100 /* We've got an rvalue now, which is a pointer. We want to yield an
1101 lvalue, whose address is exactly that pointer. So we don't
1102 actually emit any code; we just change the type from "Pointer to
1103 T" to "T", and mark the value as an lvalue in memory. Leave it
1104 to the consumer to actually dereference it. */
1105 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1106 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1107 ? axs_rvalue : axs_lvalue_memory);
1111 /* Produce the address of the lvalue on the top of the stack. */
1113 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1115 /* Special case for taking the address of a function. The ANSI
1116 standard describes this as a special case, too, so this
1117 arrangement is not without motivation. */
1118 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1119 /* The value's already an rvalue on the stack, so we just need to
1121 value->type = lookup_pointer_type (value->type);
1123 switch (value->kind)
1126 error ("Operand of `&' is an rvalue, which has no address.");
1128 case axs_lvalue_register:
1129 error ("Operand of `&' is in a register, and has no address.");
1131 case axs_lvalue_memory:
1132 value->kind = axs_rvalue;
1133 value->type = lookup_pointer_type (value->type);
1139 /* A lot of this stuff will have to change to support C++. But we're
1140 not going to deal with that at the moment. */
1142 /* Find the field in the structure type TYPE named NAME, and return
1143 its index in TYPE's field array. */
1145 find_field (struct type *type, char *name)
1149 CHECK_TYPEDEF (type);
1151 /* Make sure this isn't C++. */
1152 if (TYPE_N_BASECLASSES (type) != 0)
1153 internal_error (__FILE__, __LINE__,
1154 "find_field: derived classes supported");
1156 for (i = 0; i < TYPE_NFIELDS (type); i++)
1158 char *this_name = TYPE_FIELD_NAME (type, i);
1160 if (this_name && STREQ (name, this_name))
1163 if (this_name[0] == '\0')
1164 internal_error (__FILE__, __LINE__,
1165 "find_field: anonymous unions not supported");
1168 error ("Couldn't find member named `%s' in struct/union `%s'",
1169 name, TYPE_TAG_NAME (type));
1175 /* Generate code to push the value of a bitfield of a structure whose
1176 address is on the top of the stack. START and END give the
1177 starting and one-past-ending *bit* numbers of the field within the
1180 gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1181 struct type *type, int start, int end)
1183 /* Note that ops[i] fetches 8 << i bits. */
1184 static enum agent_op ops[]
1186 {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1187 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1189 /* We don't want to touch any byte that the bitfield doesn't
1190 actually occupy; we shouldn't make any accesses we're not
1191 explicitly permitted to. We rely here on the fact that the
1192 bytecode `ref' operators work on unaligned addresses.
1194 It takes some fancy footwork to get the stack to work the way
1195 we'd like. Say we're retrieving a bitfield that requires three
1196 fetches. Initially, the stack just contains the address:
1198 For the first fetch, we duplicate the address
1200 then add the byte offset, do the fetch, and shift and mask as
1201 needed, yielding a fragment of the value, properly aligned for
1202 the final bitwise or:
1204 then we swap, and repeat the process:
1205 frag1 addr --- address on top
1206 frag1 addr addr --- duplicate it
1207 frag1 addr frag2 --- get second fragment
1208 frag1 frag2 addr --- swap again
1209 frag1 frag2 frag3 --- get third fragment
1210 Notice that, since the third fragment is the last one, we don't
1211 bother duplicating the address this time. Now we have all the
1212 fragments on the stack, and we can simply `or' them together,
1213 yielding the final value of the bitfield. */
1215 /* The first and one-after-last bits in the field, but rounded down
1216 and up to byte boundaries. */
1217 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1218 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1222 /* current bit offset within the structure */
1225 /* The index in ops of the opcode we're considering. */
1228 /* The number of fragments we generated in the process. Probably
1229 equal to the number of `one' bits in bytesize, but who cares? */
1232 /* Dereference any typedefs. */
1233 type = check_typedef (type);
1235 /* Can we fetch the number of bits requested at all? */
1236 if ((end - start) > ((1 << num_ops) * 8))
1237 internal_error (__FILE__, __LINE__,
1238 "gen_bitfield_ref: bitfield too wide");
1240 /* Note that we know here that we only need to try each opcode once.
1241 That may not be true on machines with weird byte sizes. */
1242 offset = bound_start;
1244 for (op = num_ops - 1; op >= 0; op--)
1246 /* number of bits that ops[op] would fetch */
1247 int op_size = 8 << op;
1249 /* The stack at this point, from bottom to top, contains zero or
1250 more fragments, then the address. */
1252 /* Does this fetch fit within the bitfield? */
1253 if (offset + op_size <= bound_end)
1255 /* Is this the last fragment? */
1256 int last_frag = (offset + op_size == bound_end);
1259 ax_simple (ax, aop_dup); /* keep a copy of the address */
1261 /* Add the offset. */
1262 gen_offset (ax, offset / TARGET_CHAR_BIT);
1266 /* Record the area of memory we're about to fetch. */
1267 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1270 /* Perform the fetch. */
1271 ax_simple (ax, ops[op]);
1273 /* Shift the bits we have to their proper position.
1274 gen_left_shift will generate right shifts when the operand
1277 A big-endian field diagram to ponder:
1278 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1279 +------++------++------++------++------++------++------++------+
1280 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1282 bit number 16 32 48 53
1283 These are bit numbers as supplied by GDB. Note that the
1284 bit numbers run from right to left once you've fetched the
1287 A little-endian field diagram to ponder:
1288 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1289 +------++------++------++------++------++------++------++------+
1290 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1292 bit number 48 32 16 4 0
1294 In both cases, the most significant end is on the left
1295 (i.e. normal numeric writing order), which means that you
1296 don't go crazy thinking about `left' and `right' shifts.
1298 We don't have to worry about masking yet:
1299 - If they contain garbage off the least significant end, then we
1300 must be looking at the low end of the field, and the right
1301 shift will wipe them out.
1302 - If they contain garbage off the most significant end, then we
1303 must be looking at the most significant end of the word, and
1304 the sign/zero extension will wipe them out.
1305 - If we're in the interior of the word, then there is no garbage
1306 on either end, because the ref operators zero-extend. */
1307 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1308 gen_left_shift (ax, end - (offset + op_size));
1310 gen_left_shift (ax, offset - start);
1313 /* Bring the copy of the address up to the top. */
1314 ax_simple (ax, aop_swap);
1321 /* Generate enough bitwise `or' operations to combine all the
1322 fragments we left on the stack. */
1323 while (fragment_count-- > 1)
1324 ax_simple (ax, aop_bit_or);
1326 /* Sign- or zero-extend the value as appropriate. */
1327 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1329 /* This is *not* an lvalue. Ugh. */
1330 value->kind = axs_rvalue;
1335 /* Generate code to reference the member named FIELD of a structure or
1336 union. The top of the stack, as described by VALUE, should have
1337 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1338 the operator being compiled, and OPERAND_NAME is the kind of thing
1339 it operates on; we use them in error messages. */
1341 gen_struct_ref (struct agent_expr *ax, struct axs_value *value, char *field,
1342 char *operator_name, char *operand_name)
1347 /* Follow pointers until we reach a non-pointer. These aren't the C
1348 semantics, but they're what the normal GDB evaluator does, so we
1349 should at least be consistent. */
1350 while (TYPE_CODE (value->type) == TYPE_CODE_PTR)
1352 gen_usual_unary (ax, value);
1353 gen_deref (ax, value);
1355 type = check_typedef (value->type);
1357 /* This must yield a structure or a union. */
1358 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1359 && TYPE_CODE (type) != TYPE_CODE_UNION)
1360 error ("The left operand of `%s' is not a %s.",
1361 operator_name, operand_name);
1363 /* And it must be in memory; we don't deal with structure rvalues,
1364 or structures living in registers. */
1365 if (value->kind != axs_lvalue_memory)
1366 error ("Structure does not live in memory.");
1368 i = find_field (type, field);
1370 /* Is this a bitfield? */
1371 if (TYPE_FIELD_PACKED (type, i))
1372 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1373 TYPE_FIELD_BITPOS (type, i),
1374 (TYPE_FIELD_BITPOS (type, i)
1375 + TYPE_FIELD_BITSIZE (type, i)));
1378 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1379 value->kind = axs_lvalue_memory;
1380 value->type = TYPE_FIELD_TYPE (type, i);
1385 /* Generate code for GDB's magical `repeat' operator.
1386 LVALUE @ INT creates an array INT elements long, and whose elements
1387 have the same type as LVALUE, located in memory so that LVALUE is
1388 its first element. For example, argv[0]@argc gives you the array
1389 of command-line arguments.
1391 Unfortunately, because we have to know the types before we actually
1392 have a value for the expression, we can't implement this perfectly
1393 without changing the type system, having values that occupy two
1394 stack slots, doing weird things with sizeof, etc. So we require
1395 the right operand to be a constant expression. */
1397 gen_repeat (union exp_element **pc, struct agent_expr *ax,
1398 struct axs_value *value)
1400 struct axs_value value1;
1401 /* We don't want to turn this into an rvalue, so no conversions
1403 gen_expr (pc, ax, &value1);
1404 if (value1.kind != axs_lvalue_memory)
1405 error ("Left operand of `@' must be an object in memory.");
1407 /* Evaluate the length; it had better be a constant. */
1409 struct value *v = const_expr (pc);
1413 error ("Right operand of `@' must be a constant, in agent expressions.");
1414 if (TYPE_CODE (v->type) != TYPE_CODE_INT)
1415 error ("Right operand of `@' must be an integer.");
1416 length = value_as_long (v);
1418 error ("Right operand of `@' must be positive.");
1420 /* The top of the stack is already the address of the object, so
1421 all we need to do is frob the type of the lvalue. */
1423 /* FIXME-type-allocation: need a way to free this type when we are
1426 = create_range_type (0, builtin_type_int, 0, length - 1);
1427 struct type *array = create_array_type (0, value1.type, range);
1429 value->kind = axs_lvalue_memory;
1430 value->type = array;
1436 /* Emit code for the `sizeof' operator.
1437 *PC should point at the start of the operand expression; we advance it
1438 to the first instruction after the operand. */
1440 gen_sizeof (union exp_element **pc, struct agent_expr *ax,
1441 struct axs_value *value)
1443 /* We don't care about the value of the operand expression; we only
1444 care about its type. However, in the current arrangement, the
1445 only way to find an expression's type is to generate code for it.
1446 So we generate code for the operand, and then throw it away,
1447 replacing it with code that simply pushes its size. */
1448 int start = ax->len;
1449 gen_expr (pc, ax, value);
1451 /* Throw away the code we just generated. */
1454 ax_const_l (ax, TYPE_LENGTH (value->type));
1455 value->kind = axs_rvalue;
1456 value->type = builtin_type_int;
1460 /* Generating bytecode from GDB expressions: general recursive thingy */
1462 /* A gen_expr function written by a Gen-X'er guy.
1463 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1465 gen_expr (union exp_element **pc, struct agent_expr *ax,
1466 struct axs_value *value)
1468 /* Used to hold the descriptions of operand expressions. */
1469 struct axs_value value1, value2;
1470 enum exp_opcode op = (*pc)[0].opcode;
1472 /* If we're looking at a constant expression, just push its value. */
1474 struct value *v = maybe_const_expr (pc);
1478 ax_const_l (ax, value_as_long (v));
1479 value->kind = axs_rvalue;
1480 value->type = check_typedef (VALUE_TYPE (v));
1485 /* Otherwise, go ahead and generate code for it. */
1488 /* Binary arithmetic operators. */
1494 case BINOP_SUBSCRIPT:
1495 case BINOP_BITWISE_AND:
1496 case BINOP_BITWISE_IOR:
1497 case BINOP_BITWISE_XOR:
1499 gen_expr (pc, ax, &value1);
1500 gen_usual_unary (ax, &value1);
1501 gen_expr (pc, ax, &value2);
1502 gen_usual_unary (ax, &value2);
1503 gen_usual_arithmetic (ax, &value1, &value2);
1507 gen_add (ax, value, &value1, &value2, "addition");
1510 gen_sub (ax, value, &value1, &value2);
1513 gen_binop (ax, value, &value1, &value2,
1514 aop_mul, aop_mul, 1, "multiplication");
1517 gen_binop (ax, value, &value1, &value2,
1518 aop_div_signed, aop_div_unsigned, 1, "division");
1521 gen_binop (ax, value, &value1, &value2,
1522 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1524 case BINOP_SUBSCRIPT:
1525 gen_add (ax, value, &value1, &value2, "array subscripting");
1526 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1527 error ("Illegal combination of types in array subscripting.");
1528 gen_deref (ax, value);
1530 case BINOP_BITWISE_AND:
1531 gen_binop (ax, value, &value1, &value2,
1532 aop_bit_and, aop_bit_and, 0, "bitwise and");
1535 case BINOP_BITWISE_IOR:
1536 gen_binop (ax, value, &value1, &value2,
1537 aop_bit_or, aop_bit_or, 0, "bitwise or");
1540 case BINOP_BITWISE_XOR:
1541 gen_binop (ax, value, &value1, &value2,
1542 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1546 /* We should only list operators in the outer case statement
1547 that we actually handle in the inner case statement. */
1548 internal_error (__FILE__, __LINE__,
1549 "gen_expr: op case sets don't match");
1553 /* Note that we need to be a little subtle about generating code
1554 for comma. In C, we can do some optimizations here because
1555 we know the left operand is only being evaluated for effect.
1556 However, if the tracing kludge is in effect, then we always
1557 need to evaluate the left hand side fully, so that all the
1558 variables it mentions get traced. */
1561 gen_expr (pc, ax, &value1);
1562 /* Don't just dispose of the left operand. We might be tracing,
1563 in which case we want to emit code to trace it if it's an
1565 gen_traced_pop (ax, &value1);
1566 gen_expr (pc, ax, value);
1567 /* It's the consumer's responsibility to trace the right operand. */
1570 case OP_LONG: /* some integer constant */
1572 struct type *type = (*pc)[1].type;
1573 LONGEST k = (*pc)[2].longconst;
1575 gen_int_literal (ax, value, k, type);
1580 gen_var_ref (ax, value, (*pc)[2].symbol);
1586 int reg = (int) (*pc)[1].longconst;
1588 value->kind = axs_lvalue_register;
1590 value->type = REGISTER_VIRTUAL_TYPE (reg);
1594 case OP_INTERNALVAR:
1595 error ("GDB agent expressions cannot use convenience variables.");
1597 /* Weirdo operator: see comments for gen_repeat for details. */
1599 /* Note that gen_repeat handles its own argument evaluation. */
1601 gen_repeat (pc, ax, value);
1606 struct type *type = (*pc)[1].type;
1608 gen_expr (pc, ax, value);
1609 gen_cast (ax, value, type);
1615 struct type *type = check_typedef ((*pc)[1].type);
1617 gen_expr (pc, ax, value);
1618 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1619 it's just a hack for dealing with minsyms; you take some
1620 integer constant, pretend it's the address of an lvalue of
1621 the given type, and dereference it. */
1622 if (value->kind != axs_rvalue)
1623 /* This would be weird. */
1624 internal_error (__FILE__, __LINE__,
1625 "gen_expr: OP_MEMVAL operand isn't an rvalue???");
1627 value->kind = axs_lvalue_memory;
1633 /* -FOO is equivalent to 0 - FOO. */
1634 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
1635 gen_usual_unary (ax, &value1); /* shouldn't do much */
1636 gen_expr (pc, ax, &value2);
1637 gen_usual_unary (ax, &value2);
1638 gen_usual_arithmetic (ax, &value1, &value2);
1639 gen_sub (ax, value, &value1, &value2);
1642 case UNOP_LOGICAL_NOT:
1644 gen_expr (pc, ax, value);
1645 gen_logical_not (ax, value);
1648 case UNOP_COMPLEMENT:
1650 gen_expr (pc, ax, value);
1651 gen_complement (ax, value);
1656 gen_expr (pc, ax, value);
1657 gen_usual_unary (ax, value);
1658 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1659 error ("Argument of unary `*' is not a pointer.");
1660 gen_deref (ax, value);
1665 gen_expr (pc, ax, value);
1666 gen_address_of (ax, value);
1671 /* Notice that gen_sizeof handles its own operand, unlike most
1672 of the other unary operator functions. This is because we
1673 have to throw away the code we generate. */
1674 gen_sizeof (pc, ax, value);
1677 case STRUCTOP_STRUCT:
1680 int length = (*pc)[1].longconst;
1681 char *name = &(*pc)[2].string;
1683 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1684 gen_expr (pc, ax, value);
1685 if (op == STRUCTOP_STRUCT)
1686 gen_struct_ref (ax, value, name, ".", "structure or union");
1687 else if (op == STRUCTOP_PTR)
1688 gen_struct_ref (ax, value, name, "->",
1689 "pointer to a structure or union");
1691 /* If this `if' chain doesn't handle it, then the case list
1692 shouldn't mention it, and we shouldn't be here. */
1693 internal_error (__FILE__, __LINE__,
1694 "gen_expr: unhandled struct case");
1699 error ("Attempt to use a type name as an expression.");
1702 error ("Unsupported operator in expression.");
1708 /* Generating bytecode from GDB expressions: driver */
1710 /* Given a GDB expression EXPR, produce a string of agent bytecode
1711 which computes its value. Return the agent expression, and set
1712 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1714 expr_to_agent (struct expression *expr, struct axs_value *value)
1716 struct cleanup *old_chain = 0;
1717 struct agent_expr *ax = new_agent_expr (0);
1718 union exp_element *pc;
1720 old_chain = make_cleanup_free_agent_expr (ax);
1724 gen_expr (&pc, ax, value);
1726 /* We have successfully built the agent expr, so cancel the cleanup
1727 request. If we add more cleanups that we always want done, this
1728 will have to get more complicated. */
1729 discard_cleanups (old_chain);
1734 #if 0 /* not used */
1735 /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1736 string of agent bytecode which will leave its address and size on
1737 the top of stack. Return the agent expression.
1739 Not sure this function is useful at all. */
1741 expr_to_address_and_size (struct expression *expr)
1743 struct axs_value value;
1744 struct agent_expr *ax = expr_to_agent (expr, &value);
1746 /* Complain if the result is not a memory lvalue. */
1747 if (value.kind != axs_lvalue_memory)
1749 free_agent_expr (ax);
1750 error ("Expression does not denote an object in memory.");
1753 /* Push the object's size on the stack. */
1754 ax_const_l (ax, TYPE_LENGTH (value.type));
1760 /* Given a GDB expression EXPR, return bytecode to trace its value.
1761 The result will use the `trace' and `trace_quick' bytecodes to
1762 record the value of all memory touched by the expression. The
1763 caller can then use the ax_reqs function to discover which
1764 registers it relies upon. */
1766 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
1768 struct cleanup *old_chain = 0;
1769 struct agent_expr *ax = new_agent_expr (scope);
1770 union exp_element *pc;
1771 struct axs_value value;
1773 old_chain = make_cleanup_free_agent_expr (ax);
1777 gen_expr (&pc, ax, &value);
1779 /* Make sure we record the final object, and get rid of it. */
1780 gen_traced_pop (ax, &value);
1782 /* Oh, and terminate. */
1783 ax_simple (ax, aop_end);
1785 /* We have successfully built the agent expr, so cancel the cleanup
1786 request. If we add more cleanups that we always want done, this
1787 will have to get more complicated. */
1788 discard_cleanups (old_chain);
1794 /* The "agent" command, for testing: compile and disassemble an expression. */
1797 print_axs_value (struct ui_file *f, struct axs_value *value)
1799 switch (value->kind)
1802 fputs_filtered ("rvalue", f);
1805 case axs_lvalue_memory:
1806 fputs_filtered ("memory lvalue", f);
1809 case axs_lvalue_register:
1810 fprintf_filtered (f, "register %d lvalue", value->u.reg);
1814 fputs_filtered (" : ", f);
1815 type_print (value->type, "", f, -1);
1820 agent_command (char *exp, int from_tty)
1822 struct cleanup *old_chain = 0;
1823 struct expression *expr;
1824 struct agent_expr *agent;
1825 struct frame_info *fi = get_current_frame (); /* need current scope */
1827 /* We don't deal with overlay debugging at the moment. We need to
1828 think more carefully about this. If you copy this code into
1829 another command, change the error message; the user shouldn't
1830 have to know anything about agent expressions. */
1831 if (overlay_debugging)
1832 error ("GDB can't do agent expression translation with overlays.");
1835 error_no_arg ("expression to translate");
1837 expr = parse_expression (exp);
1838 old_chain = make_cleanup (free_current_contents, &expr);
1839 agent = gen_trace_for_expr (fi->pc, expr);
1840 make_cleanup_free_agent_expr (agent);
1841 ax_print (gdb_stdout, agent);
1843 /* It would be nice to call ax_reqs here to gather some general info
1844 about the expression, and then print out the result. */
1846 do_cleanups (old_chain);
1851 /* Initialization code. */
1853 void _initialize_ax_gdb (void);
1855 _initialize_ax_gdb (void)
1857 add_cmd ("agent", class_maintenance, agent_command,
1858 "Translate an expression into remote agent bytecode.",