xref: /dflybsd-src/contrib/gdb-7/gdb/ax-gdb.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* GDB-specific functions for operating on agent expressions.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1998-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "symtab.h"
225796c8dcSSimon Schubert #include "symfile.h"
235796c8dcSSimon Schubert #include "gdbtypes.h"
24cf7f2e2dSJohn Marino #include "language.h"
255796c8dcSSimon Schubert #include "value.h"
265796c8dcSSimon Schubert #include "expression.h"
275796c8dcSSimon Schubert #include "command.h"
285796c8dcSSimon Schubert #include "gdbcmd.h"
295796c8dcSSimon Schubert #include "frame.h"
305796c8dcSSimon Schubert #include "target.h"
315796c8dcSSimon Schubert #include "ax.h"
325796c8dcSSimon Schubert #include "ax-gdb.h"
335796c8dcSSimon Schubert #include "gdb_string.h"
345796c8dcSSimon Schubert #include "block.h"
355796c8dcSSimon Schubert #include "regcache.h"
365796c8dcSSimon Schubert #include "user-regs.h"
375796c8dcSSimon Schubert #include "language.h"
38cf7f2e2dSJohn Marino #include "dictionary.h"
39cf7f2e2dSJohn Marino #include "breakpoint.h"
40cf7f2e2dSJohn Marino #include "tracepoint.h"
41cf7f2e2dSJohn Marino #include "cp-support.h"
42a45ae5f8SJohn Marino #include "arch-utils.h"
43*ef5ccd6cSJohn Marino #include "cli/cli-utils.h"
44*ef5ccd6cSJohn Marino #include "linespec.h"
45a45ae5f8SJohn Marino 
46a45ae5f8SJohn Marino #include "valprint.h"
47a45ae5f8SJohn Marino #include "c-lang.h"
485796c8dcSSimon Schubert 
49*ef5ccd6cSJohn Marino #include "format.h"
50*ef5ccd6cSJohn Marino 
515796c8dcSSimon Schubert /* To make sense of this file, you should read doc/agentexpr.texi.
525796c8dcSSimon Schubert    Then look at the types and enums in ax-gdb.h.  For the code itself,
535796c8dcSSimon Schubert    look at gen_expr, towards the bottom; that's the main function that
545796c8dcSSimon Schubert    looks at the GDB expressions and calls everything else to generate
555796c8dcSSimon Schubert    code.
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert    I'm beginning to wonder whether it wouldn't be nicer to internally
585796c8dcSSimon Schubert    generate trees, with types, and then spit out the bytecode in
595796c8dcSSimon Schubert    linear form afterwards; we could generate fewer `swap', `ext', and
605796c8dcSSimon Schubert    `zero_ext' bytecodes that way; it would make good constant folding
615796c8dcSSimon Schubert    easier, too.  But at the moment, I think we should be willing to
625796c8dcSSimon Schubert    pay for the simplicity of this code with less-than-optimal bytecode
635796c8dcSSimon Schubert    strings.
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert    Remember, "GBD" stands for "Great Britain, Dammit!"  So be careful.  */
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert /* Prototypes for local functions.  */
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert /* There's a standard order to the arguments of these functions:
725796c8dcSSimon Schubert    union exp_element ** --- pointer into expression
735796c8dcSSimon Schubert    struct agent_expr * --- agent expression buffer to generate code into
745796c8dcSSimon Schubert    struct axs_value * --- describes value left on top of stack  */
755796c8dcSSimon Schubert 
765796c8dcSSimon Schubert static struct value *const_var_ref (struct symbol *var);
775796c8dcSSimon Schubert static struct value *const_expr (union exp_element **pc);
785796c8dcSSimon Schubert static struct value *maybe_const_expr (union exp_element **pc);
795796c8dcSSimon Schubert 
80c50c785cSJohn Marino static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
81c50c785cSJohn Marino 			    struct axs_value *);
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert static void gen_sign_extend (struct agent_expr *, struct type *);
845796c8dcSSimon Schubert static void gen_extend (struct agent_expr *, struct type *);
855796c8dcSSimon Schubert static void gen_fetch (struct agent_expr *, struct type *);
865796c8dcSSimon Schubert static void gen_left_shift (struct agent_expr *, int);
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
905796c8dcSSimon Schubert static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
915796c8dcSSimon Schubert static void gen_offset (struct agent_expr *ax, int offset);
925796c8dcSSimon Schubert static void gen_sym_offset (struct agent_expr *, struct symbol *);
935796c8dcSSimon Schubert static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
945796c8dcSSimon Schubert 			 struct axs_value *value, struct symbol *var);
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert static void gen_int_literal (struct agent_expr *ax,
985796c8dcSSimon Schubert 			     struct axs_value *value,
995796c8dcSSimon Schubert 			     LONGEST k, struct type *type);
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
1025796c8dcSSimon Schubert 			     struct axs_value *value);
1035796c8dcSSimon Schubert static int type_wider_than (struct type *type1, struct type *type2);
1045796c8dcSSimon Schubert static struct type *max_type (struct type *type1, struct type *type2);
1055796c8dcSSimon Schubert static void gen_conversion (struct agent_expr *ax,
1065796c8dcSSimon Schubert 			    struct type *from, struct type *to);
1075796c8dcSSimon Schubert static int is_nontrivial_conversion (struct type *from, struct type *to);
1085796c8dcSSimon Schubert static void gen_usual_arithmetic (struct expression *exp,
1095796c8dcSSimon Schubert 				  struct agent_expr *ax,
1105796c8dcSSimon Schubert 				  struct axs_value *value1,
1115796c8dcSSimon Schubert 				  struct axs_value *value2);
1125796c8dcSSimon Schubert static void gen_integral_promotions (struct expression *exp,
1135796c8dcSSimon Schubert 				     struct agent_expr *ax,
1145796c8dcSSimon Schubert 				     struct axs_value *value);
1155796c8dcSSimon Schubert static void gen_cast (struct agent_expr *ax,
1165796c8dcSSimon Schubert 		      struct axs_value *value, struct type *type);
1175796c8dcSSimon Schubert static void gen_scale (struct agent_expr *ax,
1185796c8dcSSimon Schubert 		       enum agent_op op, struct type *type);
1195796c8dcSSimon Schubert static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1205796c8dcSSimon Schubert 			struct axs_value *value1, struct axs_value *value2);
1215796c8dcSSimon Schubert static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1225796c8dcSSimon Schubert 			struct axs_value *value1, struct axs_value *value2);
1235796c8dcSSimon Schubert static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1245796c8dcSSimon Schubert 			 struct axs_value *value1, struct axs_value *value2,
1255796c8dcSSimon Schubert 			 struct type *result_type);
1265796c8dcSSimon Schubert static void gen_binop (struct agent_expr *ax,
1275796c8dcSSimon Schubert 		       struct axs_value *value,
1285796c8dcSSimon Schubert 		       struct axs_value *value1,
1295796c8dcSSimon Schubert 		       struct axs_value *value2,
1305796c8dcSSimon Schubert 		       enum agent_op op,
1315796c8dcSSimon Schubert 		       enum agent_op op_unsigned, int may_carry, char *name);
1325796c8dcSSimon Schubert static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1335796c8dcSSimon Schubert 			     struct type *result_type);
1345796c8dcSSimon Schubert static void gen_complement (struct agent_expr *ax, struct axs_value *value);
1355796c8dcSSimon Schubert static void gen_deref (struct agent_expr *, struct axs_value *);
1365796c8dcSSimon Schubert static void gen_address_of (struct agent_expr *, struct axs_value *);
1375796c8dcSSimon Schubert static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1385796c8dcSSimon Schubert 			      struct axs_value *value,
1395796c8dcSSimon Schubert 			      struct type *type, int start, int end);
140cf7f2e2dSJohn Marino static void gen_primitive_field (struct expression *exp,
141cf7f2e2dSJohn Marino 				 struct agent_expr *ax,
142cf7f2e2dSJohn Marino 				 struct axs_value *value,
143cf7f2e2dSJohn Marino 				 int offset, int fieldno, struct type *type);
144cf7f2e2dSJohn Marino static int gen_struct_ref_recursive (struct expression *exp,
145cf7f2e2dSJohn Marino 				     struct agent_expr *ax,
146cf7f2e2dSJohn Marino 				     struct axs_value *value,
147cf7f2e2dSJohn Marino 				     char *field, int offset,
148cf7f2e2dSJohn Marino 				     struct type *type);
1495796c8dcSSimon Schubert static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1505796c8dcSSimon Schubert 			    struct axs_value *value,
1515796c8dcSSimon Schubert 			    char *field,
1525796c8dcSSimon Schubert 			    char *operator_name, char *operand_name);
153cf7f2e2dSJohn Marino static void gen_static_field (struct gdbarch *gdbarch,
154cf7f2e2dSJohn Marino 			      struct agent_expr *ax, struct axs_value *value,
155cf7f2e2dSJohn Marino 			      struct type *type, int fieldno);
1565796c8dcSSimon Schubert static void gen_repeat (struct expression *exp, union exp_element **pc,
1575796c8dcSSimon Schubert 			struct agent_expr *ax, struct axs_value *value);
1585796c8dcSSimon Schubert static void gen_sizeof (struct expression *exp, union exp_element **pc,
1595796c8dcSSimon Schubert 			struct agent_expr *ax, struct axs_value *value,
1605796c8dcSSimon Schubert 			struct type *size_type);
161cf7f2e2dSJohn Marino static void gen_expr_binop_rest (struct expression *exp,
162cf7f2e2dSJohn Marino 				 enum exp_opcode op, union exp_element **pc,
163cf7f2e2dSJohn Marino 				 struct agent_expr *ax,
164cf7f2e2dSJohn Marino 				 struct axs_value *value,
165cf7f2e2dSJohn Marino 				 struct axs_value *value1,
166cf7f2e2dSJohn Marino 				 struct axs_value *value2);
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert static void agent_command (char *exp, int from_tty);
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert /* Detecting constant expressions.  */
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert /* If the variable reference at *PC is a constant, return its value.
1745796c8dcSSimon Schubert    Otherwise, return zero.
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert    Hey, Wally!  How can a variable reference be a constant?
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert    Well, Beav, this function really handles the OP_VAR_VALUE operator,
1795796c8dcSSimon Schubert    not specifically variable references.  GDB uses OP_VAR_VALUE to
1805796c8dcSSimon Schubert    refer to any kind of symbolic reference: function names, enum
1815796c8dcSSimon Schubert    elements, and goto labels are all handled through the OP_VAR_VALUE
1825796c8dcSSimon Schubert    operator, even though they're constants.  It makes sense given the
1835796c8dcSSimon Schubert    situation.
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert    Gee, Wally, don'cha wonder sometimes if data representations that
1865796c8dcSSimon Schubert    subvert commonly accepted definitions of terms in favor of heavily
1875796c8dcSSimon Schubert    context-specific interpretations are really just a tool of the
1885796c8dcSSimon Schubert    programming hegemony to preserve their power and exclude the
1895796c8dcSSimon Schubert    proletariat?  */
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert static struct value *
const_var_ref(struct symbol * var)1925796c8dcSSimon Schubert const_var_ref (struct symbol *var)
1935796c8dcSSimon Schubert {
1945796c8dcSSimon Schubert   struct type *type = SYMBOL_TYPE (var);
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert   switch (SYMBOL_CLASS (var))
1975796c8dcSSimon Schubert     {
1985796c8dcSSimon Schubert     case LOC_CONST:
1995796c8dcSSimon Schubert       return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert     case LOC_LABEL:
2025796c8dcSSimon Schubert       return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert     default:
2055796c8dcSSimon Schubert       return 0;
2065796c8dcSSimon Schubert     }
2075796c8dcSSimon Schubert }
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert 
2105796c8dcSSimon Schubert /* If the expression starting at *PC has a constant value, return it.
2115796c8dcSSimon Schubert    Otherwise, return zero.  If we return a value, then *PC will be
2125796c8dcSSimon Schubert    advanced to the end of it.  If we return zero, *PC could be
2135796c8dcSSimon Schubert    anywhere.  */
2145796c8dcSSimon Schubert static struct value *
const_expr(union exp_element ** pc)2155796c8dcSSimon Schubert const_expr (union exp_element **pc)
2165796c8dcSSimon Schubert {
2175796c8dcSSimon Schubert   enum exp_opcode op = (*pc)->opcode;
2185796c8dcSSimon Schubert   struct value *v1;
2195796c8dcSSimon Schubert 
2205796c8dcSSimon Schubert   switch (op)
2215796c8dcSSimon Schubert     {
2225796c8dcSSimon Schubert     case OP_LONG:
2235796c8dcSSimon Schubert       {
2245796c8dcSSimon Schubert 	struct type *type = (*pc)[1].type;
2255796c8dcSSimon Schubert 	LONGEST k = (*pc)[2].longconst;
226cf7f2e2dSJohn Marino 
2275796c8dcSSimon Schubert 	(*pc) += 4;
2285796c8dcSSimon Schubert 	return value_from_longest (type, k);
2295796c8dcSSimon Schubert       }
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert     case OP_VAR_VALUE:
2325796c8dcSSimon Schubert       {
2335796c8dcSSimon Schubert 	struct value *v = const_var_ref ((*pc)[2].symbol);
234cf7f2e2dSJohn Marino 
2355796c8dcSSimon Schubert 	(*pc) += 4;
2365796c8dcSSimon Schubert 	return v;
2375796c8dcSSimon Schubert       }
2385796c8dcSSimon Schubert 
2395796c8dcSSimon Schubert       /* We could add more operators in here.  */
2405796c8dcSSimon Schubert 
2415796c8dcSSimon Schubert     case UNOP_NEG:
2425796c8dcSSimon Schubert       (*pc)++;
2435796c8dcSSimon Schubert       v1 = const_expr (pc);
2445796c8dcSSimon Schubert       if (v1)
2455796c8dcSSimon Schubert 	return value_neg (v1);
2465796c8dcSSimon Schubert       else
2475796c8dcSSimon Schubert 	return 0;
2485796c8dcSSimon Schubert 
2495796c8dcSSimon Schubert     default:
2505796c8dcSSimon Schubert       return 0;
2515796c8dcSSimon Schubert     }
2525796c8dcSSimon Schubert }
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert /* Like const_expr, but guarantee also that *PC is undisturbed if the
2565796c8dcSSimon Schubert    expression is not constant.  */
2575796c8dcSSimon Schubert static struct value *
maybe_const_expr(union exp_element ** pc)2585796c8dcSSimon Schubert maybe_const_expr (union exp_element **pc)
2595796c8dcSSimon Schubert {
2605796c8dcSSimon Schubert   union exp_element *tentative_pc = *pc;
2615796c8dcSSimon Schubert   struct value *v = const_expr (&tentative_pc);
2625796c8dcSSimon Schubert 
2635796c8dcSSimon Schubert   /* If we got a value, then update the real PC.  */
2645796c8dcSSimon Schubert   if (v)
2655796c8dcSSimon Schubert     *pc = tentative_pc;
2665796c8dcSSimon Schubert 
2675796c8dcSSimon Schubert   return v;
2685796c8dcSSimon Schubert }
2695796c8dcSSimon Schubert 
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: general assumptions */
2725796c8dcSSimon Schubert 
2735796c8dcSSimon Schubert /* Here are a few general assumptions made throughout the code; if you
2745796c8dcSSimon Schubert    want to make a change that contradicts one of these, then you'd
2755796c8dcSSimon Schubert    better scan things pretty thoroughly.
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert    - We assume that all values occupy one stack element.  For example,
2785796c8dcSSimon Schubert    sometimes we'll swap to get at the left argument to a binary
2795796c8dcSSimon Schubert    operator.  If we decide that void values should occupy no stack
2805796c8dcSSimon Schubert    elements, or that synthetic arrays (whose size is determined at
2815796c8dcSSimon Schubert    run time, created by the `@' operator) should occupy two stack
2825796c8dcSSimon Schubert    elements (address and length), then this will cause trouble.
2835796c8dcSSimon Schubert 
2845796c8dcSSimon Schubert    - We assume the stack elements are infinitely wide, and that we
2855796c8dcSSimon Schubert    don't have to worry what happens if the user requests an
2865796c8dcSSimon Schubert    operation that is wider than the actual interpreter's stack.
2875796c8dcSSimon Schubert    That is, it's up to the interpreter to handle directly all the
2885796c8dcSSimon Schubert    integer widths the user has access to.  (Woe betide the language
2895796c8dcSSimon Schubert    with bignums!)
2905796c8dcSSimon Schubert 
2915796c8dcSSimon Schubert    - We don't support side effects.  Thus, we don't have to worry about
2925796c8dcSSimon Schubert    GCC's generalized lvalues, function calls, etc.
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert    - We don't support floating point.  Many places where we switch on
2955796c8dcSSimon Schubert    some type don't bother to include cases for floating point; there
2965796c8dcSSimon Schubert    may be even more subtle ways this assumption exists.  For
2975796c8dcSSimon Schubert    example, the arguments to % must be integers.
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert    - We assume all subexpressions have a static, unchanging type.  If
3005796c8dcSSimon Schubert    we tried to support convenience variables, this would be a
3015796c8dcSSimon Schubert    problem.
3025796c8dcSSimon Schubert 
3035796c8dcSSimon Schubert    - All values on the stack should always be fully zero- or
3045796c8dcSSimon Schubert    sign-extended.
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert    (I wasn't sure whether to choose this or its opposite --- that
3075796c8dcSSimon Schubert    only addresses are assumed extended --- but it turns out that
3085796c8dcSSimon Schubert    neither convention completely eliminates spurious extend
3095796c8dcSSimon Schubert    operations (if everything is always extended, then you have to
3105796c8dcSSimon Schubert    extend after add, because it could overflow; if nothing is
3115796c8dcSSimon Schubert    extended, then you end up producing extends whenever you change
3125796c8dcSSimon Schubert    sizes), and this is simpler.)  */
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert 
3155796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: the `trace' kludge  */
3165796c8dcSSimon Schubert 
3175796c8dcSSimon Schubert /* The compiler in this file is a general-purpose mechanism for
3185796c8dcSSimon Schubert    translating GDB expressions into bytecode.  One ought to be able to
3195796c8dcSSimon Schubert    find a million and one uses for it.
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert    However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
3225796c8dcSSimon Schubert    of expediency.  Let he who is without sin cast the first stone.
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert    For the data tracing facility, we need to insert `trace' bytecodes
3255796c8dcSSimon Schubert    before each data fetch; this records all the memory that the
3265796c8dcSSimon Schubert    expression touches in the course of evaluation, so that memory will
3275796c8dcSSimon Schubert    be available when the user later tries to evaluate the expression
3285796c8dcSSimon Schubert    in GDB.
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert    This should be done (I think) in a post-processing pass, that walks
3315796c8dcSSimon Schubert    an arbitrary agent expression and inserts `trace' operations at the
3325796c8dcSSimon Schubert    appropriate points.  But it's much faster to just hack them
3335796c8dcSSimon Schubert    directly into the code.  And since we're in a crunch, that's what
3345796c8dcSSimon Schubert    I've done.
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert    Setting the flag trace_kludge to non-zero enables the code that
3375796c8dcSSimon Schubert    emits the trace bytecodes at the appropriate points.  */
338cf7f2e2dSJohn Marino int trace_kludge;
339cf7f2e2dSJohn Marino 
340a45ae5f8SJohn Marino /* Inspired by trace_kludge, this indicates that pointers to chars
341a45ae5f8SJohn Marino    should get an added tracenz bytecode to record nonzero bytes, up to
342a45ae5f8SJohn Marino    a length that is the value of trace_string_kludge.  */
343a45ae5f8SJohn Marino int trace_string_kludge;
344a45ae5f8SJohn Marino 
345cf7f2e2dSJohn Marino /* Scan for all static fields in the given class, including any base
346cf7f2e2dSJohn Marino    classes, and generate tracing bytecodes for each.  */
347cf7f2e2dSJohn Marino 
348cf7f2e2dSJohn Marino static void
gen_trace_static_fields(struct gdbarch * gdbarch,struct agent_expr * ax,struct type * type)349cf7f2e2dSJohn Marino gen_trace_static_fields (struct gdbarch *gdbarch,
350cf7f2e2dSJohn Marino 			 struct agent_expr *ax,
351cf7f2e2dSJohn Marino 			 struct type *type)
352cf7f2e2dSJohn Marino {
353cf7f2e2dSJohn Marino   int i, nbases = TYPE_N_BASECLASSES (type);
354cf7f2e2dSJohn Marino   struct axs_value value;
355cf7f2e2dSJohn Marino 
356cf7f2e2dSJohn Marino   CHECK_TYPEDEF (type);
357cf7f2e2dSJohn Marino 
358cf7f2e2dSJohn Marino   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
359cf7f2e2dSJohn Marino     {
360cf7f2e2dSJohn Marino       if (field_is_static (&TYPE_FIELD (type, i)))
361cf7f2e2dSJohn Marino 	{
362cf7f2e2dSJohn Marino 	  gen_static_field (gdbarch, ax, &value, type, i);
363cf7f2e2dSJohn Marino 	  if (value.optimized_out)
364cf7f2e2dSJohn Marino 	    continue;
365cf7f2e2dSJohn Marino 	  switch (value.kind)
366cf7f2e2dSJohn Marino 	    {
367cf7f2e2dSJohn Marino 	    case axs_lvalue_memory:
368cf7f2e2dSJohn Marino 	      {
369*ef5ccd6cSJohn Marino 	        /* Initialize the TYPE_LENGTH if it is a typedef.  */
370*ef5ccd6cSJohn Marino 	        check_typedef (value.type);
371*ef5ccd6cSJohn Marino 		ax_const_l (ax, TYPE_LENGTH (value.type));
372cf7f2e2dSJohn Marino 		ax_simple (ax, aop_trace);
373cf7f2e2dSJohn Marino 	      }
374cf7f2e2dSJohn Marino 	      break;
375cf7f2e2dSJohn Marino 
376cf7f2e2dSJohn Marino 	    case axs_lvalue_register:
377cf7f2e2dSJohn Marino 	      /* We don't actually need the register's value to be pushed,
378cf7f2e2dSJohn Marino 		 just note that we need it to be collected.  */
379cf7f2e2dSJohn Marino 	      ax_reg_mask (ax, value.u.reg);
380cf7f2e2dSJohn Marino 
381cf7f2e2dSJohn Marino 	    default:
382cf7f2e2dSJohn Marino 	      break;
383cf7f2e2dSJohn Marino 	    }
384cf7f2e2dSJohn Marino 	}
385cf7f2e2dSJohn Marino     }
386cf7f2e2dSJohn Marino 
387cf7f2e2dSJohn Marino   /* Now scan through base classes recursively.  */
388cf7f2e2dSJohn Marino   for (i = 0; i < nbases; i++)
389cf7f2e2dSJohn Marino     {
390cf7f2e2dSJohn Marino       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
391cf7f2e2dSJohn Marino 
392cf7f2e2dSJohn Marino       gen_trace_static_fields (gdbarch, ax, basetype);
393cf7f2e2dSJohn Marino     }
394cf7f2e2dSJohn Marino }
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert /* Trace the lvalue on the stack, if it needs it.  In either case, pop
3975796c8dcSSimon Schubert    the value.  Useful on the left side of a comma, and at the end of
3985796c8dcSSimon Schubert    an expression being used for tracing.  */
3995796c8dcSSimon Schubert static void
gen_traced_pop(struct gdbarch * gdbarch,struct agent_expr * ax,struct axs_value * value)400cf7f2e2dSJohn Marino gen_traced_pop (struct gdbarch *gdbarch,
401cf7f2e2dSJohn Marino 		struct agent_expr *ax, struct axs_value *value)
4025796c8dcSSimon Schubert {
403a45ae5f8SJohn Marino   int string_trace = 0;
404a45ae5f8SJohn Marino   if (trace_string_kludge
405a45ae5f8SJohn Marino       && TYPE_CODE (value->type) == TYPE_CODE_PTR
406a45ae5f8SJohn Marino       && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
407a45ae5f8SJohn Marino 				 's'))
408a45ae5f8SJohn Marino     string_trace = 1;
409a45ae5f8SJohn Marino 
4105796c8dcSSimon Schubert   if (trace_kludge)
4115796c8dcSSimon Schubert     switch (value->kind)
4125796c8dcSSimon Schubert       {
4135796c8dcSSimon Schubert       case axs_rvalue:
414a45ae5f8SJohn Marino 	if (string_trace)
415a45ae5f8SJohn Marino 	  {
416a45ae5f8SJohn Marino 	    ax_const_l (ax, trace_string_kludge);
417a45ae5f8SJohn Marino 	    ax_simple (ax, aop_tracenz);
418a45ae5f8SJohn Marino 	  }
419a45ae5f8SJohn Marino 	else
4205796c8dcSSimon Schubert 	  /* We don't trace rvalues, just the lvalues necessary to
4215796c8dcSSimon Schubert 	     produce them.  So just dispose of this value.  */
4225796c8dcSSimon Schubert 	  ax_simple (ax, aop_pop);
4235796c8dcSSimon Schubert 	break;
4245796c8dcSSimon Schubert 
4255796c8dcSSimon Schubert       case axs_lvalue_memory:
4265796c8dcSSimon Schubert 	{
427a45ae5f8SJohn Marino 	  if (string_trace)
428a45ae5f8SJohn Marino 	    ax_simple (ax, aop_dup);
429a45ae5f8SJohn Marino 
430*ef5ccd6cSJohn Marino 	  /* Initialize the TYPE_LENGTH if it is a typedef.  */
431*ef5ccd6cSJohn Marino 	  check_typedef (value->type);
432*ef5ccd6cSJohn Marino 
4335796c8dcSSimon Schubert 	  /* There's no point in trying to use a trace_quick bytecode
4345796c8dcSSimon Schubert 	     here, since "trace_quick SIZE pop" is three bytes, whereas
4355796c8dcSSimon Schubert 	     "const8 SIZE trace" is also three bytes, does the same
4365796c8dcSSimon Schubert 	     thing, and the simplest code which generates that will also
4375796c8dcSSimon Schubert 	     work correctly for objects with large sizes.  */
438*ef5ccd6cSJohn Marino 	  ax_const_l (ax, TYPE_LENGTH (value->type));
4395796c8dcSSimon Schubert 	  ax_simple (ax, aop_trace);
440a45ae5f8SJohn Marino 
441a45ae5f8SJohn Marino 	  if (string_trace)
442a45ae5f8SJohn Marino 	    {
443a45ae5f8SJohn Marino 	      ax_simple (ax, aop_ref32);
444a45ae5f8SJohn Marino 	      ax_const_l (ax, trace_string_kludge);
445a45ae5f8SJohn Marino 	      ax_simple (ax, aop_tracenz);
446a45ae5f8SJohn Marino 	    }
4475796c8dcSSimon Schubert 	}
4485796c8dcSSimon Schubert 	break;
4495796c8dcSSimon Schubert 
4505796c8dcSSimon Schubert       case axs_lvalue_register:
451cf7f2e2dSJohn Marino 	/* We don't actually need the register's value to be on the
452cf7f2e2dSJohn Marino 	   stack, and the target will get heartburn if the register is
453cf7f2e2dSJohn Marino 	   larger than will fit in a stack, so just mark it for
454cf7f2e2dSJohn Marino 	   collection and be done with it.  */
455cf7f2e2dSJohn Marino 	ax_reg_mask (ax, value->u.reg);
456a45ae5f8SJohn Marino 
457a45ae5f8SJohn Marino 	/* But if the register points to a string, assume the value
458a45ae5f8SJohn Marino 	   will fit on the stack and push it anyway.  */
459a45ae5f8SJohn Marino 	if (string_trace)
460a45ae5f8SJohn Marino 	  {
461a45ae5f8SJohn Marino 	    ax_reg (ax, value->u.reg);
462a45ae5f8SJohn Marino 	    ax_const_l (ax, trace_string_kludge);
463a45ae5f8SJohn Marino 	    ax_simple (ax, aop_tracenz);
464a45ae5f8SJohn Marino 	  }
4655796c8dcSSimon Schubert 	break;
4665796c8dcSSimon Schubert       }
4675796c8dcSSimon Schubert   else
4685796c8dcSSimon Schubert     /* If we're not tracing, just pop the value.  */
4695796c8dcSSimon Schubert     ax_simple (ax, aop_pop);
470cf7f2e2dSJohn Marino 
471cf7f2e2dSJohn Marino   /* To trace C++ classes with static fields stored elsewhere.  */
472cf7f2e2dSJohn Marino   if (trace_kludge
473cf7f2e2dSJohn Marino       && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
474cf7f2e2dSJohn Marino 	  || TYPE_CODE (value->type) == TYPE_CODE_UNION))
475cf7f2e2dSJohn Marino     gen_trace_static_fields (gdbarch, ax, value->type);
4765796c8dcSSimon Schubert }
4775796c8dcSSimon Schubert 
4785796c8dcSSimon Schubert 
4795796c8dcSSimon Schubert 
4805796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: helper functions */
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert /* Assume that the lower bits of the top of the stack is a value of
4835796c8dcSSimon Schubert    type TYPE, and the upper bits are zero.  Sign-extend if necessary.  */
4845796c8dcSSimon Schubert static void
gen_sign_extend(struct agent_expr * ax,struct type * type)4855796c8dcSSimon Schubert gen_sign_extend (struct agent_expr *ax, struct type *type)
4865796c8dcSSimon Schubert {
4875796c8dcSSimon Schubert   /* Do we need to sign-extend this?  */
4885796c8dcSSimon Schubert   if (!TYPE_UNSIGNED (type))
4895796c8dcSSimon Schubert     ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
4905796c8dcSSimon Schubert }
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert /* Assume the lower bits of the top of the stack hold a value of type
4945796c8dcSSimon Schubert    TYPE, and the upper bits are garbage.  Sign-extend or truncate as
4955796c8dcSSimon Schubert    needed.  */
4965796c8dcSSimon Schubert static void
gen_extend(struct agent_expr * ax,struct type * type)4975796c8dcSSimon Schubert gen_extend (struct agent_expr *ax, struct type *type)
4985796c8dcSSimon Schubert {
4995796c8dcSSimon Schubert   int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
500cf7f2e2dSJohn Marino 
5015796c8dcSSimon Schubert   /* I just had to.  */
5025796c8dcSSimon Schubert   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
5035796c8dcSSimon Schubert }
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert 
5065796c8dcSSimon Schubert /* Assume that the top of the stack contains a value of type "pointer
5075796c8dcSSimon Schubert    to TYPE"; generate code to fetch its value.  Note that TYPE is the
5085796c8dcSSimon Schubert    target type, not the pointer type.  */
5095796c8dcSSimon Schubert static void
gen_fetch(struct agent_expr * ax,struct type * type)5105796c8dcSSimon Schubert gen_fetch (struct agent_expr *ax, struct type *type)
5115796c8dcSSimon Schubert {
5125796c8dcSSimon Schubert   if (trace_kludge)
5135796c8dcSSimon Schubert     {
5145796c8dcSSimon Schubert       /* Record the area of memory we're about to fetch.  */
5155796c8dcSSimon Schubert       ax_trace_quick (ax, TYPE_LENGTH (type));
5165796c8dcSSimon Schubert     }
5175796c8dcSSimon Schubert 
518*ef5ccd6cSJohn Marino   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
519*ef5ccd6cSJohn Marino     type = TYPE_TARGET_TYPE (type);
520*ef5ccd6cSJohn Marino 
5215796c8dcSSimon Schubert   switch (TYPE_CODE (type))
5225796c8dcSSimon Schubert     {
5235796c8dcSSimon Schubert     case TYPE_CODE_PTR:
524cf7f2e2dSJohn Marino     case TYPE_CODE_REF:
5255796c8dcSSimon Schubert     case TYPE_CODE_ENUM:
5265796c8dcSSimon Schubert     case TYPE_CODE_INT:
5275796c8dcSSimon Schubert     case TYPE_CODE_CHAR:
528cf7f2e2dSJohn Marino     case TYPE_CODE_BOOL:
5295796c8dcSSimon Schubert       /* It's a scalar value, so we know how to dereference it.  How
5305796c8dcSSimon Schubert          many bytes long is it?  */
5315796c8dcSSimon Schubert       switch (TYPE_LENGTH (type))
5325796c8dcSSimon Schubert 	{
5335796c8dcSSimon Schubert 	case 8 / TARGET_CHAR_BIT:
5345796c8dcSSimon Schubert 	  ax_simple (ax, aop_ref8);
5355796c8dcSSimon Schubert 	  break;
5365796c8dcSSimon Schubert 	case 16 / TARGET_CHAR_BIT:
5375796c8dcSSimon Schubert 	  ax_simple (ax, aop_ref16);
5385796c8dcSSimon Schubert 	  break;
5395796c8dcSSimon Schubert 	case 32 / TARGET_CHAR_BIT:
5405796c8dcSSimon Schubert 	  ax_simple (ax, aop_ref32);
5415796c8dcSSimon Schubert 	  break;
5425796c8dcSSimon Schubert 	case 64 / TARGET_CHAR_BIT:
5435796c8dcSSimon Schubert 	  ax_simple (ax, aop_ref64);
5445796c8dcSSimon Schubert 	  break;
5455796c8dcSSimon Schubert 
5465796c8dcSSimon Schubert 	  /* Either our caller shouldn't have asked us to dereference
5475796c8dcSSimon Schubert 	     that pointer (other code's fault), or we're not
5485796c8dcSSimon Schubert 	     implementing something we should be (this code's fault).
5495796c8dcSSimon Schubert 	     In any case, it's a bug the user shouldn't see.  */
5505796c8dcSSimon Schubert 	default:
5515796c8dcSSimon Schubert 	  internal_error (__FILE__, __LINE__,
5525796c8dcSSimon Schubert 			  _("gen_fetch: strange size"));
5535796c8dcSSimon Schubert 	}
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert       gen_sign_extend (ax, type);
5565796c8dcSSimon Schubert       break;
5575796c8dcSSimon Schubert 
5585796c8dcSSimon Schubert     default:
559*ef5ccd6cSJohn Marino       /* Our caller requested us to dereference a pointer from an unsupported
560*ef5ccd6cSJohn Marino 	 type.  Error out and give callers a chance to handle the failure
561*ef5ccd6cSJohn Marino 	 gracefully.  */
562*ef5ccd6cSJohn Marino       error (_("gen_fetch: Unsupported type code `%s'."),
563*ef5ccd6cSJohn Marino 	     TYPE_NAME (type));
5645796c8dcSSimon Schubert     }
5655796c8dcSSimon Schubert }
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert /* Generate code to left shift the top of the stack by DISTANCE bits, or
5695796c8dcSSimon Schubert    right shift it by -DISTANCE bits if DISTANCE < 0.  This generates
5705796c8dcSSimon Schubert    unsigned (logical) right shifts.  */
5715796c8dcSSimon Schubert static void
gen_left_shift(struct agent_expr * ax,int distance)5725796c8dcSSimon Schubert gen_left_shift (struct agent_expr *ax, int distance)
5735796c8dcSSimon Schubert {
5745796c8dcSSimon Schubert   if (distance > 0)
5755796c8dcSSimon Schubert     {
5765796c8dcSSimon Schubert       ax_const_l (ax, distance);
5775796c8dcSSimon Schubert       ax_simple (ax, aop_lsh);
5785796c8dcSSimon Schubert     }
5795796c8dcSSimon Schubert   else if (distance < 0)
5805796c8dcSSimon Schubert     {
5815796c8dcSSimon Schubert       ax_const_l (ax, -distance);
5825796c8dcSSimon Schubert       ax_simple (ax, aop_rsh_unsigned);
5835796c8dcSSimon Schubert     }
5845796c8dcSSimon Schubert }
5855796c8dcSSimon Schubert 
5865796c8dcSSimon Schubert 
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: symbol references */
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert /* Generate code to push the base address of the argument portion of
5915796c8dcSSimon Schubert    the top stack frame.  */
5925796c8dcSSimon Schubert static void
gen_frame_args_address(struct gdbarch * gdbarch,struct agent_expr * ax)5935796c8dcSSimon Schubert gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
5945796c8dcSSimon Schubert {
5955796c8dcSSimon Schubert   int frame_reg;
5965796c8dcSSimon Schubert   LONGEST frame_offset;
5975796c8dcSSimon Schubert 
5985796c8dcSSimon Schubert   gdbarch_virtual_frame_pointer (gdbarch,
5995796c8dcSSimon Schubert 				 ax->scope, &frame_reg, &frame_offset);
6005796c8dcSSimon Schubert   ax_reg (ax, frame_reg);
6015796c8dcSSimon Schubert   gen_offset (ax, frame_offset);
6025796c8dcSSimon Schubert }
6035796c8dcSSimon Schubert 
6045796c8dcSSimon Schubert 
6055796c8dcSSimon Schubert /* Generate code to push the base address of the locals portion of the
6065796c8dcSSimon Schubert    top stack frame.  */
6075796c8dcSSimon Schubert static void
gen_frame_locals_address(struct gdbarch * gdbarch,struct agent_expr * ax)6085796c8dcSSimon Schubert gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
6095796c8dcSSimon Schubert {
6105796c8dcSSimon Schubert   int frame_reg;
6115796c8dcSSimon Schubert   LONGEST frame_offset;
6125796c8dcSSimon Schubert 
6135796c8dcSSimon Schubert   gdbarch_virtual_frame_pointer (gdbarch,
6145796c8dcSSimon Schubert 				 ax->scope, &frame_reg, &frame_offset);
6155796c8dcSSimon Schubert   ax_reg (ax, frame_reg);
6165796c8dcSSimon Schubert   gen_offset (ax, frame_offset);
6175796c8dcSSimon Schubert }
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert 
6205796c8dcSSimon Schubert /* Generate code to add OFFSET to the top of the stack.  Try to
6215796c8dcSSimon Schubert    generate short and readable code.  We use this for getting to
6225796c8dcSSimon Schubert    variables on the stack, and structure members.  If we were
6235796c8dcSSimon Schubert    programming in ML, it would be clearer why these are the same
6245796c8dcSSimon Schubert    thing.  */
6255796c8dcSSimon Schubert static void
gen_offset(struct agent_expr * ax,int offset)6265796c8dcSSimon Schubert gen_offset (struct agent_expr *ax, int offset)
6275796c8dcSSimon Schubert {
6285796c8dcSSimon Schubert   /* It would suffice to simply push the offset and add it, but this
6295796c8dcSSimon Schubert      makes it easier to read positive and negative offsets in the
6305796c8dcSSimon Schubert      bytecode.  */
6315796c8dcSSimon Schubert   if (offset > 0)
6325796c8dcSSimon Schubert     {
6335796c8dcSSimon Schubert       ax_const_l (ax, offset);
6345796c8dcSSimon Schubert       ax_simple (ax, aop_add);
6355796c8dcSSimon Schubert     }
6365796c8dcSSimon Schubert   else if (offset < 0)
6375796c8dcSSimon Schubert     {
6385796c8dcSSimon Schubert       ax_const_l (ax, -offset);
6395796c8dcSSimon Schubert       ax_simple (ax, aop_sub);
6405796c8dcSSimon Schubert     }
6415796c8dcSSimon Schubert }
6425796c8dcSSimon Schubert 
6435796c8dcSSimon Schubert 
6445796c8dcSSimon Schubert /* In many cases, a symbol's value is the offset from some other
6455796c8dcSSimon Schubert    address (stack frame, base register, etc.)  Generate code to add
6465796c8dcSSimon Schubert    VAR's value to the top of the stack.  */
6475796c8dcSSimon Schubert static void
gen_sym_offset(struct agent_expr * ax,struct symbol * var)6485796c8dcSSimon Schubert gen_sym_offset (struct agent_expr *ax, struct symbol *var)
6495796c8dcSSimon Schubert {
6505796c8dcSSimon Schubert   gen_offset (ax, SYMBOL_VALUE (var));
6515796c8dcSSimon Schubert }
6525796c8dcSSimon Schubert 
6535796c8dcSSimon Schubert 
6545796c8dcSSimon Schubert /* Generate code for a variable reference to AX.  The variable is the
6555796c8dcSSimon Schubert    symbol VAR.  Set VALUE to describe the result.  */
6565796c8dcSSimon Schubert 
6575796c8dcSSimon Schubert static void
gen_var_ref(struct gdbarch * gdbarch,struct agent_expr * ax,struct axs_value * value,struct symbol * var)6585796c8dcSSimon Schubert gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
6595796c8dcSSimon Schubert 	     struct axs_value *value, struct symbol *var)
6605796c8dcSSimon Schubert {
6615796c8dcSSimon Schubert   /* Dereference any typedefs.  */
6625796c8dcSSimon Schubert   value->type = check_typedef (SYMBOL_TYPE (var));
663cf7f2e2dSJohn Marino   value->optimized_out = 0;
6645796c8dcSSimon Schubert 
6655796c8dcSSimon Schubert   /* I'm imitating the code in read_var_value.  */
6665796c8dcSSimon Schubert   switch (SYMBOL_CLASS (var))
6675796c8dcSSimon Schubert     {
6685796c8dcSSimon Schubert     case LOC_CONST:		/* A constant, like an enum value.  */
6695796c8dcSSimon Schubert       ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
6705796c8dcSSimon Schubert       value->kind = axs_rvalue;
6715796c8dcSSimon Schubert       break;
6725796c8dcSSimon Schubert 
6735796c8dcSSimon Schubert     case LOC_LABEL:		/* A goto label, being used as a value.  */
6745796c8dcSSimon Schubert       ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
6755796c8dcSSimon Schubert       value->kind = axs_rvalue;
6765796c8dcSSimon Schubert       break;
6775796c8dcSSimon Schubert 
6785796c8dcSSimon Schubert     case LOC_CONST_BYTES:
6795796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
680c50c785cSJohn Marino 		      _("gen_var_ref: LOC_CONST_BYTES "
681c50c785cSJohn Marino 			"symbols are not supported"));
6825796c8dcSSimon Schubert 
6835796c8dcSSimon Schubert       /* Variable at a fixed location in memory.  Easy.  */
6845796c8dcSSimon Schubert     case LOC_STATIC:
6855796c8dcSSimon Schubert       /* Push the address of the variable.  */
6865796c8dcSSimon Schubert       ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
6875796c8dcSSimon Schubert       value->kind = axs_lvalue_memory;
6885796c8dcSSimon Schubert       break;
6895796c8dcSSimon Schubert 
6905796c8dcSSimon Schubert     case LOC_ARG:		/* var lives in argument area of frame */
6915796c8dcSSimon Schubert       gen_frame_args_address (gdbarch, ax);
6925796c8dcSSimon Schubert       gen_sym_offset (ax, var);
6935796c8dcSSimon Schubert       value->kind = axs_lvalue_memory;
6945796c8dcSSimon Schubert       break;
6955796c8dcSSimon Schubert 
6965796c8dcSSimon Schubert     case LOC_REF_ARG:		/* As above, but the frame slot really
6975796c8dcSSimon Schubert 				   holds the address of the variable.  */
6985796c8dcSSimon Schubert       gen_frame_args_address (gdbarch, ax);
6995796c8dcSSimon Schubert       gen_sym_offset (ax, var);
7005796c8dcSSimon Schubert       /* Don't assume any particular pointer size.  */
7015796c8dcSSimon Schubert       gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
7025796c8dcSSimon Schubert       value->kind = axs_lvalue_memory;
7035796c8dcSSimon Schubert       break;
7045796c8dcSSimon Schubert 
7055796c8dcSSimon Schubert     case LOC_LOCAL:		/* var lives in locals area of frame */
7065796c8dcSSimon Schubert       gen_frame_locals_address (gdbarch, ax);
7075796c8dcSSimon Schubert       gen_sym_offset (ax, var);
7085796c8dcSSimon Schubert       value->kind = axs_lvalue_memory;
7095796c8dcSSimon Schubert       break;
7105796c8dcSSimon Schubert 
7115796c8dcSSimon Schubert     case LOC_TYPEDEF:
7125796c8dcSSimon Schubert       error (_("Cannot compute value of typedef `%s'."),
7135796c8dcSSimon Schubert 	     SYMBOL_PRINT_NAME (var));
7145796c8dcSSimon Schubert       break;
7155796c8dcSSimon Schubert 
7165796c8dcSSimon Schubert     case LOC_BLOCK:
7175796c8dcSSimon Schubert       ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
7185796c8dcSSimon Schubert       value->kind = axs_rvalue;
7195796c8dcSSimon Schubert       break;
7205796c8dcSSimon Schubert 
7215796c8dcSSimon Schubert     case LOC_REGISTER:
7225796c8dcSSimon Schubert       /* Don't generate any code at all; in the process of treating
7235796c8dcSSimon Schubert          this as an lvalue or rvalue, the caller will generate the
7245796c8dcSSimon Schubert          right code.  */
7255796c8dcSSimon Schubert       value->kind = axs_lvalue_register;
7265796c8dcSSimon Schubert       value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
7275796c8dcSSimon Schubert       break;
7285796c8dcSSimon Schubert 
7295796c8dcSSimon Schubert       /* A lot like LOC_REF_ARG, but the pointer lives directly in a
7305796c8dcSSimon Schubert          register, not on the stack.  Simpler than LOC_REGISTER
7315796c8dcSSimon Schubert          because it's just like any other case where the thing
7325796c8dcSSimon Schubert 	 has a real address.  */
7335796c8dcSSimon Schubert     case LOC_REGPARM_ADDR:
7345796c8dcSSimon Schubert       ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
7355796c8dcSSimon Schubert       value->kind = axs_lvalue_memory;
7365796c8dcSSimon Schubert       break;
7375796c8dcSSimon Schubert 
7385796c8dcSSimon Schubert     case LOC_UNRESOLVED:
7395796c8dcSSimon Schubert       {
7405796c8dcSSimon Schubert 	struct minimal_symbol *msym
7415796c8dcSSimon Schubert 	  = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
742cf7f2e2dSJohn Marino 
7435796c8dcSSimon Schubert 	if (!msym)
7445796c8dcSSimon Schubert 	  error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
7455796c8dcSSimon Schubert 
7465796c8dcSSimon Schubert 	/* Push the address of the variable.  */
7475796c8dcSSimon Schubert 	ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
7485796c8dcSSimon Schubert 	value->kind = axs_lvalue_memory;
7495796c8dcSSimon Schubert       }
7505796c8dcSSimon Schubert       break;
7515796c8dcSSimon Schubert 
7525796c8dcSSimon Schubert     case LOC_COMPUTED:
7535796c8dcSSimon Schubert       /* FIXME: cagney/2004-01-26: It should be possible to
7545796c8dcSSimon Schubert 	 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
7555796c8dcSSimon Schubert 	 Unfortunately DWARF 2 stores the frame-base (instead of the
7565796c8dcSSimon Schubert 	 function) location in a function's symbol.  Oops!  For the
7575796c8dcSSimon Schubert 	 moment enable this when/where applicable.  */
7585796c8dcSSimon Schubert       SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
7595796c8dcSSimon Schubert       break;
7605796c8dcSSimon Schubert 
7615796c8dcSSimon Schubert     case LOC_OPTIMIZED_OUT:
762cf7f2e2dSJohn Marino       /* Flag this, but don't say anything; leave it up to callers to
763cf7f2e2dSJohn Marino 	 warn the user.  */
764cf7f2e2dSJohn Marino       value->optimized_out = 1;
7655796c8dcSSimon Schubert       break;
7665796c8dcSSimon Schubert 
7675796c8dcSSimon Schubert     default:
7685796c8dcSSimon Schubert       error (_("Cannot find value of botched symbol `%s'."),
7695796c8dcSSimon Schubert 	     SYMBOL_PRINT_NAME (var));
7705796c8dcSSimon Schubert       break;
7715796c8dcSSimon Schubert     }
7725796c8dcSSimon Schubert }
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert 
7755796c8dcSSimon Schubert 
7765796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: literals */
7775796c8dcSSimon Schubert 
7785796c8dcSSimon Schubert static void
gen_int_literal(struct agent_expr * ax,struct axs_value * value,LONGEST k,struct type * type)7795796c8dcSSimon Schubert gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
7805796c8dcSSimon Schubert 		 struct type *type)
7815796c8dcSSimon Schubert {
7825796c8dcSSimon Schubert   ax_const_l (ax, k);
7835796c8dcSSimon Schubert   value->kind = axs_rvalue;
7845796c8dcSSimon Schubert   value->type = check_typedef (type);
7855796c8dcSSimon Schubert }
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert 
7885796c8dcSSimon Schubert 
7895796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: unary conversions, casts */
7905796c8dcSSimon Schubert 
7915796c8dcSSimon Schubert /* Take what's on the top of the stack (as described by VALUE), and
7925796c8dcSSimon Schubert    try to make an rvalue out of it.  Signal an error if we can't do
7935796c8dcSSimon Schubert    that.  */
794*ef5ccd6cSJohn Marino void
require_rvalue(struct agent_expr * ax,struct axs_value * value)7955796c8dcSSimon Schubert require_rvalue (struct agent_expr *ax, struct axs_value *value)
7965796c8dcSSimon Schubert {
797cf7f2e2dSJohn Marino   /* Only deal with scalars, structs and such may be too large
798cf7f2e2dSJohn Marino      to fit in a stack entry.  */
799cf7f2e2dSJohn Marino   value->type = check_typedef (value->type);
800cf7f2e2dSJohn Marino   if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
801cf7f2e2dSJohn Marino       || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
802cf7f2e2dSJohn Marino       || TYPE_CODE (value->type) == TYPE_CODE_UNION
803cf7f2e2dSJohn Marino       || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
804cf7f2e2dSJohn Marino     error (_("Value not scalar: cannot be an rvalue."));
805cf7f2e2dSJohn Marino 
8065796c8dcSSimon Schubert   switch (value->kind)
8075796c8dcSSimon Schubert     {
8085796c8dcSSimon Schubert     case axs_rvalue:
8095796c8dcSSimon Schubert       /* It's already an rvalue.  */
8105796c8dcSSimon Schubert       break;
8115796c8dcSSimon Schubert 
8125796c8dcSSimon Schubert     case axs_lvalue_memory:
8135796c8dcSSimon Schubert       /* The top of stack is the address of the object.  Dereference.  */
8145796c8dcSSimon Schubert       gen_fetch (ax, value->type);
8155796c8dcSSimon Schubert       break;
8165796c8dcSSimon Schubert 
8175796c8dcSSimon Schubert     case axs_lvalue_register:
8185796c8dcSSimon Schubert       /* There's nothing on the stack, but value->u.reg is the
8195796c8dcSSimon Schubert          register number containing the value.
8205796c8dcSSimon Schubert 
8215796c8dcSSimon Schubert          When we add floating-point support, this is going to have to
8225796c8dcSSimon Schubert          change.  What about SPARC register pairs, for example?  */
8235796c8dcSSimon Schubert       ax_reg (ax, value->u.reg);
8245796c8dcSSimon Schubert       gen_extend (ax, value->type);
8255796c8dcSSimon Schubert       break;
8265796c8dcSSimon Schubert     }
8275796c8dcSSimon Schubert 
8285796c8dcSSimon Schubert   value->kind = axs_rvalue;
8295796c8dcSSimon Schubert }
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert 
8325796c8dcSSimon Schubert /* Assume the top of the stack is described by VALUE, and perform the
8335796c8dcSSimon Schubert    usual unary conversions.  This is motivated by ANSI 6.2.2, but of
8345796c8dcSSimon Schubert    course GDB expressions are not ANSI; they're the mishmash union of
8355796c8dcSSimon Schubert    a bunch of languages.  Rah.
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert    NOTE!  This function promises to produce an rvalue only when the
8385796c8dcSSimon Schubert    incoming value is of an appropriate type.  In other words, the
8395796c8dcSSimon Schubert    consumer of the value this function produces may assume the value
8405796c8dcSSimon Schubert    is an rvalue only after checking its type.
8415796c8dcSSimon Schubert 
8425796c8dcSSimon Schubert    The immediate issue is that if the user tries to use a structure or
8435796c8dcSSimon Schubert    union as an operand of, say, the `+' operator, we don't want to try
8445796c8dcSSimon Schubert    to convert that structure to an rvalue; require_rvalue will bomb on
8455796c8dcSSimon Schubert    structs and unions.  Rather, we want to simply pass the struct
8465796c8dcSSimon Schubert    lvalue through unchanged, and let `+' raise an error.  */
8475796c8dcSSimon Schubert 
8485796c8dcSSimon Schubert static void
gen_usual_unary(struct expression * exp,struct agent_expr * ax,struct axs_value * value)8495796c8dcSSimon Schubert gen_usual_unary (struct expression *exp, struct agent_expr *ax,
8505796c8dcSSimon Schubert 		 struct axs_value *value)
8515796c8dcSSimon Schubert {
8525796c8dcSSimon Schubert   /* We don't have to generate any code for the usual integral
8535796c8dcSSimon Schubert      conversions, since values are always represented as full-width on
8545796c8dcSSimon Schubert      the stack.  Should we tweak the type?  */
8555796c8dcSSimon Schubert 
8565796c8dcSSimon Schubert   /* Some types require special handling.  */
8575796c8dcSSimon Schubert   switch (TYPE_CODE (value->type))
8585796c8dcSSimon Schubert     {
8595796c8dcSSimon Schubert       /* Functions get converted to a pointer to the function.  */
8605796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
8615796c8dcSSimon Schubert       value->type = lookup_pointer_type (value->type);
8625796c8dcSSimon Schubert       value->kind = axs_rvalue;	/* Should always be true, but just in case.  */
8635796c8dcSSimon Schubert       break;
8645796c8dcSSimon Schubert 
8655796c8dcSSimon Schubert       /* Arrays get converted to a pointer to their first element, and
8665796c8dcSSimon Schubert          are no longer an lvalue.  */
8675796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
8685796c8dcSSimon Schubert       {
8695796c8dcSSimon Schubert 	struct type *elements = TYPE_TARGET_TYPE (value->type);
870cf7f2e2dSJohn Marino 
8715796c8dcSSimon Schubert 	value->type = lookup_pointer_type (elements);
8725796c8dcSSimon Schubert 	value->kind = axs_rvalue;
8735796c8dcSSimon Schubert 	/* We don't need to generate any code; the address of the array
8745796c8dcSSimon Schubert 	   is also the address of its first element.  */
8755796c8dcSSimon Schubert       }
8765796c8dcSSimon Schubert       break;
8775796c8dcSSimon Schubert 
8785796c8dcSSimon Schubert       /* Don't try to convert structures and unions to rvalues.  Let the
8795796c8dcSSimon Schubert          consumer signal an error.  */
8805796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
8815796c8dcSSimon Schubert     case TYPE_CODE_UNION:
8825796c8dcSSimon Schubert       return;
8835796c8dcSSimon Schubert     }
8845796c8dcSSimon Schubert 
8855796c8dcSSimon Schubert   /* If the value is an lvalue, dereference it.  */
8865796c8dcSSimon Schubert   require_rvalue (ax, value);
8875796c8dcSSimon Schubert }
8885796c8dcSSimon Schubert 
8895796c8dcSSimon Schubert 
8905796c8dcSSimon Schubert /* Return non-zero iff the type TYPE1 is considered "wider" than the
8915796c8dcSSimon Schubert    type TYPE2, according to the rules described in gen_usual_arithmetic.  */
8925796c8dcSSimon Schubert static int
type_wider_than(struct type * type1,struct type * type2)8935796c8dcSSimon Schubert type_wider_than (struct type *type1, struct type *type2)
8945796c8dcSSimon Schubert {
8955796c8dcSSimon Schubert   return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
8965796c8dcSSimon Schubert 	  || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
8975796c8dcSSimon Schubert 	      && TYPE_UNSIGNED (type1)
8985796c8dcSSimon Schubert 	      && !TYPE_UNSIGNED (type2)));
8995796c8dcSSimon Schubert }
9005796c8dcSSimon Schubert 
9015796c8dcSSimon Schubert 
9025796c8dcSSimon Schubert /* Return the "wider" of the two types TYPE1 and TYPE2.  */
9035796c8dcSSimon Schubert static struct type *
max_type(struct type * type1,struct type * type2)9045796c8dcSSimon Schubert max_type (struct type *type1, struct type *type2)
9055796c8dcSSimon Schubert {
9065796c8dcSSimon Schubert   return type_wider_than (type1, type2) ? type1 : type2;
9075796c8dcSSimon Schubert }
9085796c8dcSSimon Schubert 
9095796c8dcSSimon Schubert 
9105796c8dcSSimon Schubert /* Generate code to convert a scalar value of type FROM to type TO.  */
9115796c8dcSSimon Schubert static void
gen_conversion(struct agent_expr * ax,struct type * from,struct type * to)9125796c8dcSSimon Schubert gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
9135796c8dcSSimon Schubert {
9145796c8dcSSimon Schubert   /* Perhaps there is a more graceful way to state these rules.  */
9155796c8dcSSimon Schubert 
9165796c8dcSSimon Schubert   /* If we're converting to a narrower type, then we need to clear out
9175796c8dcSSimon Schubert      the upper bits.  */
9185796c8dcSSimon Schubert   if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
9195796c8dcSSimon Schubert     gen_extend (ax, from);
9205796c8dcSSimon Schubert 
9215796c8dcSSimon Schubert   /* If the two values have equal width, but different signednesses,
9225796c8dcSSimon Schubert      then we need to extend.  */
9235796c8dcSSimon Schubert   else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
9245796c8dcSSimon Schubert     {
9255796c8dcSSimon Schubert       if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
9265796c8dcSSimon Schubert 	gen_extend (ax, to);
9275796c8dcSSimon Schubert     }
9285796c8dcSSimon Schubert 
9295796c8dcSSimon Schubert   /* If we're converting to a wider type, and becoming unsigned, then
9305796c8dcSSimon Schubert      we need to zero out any possible sign bits.  */
9315796c8dcSSimon Schubert   else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
9325796c8dcSSimon Schubert     {
9335796c8dcSSimon Schubert       if (TYPE_UNSIGNED (to))
9345796c8dcSSimon Schubert 	gen_extend (ax, to);
9355796c8dcSSimon Schubert     }
9365796c8dcSSimon Schubert }
9375796c8dcSSimon Schubert 
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert /* Return non-zero iff the type FROM will require any bytecodes to be
9405796c8dcSSimon Schubert    emitted to be converted to the type TO.  */
9415796c8dcSSimon Schubert static int
is_nontrivial_conversion(struct type * from,struct type * to)9425796c8dcSSimon Schubert is_nontrivial_conversion (struct type *from, struct type *to)
9435796c8dcSSimon Schubert {
944cf7f2e2dSJohn Marino   struct agent_expr *ax = new_agent_expr (NULL, 0);
9455796c8dcSSimon Schubert   int nontrivial;
9465796c8dcSSimon Schubert 
9475796c8dcSSimon Schubert   /* Actually generate the code, and see if anything came out.  At the
9485796c8dcSSimon Schubert      moment, it would be trivial to replicate the code in
9495796c8dcSSimon Schubert      gen_conversion here, but in the future, when we're supporting
9505796c8dcSSimon Schubert      floating point and the like, it may not be.  Doing things this
9515796c8dcSSimon Schubert      way allows this function to be independent of the logic in
9525796c8dcSSimon Schubert      gen_conversion.  */
9535796c8dcSSimon Schubert   gen_conversion (ax, from, to);
9545796c8dcSSimon Schubert   nontrivial = ax->len > 0;
9555796c8dcSSimon Schubert   free_agent_expr (ax);
9565796c8dcSSimon Schubert   return nontrivial;
9575796c8dcSSimon Schubert }
9585796c8dcSSimon Schubert 
9595796c8dcSSimon Schubert 
9605796c8dcSSimon Schubert /* Generate code to perform the "usual arithmetic conversions" (ANSI C
9615796c8dcSSimon Schubert    6.2.1.5) for the two operands of an arithmetic operator.  This
9625796c8dcSSimon Schubert    effectively finds a "least upper bound" type for the two arguments,
9635796c8dcSSimon Schubert    and promotes each argument to that type.  *VALUE1 and *VALUE2
9645796c8dcSSimon Schubert    describe the values as they are passed in, and as they are left.  */
9655796c8dcSSimon Schubert static void
gen_usual_arithmetic(struct expression * exp,struct agent_expr * ax,struct axs_value * value1,struct axs_value * value2)9665796c8dcSSimon Schubert gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
9675796c8dcSSimon Schubert 		      struct axs_value *value1, struct axs_value *value2)
9685796c8dcSSimon Schubert {
9695796c8dcSSimon Schubert   /* Do the usual binary conversions.  */
9705796c8dcSSimon Schubert   if (TYPE_CODE (value1->type) == TYPE_CODE_INT
9715796c8dcSSimon Schubert       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
9725796c8dcSSimon Schubert     {
9735796c8dcSSimon Schubert       /* The ANSI integral promotions seem to work this way: Order the
9745796c8dcSSimon Schubert          integer types by size, and then by signedness: an n-bit
9755796c8dcSSimon Schubert          unsigned type is considered "wider" than an n-bit signed
9765796c8dcSSimon Schubert          type.  Promote to the "wider" of the two types, and always
9775796c8dcSSimon Schubert          promote at least to int.  */
9785796c8dcSSimon Schubert       struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
9795796c8dcSSimon Schubert 				      max_type (value1->type, value2->type));
9805796c8dcSSimon Schubert 
9815796c8dcSSimon Schubert       /* Deal with value2, on the top of the stack.  */
9825796c8dcSSimon Schubert       gen_conversion (ax, value2->type, target);
9835796c8dcSSimon Schubert 
9845796c8dcSSimon Schubert       /* Deal with value1, not on the top of the stack.  Don't
9855796c8dcSSimon Schubert          generate the `swap' instructions if we're not actually going
9865796c8dcSSimon Schubert          to do anything.  */
9875796c8dcSSimon Schubert       if (is_nontrivial_conversion (value1->type, target))
9885796c8dcSSimon Schubert 	{
9895796c8dcSSimon Schubert 	  ax_simple (ax, aop_swap);
9905796c8dcSSimon Schubert 	  gen_conversion (ax, value1->type, target);
9915796c8dcSSimon Schubert 	  ax_simple (ax, aop_swap);
9925796c8dcSSimon Schubert 	}
9935796c8dcSSimon Schubert 
9945796c8dcSSimon Schubert       value1->type = value2->type = check_typedef (target);
9955796c8dcSSimon Schubert     }
9965796c8dcSSimon Schubert }
9975796c8dcSSimon Schubert 
9985796c8dcSSimon Schubert 
9995796c8dcSSimon Schubert /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
10005796c8dcSSimon Schubert    the value on the top of the stack, as described by VALUE.  Assume
10015796c8dcSSimon Schubert    the value has integral type.  */
10025796c8dcSSimon Schubert static void
gen_integral_promotions(struct expression * exp,struct agent_expr * ax,struct axs_value * value)10035796c8dcSSimon Schubert gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
10045796c8dcSSimon Schubert 			 struct axs_value *value)
10055796c8dcSSimon Schubert {
10065796c8dcSSimon Schubert   const struct builtin_type *builtin = builtin_type (exp->gdbarch);
10075796c8dcSSimon Schubert 
10085796c8dcSSimon Schubert   if (!type_wider_than (value->type, builtin->builtin_int))
10095796c8dcSSimon Schubert     {
10105796c8dcSSimon Schubert       gen_conversion (ax, value->type, builtin->builtin_int);
10115796c8dcSSimon Schubert       value->type = builtin->builtin_int;
10125796c8dcSSimon Schubert     }
10135796c8dcSSimon Schubert   else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
10145796c8dcSSimon Schubert     {
10155796c8dcSSimon Schubert       gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
10165796c8dcSSimon Schubert       value->type = builtin->builtin_unsigned_int;
10175796c8dcSSimon Schubert     }
10185796c8dcSSimon Schubert }
10195796c8dcSSimon Schubert 
10205796c8dcSSimon Schubert 
10215796c8dcSSimon Schubert /* Generate code for a cast to TYPE.  */
10225796c8dcSSimon Schubert static void
gen_cast(struct agent_expr * ax,struct axs_value * value,struct type * type)10235796c8dcSSimon Schubert gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
10245796c8dcSSimon Schubert {
10255796c8dcSSimon Schubert   /* GCC does allow casts to yield lvalues, so this should be fixed
10265796c8dcSSimon Schubert      before merging these changes into the trunk.  */
10275796c8dcSSimon Schubert   require_rvalue (ax, value);
10285796c8dcSSimon Schubert   /* Dereference typedefs.  */
10295796c8dcSSimon Schubert   type = check_typedef (type);
10305796c8dcSSimon Schubert 
10315796c8dcSSimon Schubert   switch (TYPE_CODE (type))
10325796c8dcSSimon Schubert     {
10335796c8dcSSimon Schubert     case TYPE_CODE_PTR:
1034cf7f2e2dSJohn Marino     case TYPE_CODE_REF:
10355796c8dcSSimon Schubert       /* It's implementation-defined, and I'll bet this is what GCC
10365796c8dcSSimon Schubert          does.  */
10375796c8dcSSimon Schubert       break;
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
10405796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
10415796c8dcSSimon Schubert     case TYPE_CODE_UNION:
10425796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
10435796c8dcSSimon Schubert       error (_("Invalid type cast: intended type must be scalar."));
10445796c8dcSSimon Schubert 
10455796c8dcSSimon Schubert     case TYPE_CODE_ENUM:
1046cf7f2e2dSJohn Marino     case TYPE_CODE_BOOL:
10475796c8dcSSimon Schubert       /* We don't have to worry about the size of the value, because
10485796c8dcSSimon Schubert          all our integral values are fully sign-extended, and when
10495796c8dcSSimon Schubert          casting pointers we can do anything we like.  Is there any
10505796c8dcSSimon Schubert          way for us to know what GCC actually does with a cast like
10515796c8dcSSimon Schubert          this?  */
10525796c8dcSSimon Schubert       break;
10535796c8dcSSimon Schubert 
10545796c8dcSSimon Schubert     case TYPE_CODE_INT:
10555796c8dcSSimon Schubert       gen_conversion (ax, value->type, type);
10565796c8dcSSimon Schubert       break;
10575796c8dcSSimon Schubert 
10585796c8dcSSimon Schubert     case TYPE_CODE_VOID:
10595796c8dcSSimon Schubert       /* We could pop the value, and rely on everyone else to check
10605796c8dcSSimon Schubert          the type and notice that this value doesn't occupy a stack
10615796c8dcSSimon Schubert          slot.  But for now, leave the value on the stack, and
10625796c8dcSSimon Schubert          preserve the "value == stack element" assumption.  */
10635796c8dcSSimon Schubert       break;
10645796c8dcSSimon Schubert 
10655796c8dcSSimon Schubert     default:
10665796c8dcSSimon Schubert       error (_("Casts to requested type are not yet implemented."));
10675796c8dcSSimon Schubert     }
10685796c8dcSSimon Schubert 
10695796c8dcSSimon Schubert   value->type = type;
10705796c8dcSSimon Schubert }
10715796c8dcSSimon Schubert 
10725796c8dcSSimon Schubert 
10735796c8dcSSimon Schubert 
10745796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: arithmetic */
10755796c8dcSSimon Schubert 
10765796c8dcSSimon Schubert /* Scale the integer on the top of the stack by the size of the target
10775796c8dcSSimon Schubert    of the pointer type TYPE.  */
10785796c8dcSSimon Schubert static void
gen_scale(struct agent_expr * ax,enum agent_op op,struct type * type)10795796c8dcSSimon Schubert gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
10805796c8dcSSimon Schubert {
10815796c8dcSSimon Schubert   struct type *element = TYPE_TARGET_TYPE (type);
10825796c8dcSSimon Schubert 
10835796c8dcSSimon Schubert   if (TYPE_LENGTH (element) != 1)
10845796c8dcSSimon Schubert     {
10855796c8dcSSimon Schubert       ax_const_l (ax, TYPE_LENGTH (element));
10865796c8dcSSimon Schubert       ax_simple (ax, op);
10875796c8dcSSimon Schubert     }
10885796c8dcSSimon Schubert }
10895796c8dcSSimon Schubert 
10905796c8dcSSimon Schubert 
10915796c8dcSSimon Schubert /* Generate code for pointer arithmetic PTR + INT.  */
10925796c8dcSSimon Schubert static void
gen_ptradd(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2)10935796c8dcSSimon Schubert gen_ptradd (struct agent_expr *ax, struct axs_value *value,
10945796c8dcSSimon Schubert 	    struct axs_value *value1, struct axs_value *value2)
10955796c8dcSSimon Schubert {
1096cf7f2e2dSJohn Marino   gdb_assert (pointer_type (value1->type));
10975796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
10985796c8dcSSimon Schubert 
10995796c8dcSSimon Schubert   gen_scale (ax, aop_mul, value1->type);
11005796c8dcSSimon Schubert   ax_simple (ax, aop_add);
11015796c8dcSSimon Schubert   gen_extend (ax, value1->type);	/* Catch overflow.  */
11025796c8dcSSimon Schubert   value->type = value1->type;
11035796c8dcSSimon Schubert   value->kind = axs_rvalue;
11045796c8dcSSimon Schubert }
11055796c8dcSSimon Schubert 
11065796c8dcSSimon Schubert 
11075796c8dcSSimon Schubert /* Generate code for pointer arithmetic PTR - INT.  */
11085796c8dcSSimon Schubert static void
gen_ptrsub(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2)11095796c8dcSSimon Schubert gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
11105796c8dcSSimon Schubert 	    struct axs_value *value1, struct axs_value *value2)
11115796c8dcSSimon Schubert {
1112cf7f2e2dSJohn Marino   gdb_assert (pointer_type (value1->type));
11135796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
11145796c8dcSSimon Schubert 
11155796c8dcSSimon Schubert   gen_scale (ax, aop_mul, value1->type);
11165796c8dcSSimon Schubert   ax_simple (ax, aop_sub);
11175796c8dcSSimon Schubert   gen_extend (ax, value1->type);	/* Catch overflow.  */
11185796c8dcSSimon Schubert   value->type = value1->type;
11195796c8dcSSimon Schubert   value->kind = axs_rvalue;
11205796c8dcSSimon Schubert }
11215796c8dcSSimon Schubert 
11225796c8dcSSimon Schubert 
11235796c8dcSSimon Schubert /* Generate code for pointer arithmetic PTR - PTR.  */
11245796c8dcSSimon Schubert static void
gen_ptrdiff(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,struct type * result_type)11255796c8dcSSimon Schubert gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
11265796c8dcSSimon Schubert 	     struct axs_value *value1, struct axs_value *value2,
11275796c8dcSSimon Schubert 	     struct type *result_type)
11285796c8dcSSimon Schubert {
1129cf7f2e2dSJohn Marino   gdb_assert (pointer_type (value1->type));
1130cf7f2e2dSJohn Marino   gdb_assert (pointer_type (value2->type));
11315796c8dcSSimon Schubert 
11325796c8dcSSimon Schubert   if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
11335796c8dcSSimon Schubert       != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
11345796c8dcSSimon Schubert     error (_("\
11355796c8dcSSimon Schubert First argument of `-' is a pointer, but second argument is neither\n\
11365796c8dcSSimon Schubert an integer nor a pointer of the same type."));
11375796c8dcSSimon Schubert 
11385796c8dcSSimon Schubert   ax_simple (ax, aop_sub);
11395796c8dcSSimon Schubert   gen_scale (ax, aop_div_unsigned, value1->type);
11405796c8dcSSimon Schubert   value->type = result_type;
11415796c8dcSSimon Schubert   value->kind = axs_rvalue;
11425796c8dcSSimon Schubert }
11435796c8dcSSimon Schubert 
1144cf7f2e2dSJohn Marino static void
gen_equal(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,struct type * result_type)1145cf7f2e2dSJohn Marino gen_equal (struct agent_expr *ax, struct axs_value *value,
1146cf7f2e2dSJohn Marino 	   struct axs_value *value1, struct axs_value *value2,
1147cf7f2e2dSJohn Marino 	   struct type *result_type)
1148cf7f2e2dSJohn Marino {
1149cf7f2e2dSJohn Marino   if (pointer_type (value1->type) || pointer_type (value2->type))
1150cf7f2e2dSJohn Marino     ax_simple (ax, aop_equal);
1151cf7f2e2dSJohn Marino   else
1152cf7f2e2dSJohn Marino     gen_binop (ax, value, value1, value2,
1153cf7f2e2dSJohn Marino 	       aop_equal, aop_equal, 0, "equal");
1154cf7f2e2dSJohn Marino   value->type = result_type;
1155cf7f2e2dSJohn Marino   value->kind = axs_rvalue;
1156cf7f2e2dSJohn Marino }
1157cf7f2e2dSJohn Marino 
1158cf7f2e2dSJohn Marino static void
gen_less(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,struct type * result_type)1159cf7f2e2dSJohn Marino gen_less (struct agent_expr *ax, struct axs_value *value,
1160cf7f2e2dSJohn Marino 	  struct axs_value *value1, struct axs_value *value2,
1161cf7f2e2dSJohn Marino 	  struct type *result_type)
1162cf7f2e2dSJohn Marino {
1163cf7f2e2dSJohn Marino   if (pointer_type (value1->type) || pointer_type (value2->type))
1164cf7f2e2dSJohn Marino     ax_simple (ax, aop_less_unsigned);
1165cf7f2e2dSJohn Marino   else
1166cf7f2e2dSJohn Marino     gen_binop (ax, value, value1, value2,
1167cf7f2e2dSJohn Marino 	       aop_less_signed, aop_less_unsigned, 0, "less than");
1168cf7f2e2dSJohn Marino   value->type = result_type;
1169cf7f2e2dSJohn Marino   value->kind = axs_rvalue;
1170cf7f2e2dSJohn Marino }
11715796c8dcSSimon Schubert 
11725796c8dcSSimon Schubert /* Generate code for a binary operator that doesn't do pointer magic.
11735796c8dcSSimon Schubert    We set VALUE to describe the result value; we assume VALUE1 and
11745796c8dcSSimon Schubert    VALUE2 describe the two operands, and that they've undergone the
11755796c8dcSSimon Schubert    usual binary conversions.  MAY_CARRY should be non-zero iff the
11765796c8dcSSimon Schubert    result needs to be extended.  NAME is the English name of the
11775796c8dcSSimon Schubert    operator, used in error messages */
11785796c8dcSSimon Schubert static void
gen_binop(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,enum agent_op op,enum agent_op op_unsigned,int may_carry,char * name)11795796c8dcSSimon Schubert gen_binop (struct agent_expr *ax, struct axs_value *value,
1180c50c785cSJohn Marino 	   struct axs_value *value1, struct axs_value *value2,
1181c50c785cSJohn Marino 	   enum agent_op op, enum agent_op op_unsigned,
1182c50c785cSJohn Marino 	   int may_carry, char *name)
11835796c8dcSSimon Schubert {
11845796c8dcSSimon Schubert   /* We only handle INT op INT.  */
11855796c8dcSSimon Schubert   if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
11865796c8dcSSimon Schubert       || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
11875796c8dcSSimon Schubert     error (_("Invalid combination of types in %s."), name);
11885796c8dcSSimon Schubert 
11895796c8dcSSimon Schubert   ax_simple (ax,
11905796c8dcSSimon Schubert 	     TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
11915796c8dcSSimon Schubert   if (may_carry)
11925796c8dcSSimon Schubert     gen_extend (ax, value1->type);	/* catch overflow */
11935796c8dcSSimon Schubert   value->type = value1->type;
11945796c8dcSSimon Schubert   value->kind = axs_rvalue;
11955796c8dcSSimon Schubert }
11965796c8dcSSimon Schubert 
11975796c8dcSSimon Schubert 
11985796c8dcSSimon Schubert static void
gen_logical_not(struct agent_expr * ax,struct axs_value * value,struct type * result_type)11995796c8dcSSimon Schubert gen_logical_not (struct agent_expr *ax, struct axs_value *value,
12005796c8dcSSimon Schubert 		 struct type *result_type)
12015796c8dcSSimon Schubert {
12025796c8dcSSimon Schubert   if (TYPE_CODE (value->type) != TYPE_CODE_INT
12035796c8dcSSimon Schubert       && TYPE_CODE (value->type) != TYPE_CODE_PTR)
12045796c8dcSSimon Schubert     error (_("Invalid type of operand to `!'."));
12055796c8dcSSimon Schubert 
12065796c8dcSSimon Schubert   ax_simple (ax, aop_log_not);
12075796c8dcSSimon Schubert   value->type = result_type;
12085796c8dcSSimon Schubert }
12095796c8dcSSimon Schubert 
12105796c8dcSSimon Schubert 
12115796c8dcSSimon Schubert static void
gen_complement(struct agent_expr * ax,struct axs_value * value)12125796c8dcSSimon Schubert gen_complement (struct agent_expr *ax, struct axs_value *value)
12135796c8dcSSimon Schubert {
12145796c8dcSSimon Schubert   if (TYPE_CODE (value->type) != TYPE_CODE_INT)
12155796c8dcSSimon Schubert     error (_("Invalid type of operand to `~'."));
12165796c8dcSSimon Schubert 
12175796c8dcSSimon Schubert   ax_simple (ax, aop_bit_not);
12185796c8dcSSimon Schubert   gen_extend (ax, value->type);
12195796c8dcSSimon Schubert }
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert 
12225796c8dcSSimon Schubert 
12235796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
12245796c8dcSSimon Schubert 
12255796c8dcSSimon Schubert /* Dereference the value on the top of the stack.  */
12265796c8dcSSimon Schubert static void
gen_deref(struct agent_expr * ax,struct axs_value * value)12275796c8dcSSimon Schubert gen_deref (struct agent_expr *ax, struct axs_value *value)
12285796c8dcSSimon Schubert {
12295796c8dcSSimon Schubert   /* The caller should check the type, because several operators use
12305796c8dcSSimon Schubert      this, and we don't know what error message to generate.  */
1231cf7f2e2dSJohn Marino   if (!pointer_type (value->type))
12325796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
12335796c8dcSSimon Schubert 		    _("gen_deref: expected a pointer"));
12345796c8dcSSimon Schubert 
12355796c8dcSSimon Schubert   /* We've got an rvalue now, which is a pointer.  We want to yield an
12365796c8dcSSimon Schubert      lvalue, whose address is exactly that pointer.  So we don't
12375796c8dcSSimon Schubert      actually emit any code; we just change the type from "Pointer to
12385796c8dcSSimon Schubert      T" to "T", and mark the value as an lvalue in memory.  Leave it
12395796c8dcSSimon Schubert      to the consumer to actually dereference it.  */
12405796c8dcSSimon Schubert   value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1241cf7f2e2dSJohn Marino   if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1242cf7f2e2dSJohn Marino     error (_("Attempt to dereference a generic pointer."));
12435796c8dcSSimon Schubert   value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
12445796c8dcSSimon Schubert 		 ? axs_rvalue : axs_lvalue_memory);
12455796c8dcSSimon Schubert }
12465796c8dcSSimon Schubert 
12475796c8dcSSimon Schubert 
12485796c8dcSSimon Schubert /* Produce the address of the lvalue on the top of the stack.  */
12495796c8dcSSimon Schubert static void
gen_address_of(struct agent_expr * ax,struct axs_value * value)12505796c8dcSSimon Schubert gen_address_of (struct agent_expr *ax, struct axs_value *value)
12515796c8dcSSimon Schubert {
12525796c8dcSSimon Schubert   /* Special case for taking the address of a function.  The ANSI
12535796c8dcSSimon Schubert      standard describes this as a special case, too, so this
12545796c8dcSSimon Schubert      arrangement is not without motivation.  */
12555796c8dcSSimon Schubert   if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
12565796c8dcSSimon Schubert     /* The value's already an rvalue on the stack, so we just need to
12575796c8dcSSimon Schubert        change the type.  */
12585796c8dcSSimon Schubert     value->type = lookup_pointer_type (value->type);
12595796c8dcSSimon Schubert   else
12605796c8dcSSimon Schubert     switch (value->kind)
12615796c8dcSSimon Schubert       {
12625796c8dcSSimon Schubert       case axs_rvalue:
12635796c8dcSSimon Schubert 	error (_("Operand of `&' is an rvalue, which has no address."));
12645796c8dcSSimon Schubert 
12655796c8dcSSimon Schubert       case axs_lvalue_register:
12665796c8dcSSimon Schubert 	error (_("Operand of `&' is in a register, and has no address."));
12675796c8dcSSimon Schubert 
12685796c8dcSSimon Schubert       case axs_lvalue_memory:
12695796c8dcSSimon Schubert 	value->kind = axs_rvalue;
12705796c8dcSSimon Schubert 	value->type = lookup_pointer_type (value->type);
12715796c8dcSSimon Schubert 	break;
12725796c8dcSSimon Schubert       }
12735796c8dcSSimon Schubert }
12745796c8dcSSimon Schubert 
12755796c8dcSSimon Schubert /* Generate code to push the value of a bitfield of a structure whose
12765796c8dcSSimon Schubert    address is on the top of the stack.  START and END give the
12775796c8dcSSimon Schubert    starting and one-past-ending *bit* numbers of the field within the
12785796c8dcSSimon Schubert    structure.  */
12795796c8dcSSimon Schubert static void
gen_bitfield_ref(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * type,int start,int end)12805796c8dcSSimon Schubert gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
12815796c8dcSSimon Schubert 		  struct axs_value *value, struct type *type,
12825796c8dcSSimon Schubert 		  int start, int end)
12835796c8dcSSimon Schubert {
12845796c8dcSSimon Schubert   /* Note that ops[i] fetches 8 << i bits.  */
12855796c8dcSSimon Schubert   static enum agent_op ops[]
1286cf7f2e2dSJohn Marino     = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
12875796c8dcSSimon Schubert   static int num_ops = (sizeof (ops) / sizeof (ops[0]));
12885796c8dcSSimon Schubert 
12895796c8dcSSimon Schubert   /* We don't want to touch any byte that the bitfield doesn't
12905796c8dcSSimon Schubert      actually occupy; we shouldn't make any accesses we're not
12915796c8dcSSimon Schubert      explicitly permitted to.  We rely here on the fact that the
12925796c8dcSSimon Schubert      bytecode `ref' operators work on unaligned addresses.
12935796c8dcSSimon Schubert 
12945796c8dcSSimon Schubert      It takes some fancy footwork to get the stack to work the way
12955796c8dcSSimon Schubert      we'd like.  Say we're retrieving a bitfield that requires three
12965796c8dcSSimon Schubert      fetches.  Initially, the stack just contains the address:
12975796c8dcSSimon Schubert      addr
12985796c8dcSSimon Schubert      For the first fetch, we duplicate the address
12995796c8dcSSimon Schubert      addr addr
13005796c8dcSSimon Schubert      then add the byte offset, do the fetch, and shift and mask as
13015796c8dcSSimon Schubert      needed, yielding a fragment of the value, properly aligned for
13025796c8dcSSimon Schubert      the final bitwise or:
13035796c8dcSSimon Schubert      addr frag1
13045796c8dcSSimon Schubert      then we swap, and repeat the process:
13055796c8dcSSimon Schubert      frag1 addr                    --- address on top
13065796c8dcSSimon Schubert      frag1 addr addr               --- duplicate it
13075796c8dcSSimon Schubert      frag1 addr frag2              --- get second fragment
13085796c8dcSSimon Schubert      frag1 frag2 addr              --- swap again
13095796c8dcSSimon Schubert      frag1 frag2 frag3             --- get third fragment
13105796c8dcSSimon Schubert      Notice that, since the third fragment is the last one, we don't
13115796c8dcSSimon Schubert      bother duplicating the address this time.  Now we have all the
13125796c8dcSSimon Schubert      fragments on the stack, and we can simply `or' them together,
13135796c8dcSSimon Schubert      yielding the final value of the bitfield.  */
13145796c8dcSSimon Schubert 
13155796c8dcSSimon Schubert   /* The first and one-after-last bits in the field, but rounded down
13165796c8dcSSimon Schubert      and up to byte boundaries.  */
13175796c8dcSSimon Schubert   int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
13185796c8dcSSimon Schubert   int bound_end = (((end + TARGET_CHAR_BIT - 1)
13195796c8dcSSimon Schubert 		    / TARGET_CHAR_BIT)
13205796c8dcSSimon Schubert 		   * TARGET_CHAR_BIT);
13215796c8dcSSimon Schubert 
13225796c8dcSSimon Schubert   /* current bit offset within the structure */
13235796c8dcSSimon Schubert   int offset;
13245796c8dcSSimon Schubert 
13255796c8dcSSimon Schubert   /* The index in ops of the opcode we're considering.  */
13265796c8dcSSimon Schubert   int op;
13275796c8dcSSimon Schubert 
13285796c8dcSSimon Schubert   /* The number of fragments we generated in the process.  Probably
13295796c8dcSSimon Schubert      equal to the number of `one' bits in bytesize, but who cares?  */
13305796c8dcSSimon Schubert   int fragment_count;
13315796c8dcSSimon Schubert 
13325796c8dcSSimon Schubert   /* Dereference any typedefs.  */
13335796c8dcSSimon Schubert   type = check_typedef (type);
13345796c8dcSSimon Schubert 
13355796c8dcSSimon Schubert   /* Can we fetch the number of bits requested at all?  */
13365796c8dcSSimon Schubert   if ((end - start) > ((1 << num_ops) * 8))
13375796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
13385796c8dcSSimon Schubert 		    _("gen_bitfield_ref: bitfield too wide"));
13395796c8dcSSimon Schubert 
13405796c8dcSSimon Schubert   /* Note that we know here that we only need to try each opcode once.
13415796c8dcSSimon Schubert      That may not be true on machines with weird byte sizes.  */
13425796c8dcSSimon Schubert   offset = bound_start;
13435796c8dcSSimon Schubert   fragment_count = 0;
13445796c8dcSSimon Schubert   for (op = num_ops - 1; op >= 0; op--)
13455796c8dcSSimon Schubert     {
13465796c8dcSSimon Schubert       /* number of bits that ops[op] would fetch */
13475796c8dcSSimon Schubert       int op_size = 8 << op;
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert       /* The stack at this point, from bottom to top, contains zero or
13505796c8dcSSimon Schubert          more fragments, then the address.  */
13515796c8dcSSimon Schubert 
13525796c8dcSSimon Schubert       /* Does this fetch fit within the bitfield?  */
13535796c8dcSSimon Schubert       if (offset + op_size <= bound_end)
13545796c8dcSSimon Schubert 	{
13555796c8dcSSimon Schubert 	  /* Is this the last fragment?  */
13565796c8dcSSimon Schubert 	  int last_frag = (offset + op_size == bound_end);
13575796c8dcSSimon Schubert 
13585796c8dcSSimon Schubert 	  if (!last_frag)
13595796c8dcSSimon Schubert 	    ax_simple (ax, aop_dup);	/* keep a copy of the address */
13605796c8dcSSimon Schubert 
13615796c8dcSSimon Schubert 	  /* Add the offset.  */
13625796c8dcSSimon Schubert 	  gen_offset (ax, offset / TARGET_CHAR_BIT);
13635796c8dcSSimon Schubert 
13645796c8dcSSimon Schubert 	  if (trace_kludge)
13655796c8dcSSimon Schubert 	    {
13665796c8dcSSimon Schubert 	      /* Record the area of memory we're about to fetch.  */
13675796c8dcSSimon Schubert 	      ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
13685796c8dcSSimon Schubert 	    }
13695796c8dcSSimon Schubert 
13705796c8dcSSimon Schubert 	  /* Perform the fetch.  */
13715796c8dcSSimon Schubert 	  ax_simple (ax, ops[op]);
13725796c8dcSSimon Schubert 
13735796c8dcSSimon Schubert 	  /* Shift the bits we have to their proper position.
13745796c8dcSSimon Schubert 	     gen_left_shift will generate right shifts when the operand
13755796c8dcSSimon Schubert 	     is negative.
13765796c8dcSSimon Schubert 
13775796c8dcSSimon Schubert 	     A big-endian field diagram to ponder:
13785796c8dcSSimon Schubert 	     byte 0  byte 1  byte 2  byte 3  byte 4  byte 5  byte 6  byte 7
13795796c8dcSSimon Schubert 	     +------++------++------++------++------++------++------++------+
13805796c8dcSSimon Schubert 	     xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
13815796c8dcSSimon Schubert 	     ^               ^               ^    ^
13825796c8dcSSimon Schubert 	     bit number      16              32              48   53
13835796c8dcSSimon Schubert 	     These are bit numbers as supplied by GDB.  Note that the
13845796c8dcSSimon Schubert 	     bit numbers run from right to left once you've fetched the
13855796c8dcSSimon Schubert 	     value!
13865796c8dcSSimon Schubert 
13875796c8dcSSimon Schubert 	     A little-endian field diagram to ponder:
13885796c8dcSSimon Schubert 	     byte 7  byte 6  byte 5  byte 4  byte 3  byte 2  byte 1  byte 0
13895796c8dcSSimon Schubert 	     +------++------++------++------++------++------++------++------+
13905796c8dcSSimon Schubert 	     xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
13915796c8dcSSimon Schubert 	     ^               ^               ^           ^   ^
13925796c8dcSSimon Schubert 	     bit number     48              32              16          4   0
13935796c8dcSSimon Schubert 
13945796c8dcSSimon Schubert 	     In both cases, the most significant end is on the left
13955796c8dcSSimon Schubert 	     (i.e. normal numeric writing order), which means that you
13965796c8dcSSimon Schubert 	     don't go crazy thinking about `left' and `right' shifts.
13975796c8dcSSimon Schubert 
13985796c8dcSSimon Schubert 	     We don't have to worry about masking yet:
13995796c8dcSSimon Schubert 	     - If they contain garbage off the least significant end, then we
14005796c8dcSSimon Schubert 	     must be looking at the low end of the field, and the right
14015796c8dcSSimon Schubert 	     shift will wipe them out.
14025796c8dcSSimon Schubert 	     - If they contain garbage off the most significant end, then we
14035796c8dcSSimon Schubert 	     must be looking at the most significant end of the word, and
14045796c8dcSSimon Schubert 	     the sign/zero extension will wipe them out.
14055796c8dcSSimon Schubert 	     - If we're in the interior of the word, then there is no garbage
14065796c8dcSSimon Schubert 	     on either end, because the ref operators zero-extend.  */
14075796c8dcSSimon Schubert 	  if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
14085796c8dcSSimon Schubert 	    gen_left_shift (ax, end - (offset + op_size));
14095796c8dcSSimon Schubert 	  else
14105796c8dcSSimon Schubert 	    gen_left_shift (ax, offset - start);
14115796c8dcSSimon Schubert 
14125796c8dcSSimon Schubert 	  if (!last_frag)
14135796c8dcSSimon Schubert 	    /* Bring the copy of the address up to the top.  */
14145796c8dcSSimon Schubert 	    ax_simple (ax, aop_swap);
14155796c8dcSSimon Schubert 
14165796c8dcSSimon Schubert 	  offset += op_size;
14175796c8dcSSimon Schubert 	  fragment_count++;
14185796c8dcSSimon Schubert 	}
14195796c8dcSSimon Schubert     }
14205796c8dcSSimon Schubert 
14215796c8dcSSimon Schubert   /* Generate enough bitwise `or' operations to combine all the
14225796c8dcSSimon Schubert      fragments we left on the stack.  */
14235796c8dcSSimon Schubert   while (fragment_count-- > 1)
14245796c8dcSSimon Schubert     ax_simple (ax, aop_bit_or);
14255796c8dcSSimon Schubert 
14265796c8dcSSimon Schubert   /* Sign- or zero-extend the value as appropriate.  */
14275796c8dcSSimon Schubert   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
14285796c8dcSSimon Schubert 
14295796c8dcSSimon Schubert   /* This is *not* an lvalue.  Ugh.  */
14305796c8dcSSimon Schubert   value->kind = axs_rvalue;
14315796c8dcSSimon Schubert   value->type = type;
14325796c8dcSSimon Schubert }
14335796c8dcSSimon Schubert 
1434cf7f2e2dSJohn Marino /* Generate bytecodes for field number FIELDNO of type TYPE.  OFFSET
1435cf7f2e2dSJohn Marino    is an accumulated offset (in bytes), will be nonzero for objects
1436cf7f2e2dSJohn Marino    embedded in other objects, like C++ base classes.  Behavior should
1437cf7f2e2dSJohn Marino    generally follow value_primitive_field.  */
1438cf7f2e2dSJohn Marino 
1439cf7f2e2dSJohn Marino static void
gen_primitive_field(struct expression * exp,struct agent_expr * ax,struct axs_value * value,int offset,int fieldno,struct type * type)1440cf7f2e2dSJohn Marino gen_primitive_field (struct expression *exp,
1441cf7f2e2dSJohn Marino 		     struct agent_expr *ax, struct axs_value *value,
1442cf7f2e2dSJohn Marino 		     int offset, int fieldno, struct type *type)
1443cf7f2e2dSJohn Marino {
1444cf7f2e2dSJohn Marino   /* Is this a bitfield?  */
1445cf7f2e2dSJohn Marino   if (TYPE_FIELD_PACKED (type, fieldno))
1446cf7f2e2dSJohn Marino     gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1447cf7f2e2dSJohn Marino 		      (offset * TARGET_CHAR_BIT
1448cf7f2e2dSJohn Marino 		       + TYPE_FIELD_BITPOS (type, fieldno)),
1449cf7f2e2dSJohn Marino 		      (offset * TARGET_CHAR_BIT
1450cf7f2e2dSJohn Marino 		       + TYPE_FIELD_BITPOS (type, fieldno)
1451cf7f2e2dSJohn Marino 		       + TYPE_FIELD_BITSIZE (type, fieldno)));
1452cf7f2e2dSJohn Marino   else
1453cf7f2e2dSJohn Marino     {
1454cf7f2e2dSJohn Marino       gen_offset (ax, offset
1455cf7f2e2dSJohn Marino 		  + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1456cf7f2e2dSJohn Marino       value->kind = axs_lvalue_memory;
1457cf7f2e2dSJohn Marino       value->type = TYPE_FIELD_TYPE (type, fieldno);
1458cf7f2e2dSJohn Marino     }
1459cf7f2e2dSJohn Marino }
1460cf7f2e2dSJohn Marino 
1461cf7f2e2dSJohn Marino /* Search for the given field in either the given type or one of its
1462cf7f2e2dSJohn Marino    base classes.  Return 1 if found, 0 if not.  */
1463cf7f2e2dSJohn Marino 
1464cf7f2e2dSJohn Marino static int
gen_struct_ref_recursive(struct expression * exp,struct agent_expr * ax,struct axs_value * value,char * field,int offset,struct type * type)1465cf7f2e2dSJohn Marino gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1466cf7f2e2dSJohn Marino 			  struct axs_value *value,
1467cf7f2e2dSJohn Marino 			  char *field, int offset, struct type *type)
1468cf7f2e2dSJohn Marino {
1469cf7f2e2dSJohn Marino   int i, rslt;
1470cf7f2e2dSJohn Marino   int nbases = TYPE_N_BASECLASSES (type);
1471cf7f2e2dSJohn Marino 
1472cf7f2e2dSJohn Marino   CHECK_TYPEDEF (type);
1473cf7f2e2dSJohn Marino 
1474cf7f2e2dSJohn Marino   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1475cf7f2e2dSJohn Marino     {
1476*ef5ccd6cSJohn Marino       const char *this_name = TYPE_FIELD_NAME (type, i);
1477cf7f2e2dSJohn Marino 
1478cf7f2e2dSJohn Marino       if (this_name)
1479cf7f2e2dSJohn Marino 	{
1480cf7f2e2dSJohn Marino 	  if (strcmp (field, this_name) == 0)
1481cf7f2e2dSJohn Marino 	    {
1482cf7f2e2dSJohn Marino 	      /* Note that bytecodes for the struct's base (aka
1483cf7f2e2dSJohn Marino 		 "this") will have been generated already, which will
1484cf7f2e2dSJohn Marino 		 be unnecessary but not harmful if the static field is
1485cf7f2e2dSJohn Marino 		 being handled as a global.  */
1486cf7f2e2dSJohn Marino 	      if (field_is_static (&TYPE_FIELD (type, i)))
1487cf7f2e2dSJohn Marino 		{
1488cf7f2e2dSJohn Marino 		  gen_static_field (exp->gdbarch, ax, value, type, i);
1489cf7f2e2dSJohn Marino 		  if (value->optimized_out)
1490c50c785cSJohn Marino 		    error (_("static field `%s' has been "
1491c50c785cSJohn Marino 			     "optimized out, cannot use"),
1492cf7f2e2dSJohn Marino 			   field);
1493cf7f2e2dSJohn Marino 		  return 1;
1494cf7f2e2dSJohn Marino 		}
1495cf7f2e2dSJohn Marino 
1496cf7f2e2dSJohn Marino 	      gen_primitive_field (exp, ax, value, offset, i, type);
1497cf7f2e2dSJohn Marino 	      return 1;
1498cf7f2e2dSJohn Marino 	    }
1499cf7f2e2dSJohn Marino #if 0 /* is this right? */
1500cf7f2e2dSJohn Marino 	  if (this_name[0] == '\0')
1501cf7f2e2dSJohn Marino 	    internal_error (__FILE__, __LINE__,
1502cf7f2e2dSJohn Marino 			    _("find_field: anonymous unions not supported"));
1503cf7f2e2dSJohn Marino #endif
1504cf7f2e2dSJohn Marino 	}
1505cf7f2e2dSJohn Marino     }
1506cf7f2e2dSJohn Marino 
1507cf7f2e2dSJohn Marino   /* Now scan through base classes recursively.  */
1508cf7f2e2dSJohn Marino   for (i = 0; i < nbases; i++)
1509cf7f2e2dSJohn Marino     {
1510cf7f2e2dSJohn Marino       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1511cf7f2e2dSJohn Marino 
1512cf7f2e2dSJohn Marino       rslt = gen_struct_ref_recursive (exp, ax, value, field,
1513c50c785cSJohn Marino 				       offset + TYPE_BASECLASS_BITPOS (type, i)
1514c50c785cSJohn Marino 				       / TARGET_CHAR_BIT,
1515cf7f2e2dSJohn Marino 				       basetype);
1516cf7f2e2dSJohn Marino       if (rslt)
1517cf7f2e2dSJohn Marino 	return 1;
1518cf7f2e2dSJohn Marino     }
1519cf7f2e2dSJohn Marino 
1520cf7f2e2dSJohn Marino   /* Not found anywhere, flag so caller can complain.  */
1521cf7f2e2dSJohn Marino   return 0;
1522cf7f2e2dSJohn Marino }
15235796c8dcSSimon Schubert 
15245796c8dcSSimon Schubert /* Generate code to reference the member named FIELD of a structure or
15255796c8dcSSimon Schubert    union.  The top of the stack, as described by VALUE, should have
15265796c8dcSSimon Schubert    type (pointer to a)* struct/union.  OPERATOR_NAME is the name of
15275796c8dcSSimon Schubert    the operator being compiled, and OPERAND_NAME is the kind of thing
15285796c8dcSSimon Schubert    it operates on; we use them in error messages.  */
15295796c8dcSSimon Schubert static void
gen_struct_ref(struct expression * exp,struct agent_expr * ax,struct axs_value * value,char * field,char * operator_name,char * operand_name)15305796c8dcSSimon Schubert gen_struct_ref (struct expression *exp, struct agent_expr *ax,
15315796c8dcSSimon Schubert 		struct axs_value *value, char *field,
15325796c8dcSSimon Schubert 		char *operator_name, char *operand_name)
15335796c8dcSSimon Schubert {
15345796c8dcSSimon Schubert   struct type *type;
1535cf7f2e2dSJohn Marino   int found;
15365796c8dcSSimon Schubert 
15375796c8dcSSimon Schubert   /* Follow pointers until we reach a non-pointer.  These aren't the C
15385796c8dcSSimon Schubert      semantics, but they're what the normal GDB evaluator does, so we
15395796c8dcSSimon Schubert      should at least be consistent.  */
1540cf7f2e2dSJohn Marino   while (pointer_type (value->type))
15415796c8dcSSimon Schubert     {
15425796c8dcSSimon Schubert       require_rvalue (ax, value);
15435796c8dcSSimon Schubert       gen_deref (ax, value);
15445796c8dcSSimon Schubert     }
15455796c8dcSSimon Schubert   type = check_typedef (value->type);
15465796c8dcSSimon Schubert 
15475796c8dcSSimon Schubert   /* This must yield a structure or a union.  */
15485796c8dcSSimon Schubert   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
15495796c8dcSSimon Schubert       && TYPE_CODE (type) != TYPE_CODE_UNION)
15505796c8dcSSimon Schubert     error (_("The left operand of `%s' is not a %s."),
15515796c8dcSSimon Schubert 	   operator_name, operand_name);
15525796c8dcSSimon Schubert 
15535796c8dcSSimon Schubert   /* And it must be in memory; we don't deal with structure rvalues,
15545796c8dcSSimon Schubert      or structures living in registers.  */
15555796c8dcSSimon Schubert   if (value->kind != axs_lvalue_memory)
15565796c8dcSSimon Schubert     error (_("Structure does not live in memory."));
15575796c8dcSSimon Schubert 
1558cf7f2e2dSJohn Marino   /* Search through fields and base classes recursively.  */
1559cf7f2e2dSJohn Marino   found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
15605796c8dcSSimon Schubert 
1561cf7f2e2dSJohn Marino   if (!found)
1562cf7f2e2dSJohn Marino     error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1563cf7f2e2dSJohn Marino 	   field, TYPE_TAG_NAME (type));
1564cf7f2e2dSJohn Marino }
1565cf7f2e2dSJohn Marino 
1566cf7f2e2dSJohn Marino static int
1567cf7f2e2dSJohn Marino gen_namespace_elt (struct expression *exp,
1568cf7f2e2dSJohn Marino 		   struct agent_expr *ax, struct axs_value *value,
1569cf7f2e2dSJohn Marino 		   const struct type *curtype, char *name);
1570cf7f2e2dSJohn Marino static int
1571cf7f2e2dSJohn Marino gen_maybe_namespace_elt (struct expression *exp,
1572cf7f2e2dSJohn Marino 			 struct agent_expr *ax, struct axs_value *value,
1573cf7f2e2dSJohn Marino 			 const struct type *curtype, char *name);
1574cf7f2e2dSJohn Marino 
1575cf7f2e2dSJohn Marino static void
gen_static_field(struct gdbarch * gdbarch,struct agent_expr * ax,struct axs_value * value,struct type * type,int fieldno)1576cf7f2e2dSJohn Marino gen_static_field (struct gdbarch *gdbarch,
1577cf7f2e2dSJohn Marino 		  struct agent_expr *ax, struct axs_value *value,
1578cf7f2e2dSJohn Marino 		  struct type *type, int fieldno)
1579cf7f2e2dSJohn Marino {
1580cf7f2e2dSJohn Marino   if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1581cf7f2e2dSJohn Marino     {
1582cf7f2e2dSJohn Marino       ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1583cf7f2e2dSJohn Marino       value->kind = axs_lvalue_memory;
1584cf7f2e2dSJohn Marino       value->type = TYPE_FIELD_TYPE (type, fieldno);
1585cf7f2e2dSJohn Marino       value->optimized_out = 0;
1586cf7f2e2dSJohn Marino     }
15875796c8dcSSimon Schubert   else
15885796c8dcSSimon Schubert     {
1589a45ae5f8SJohn Marino       const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1590cf7f2e2dSJohn Marino       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
1591cf7f2e2dSJohn Marino 
1592cf7f2e2dSJohn Marino       if (sym)
1593cf7f2e2dSJohn Marino 	{
1594cf7f2e2dSJohn Marino 	  gen_var_ref (gdbarch, ax, value, sym);
1595cf7f2e2dSJohn Marino 
1596cf7f2e2dSJohn Marino 	  /* Don't error if the value was optimized out, we may be
1597cf7f2e2dSJohn Marino 	     scanning all static fields and just want to pass over this
1598cf7f2e2dSJohn Marino 	     and continue with the rest.  */
1599cf7f2e2dSJohn Marino 	}
1600cf7f2e2dSJohn Marino       else
1601cf7f2e2dSJohn Marino 	{
1602cf7f2e2dSJohn Marino 	  /* Silently assume this was optimized out; class printing
1603cf7f2e2dSJohn Marino 	     will let the user know why the data is missing.  */
1604cf7f2e2dSJohn Marino 	  value->optimized_out = 1;
1605cf7f2e2dSJohn Marino 	}
16065796c8dcSSimon Schubert     }
16075796c8dcSSimon Schubert }
16085796c8dcSSimon Schubert 
1609cf7f2e2dSJohn Marino static int
gen_struct_elt_for_reference(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * type,char * fieldname)1610cf7f2e2dSJohn Marino gen_struct_elt_for_reference (struct expression *exp,
1611cf7f2e2dSJohn Marino 			      struct agent_expr *ax, struct axs_value *value,
1612cf7f2e2dSJohn Marino 			      struct type *type, char *fieldname)
1613cf7f2e2dSJohn Marino {
1614cf7f2e2dSJohn Marino   struct type *t = type;
1615cf7f2e2dSJohn Marino   int i;
1616cf7f2e2dSJohn Marino 
1617cf7f2e2dSJohn Marino   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1618cf7f2e2dSJohn Marino       && TYPE_CODE (t) != TYPE_CODE_UNION)
1619cf7f2e2dSJohn Marino     internal_error (__FILE__, __LINE__,
1620cf7f2e2dSJohn Marino 		    _("non-aggregate type to gen_struct_elt_for_reference"));
1621cf7f2e2dSJohn Marino 
1622cf7f2e2dSJohn Marino   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1623cf7f2e2dSJohn Marino     {
1624*ef5ccd6cSJohn Marino       const char *t_field_name = TYPE_FIELD_NAME (t, i);
1625cf7f2e2dSJohn Marino 
1626cf7f2e2dSJohn Marino       if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1627cf7f2e2dSJohn Marino 	{
1628cf7f2e2dSJohn Marino 	  if (field_is_static (&TYPE_FIELD (t, i)))
1629cf7f2e2dSJohn Marino 	    {
1630cf7f2e2dSJohn Marino 	      gen_static_field (exp->gdbarch, ax, value, t, i);
1631cf7f2e2dSJohn Marino 	      if (value->optimized_out)
1632c50c785cSJohn Marino 		error (_("static field `%s' has been "
1633c50c785cSJohn Marino 			 "optimized out, cannot use"),
1634cf7f2e2dSJohn Marino 		       fieldname);
1635cf7f2e2dSJohn Marino 	      return 1;
1636cf7f2e2dSJohn Marino 	    }
1637cf7f2e2dSJohn Marino 	  if (TYPE_FIELD_PACKED (t, i))
1638cf7f2e2dSJohn Marino 	    error (_("pointers to bitfield members not allowed"));
1639cf7f2e2dSJohn Marino 
1640cf7f2e2dSJohn Marino 	  /* FIXME we need a way to do "want_address" equivalent */
1641cf7f2e2dSJohn Marino 
1642cf7f2e2dSJohn Marino 	  error (_("Cannot reference non-static field \"%s\""), fieldname);
1643cf7f2e2dSJohn Marino 	}
1644cf7f2e2dSJohn Marino     }
1645cf7f2e2dSJohn Marino 
1646cf7f2e2dSJohn Marino   /* FIXME add other scoped-reference cases here */
1647cf7f2e2dSJohn Marino 
1648cf7f2e2dSJohn Marino   /* Do a last-ditch lookup.  */
1649cf7f2e2dSJohn Marino   return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1650cf7f2e2dSJohn Marino }
1651cf7f2e2dSJohn Marino 
1652cf7f2e2dSJohn Marino /* C++: Return the member NAME of the namespace given by the type
1653cf7f2e2dSJohn Marino    CURTYPE.  */
1654cf7f2e2dSJohn Marino 
1655cf7f2e2dSJohn Marino static int
gen_namespace_elt(struct expression * exp,struct agent_expr * ax,struct axs_value * value,const struct type * curtype,char * name)1656cf7f2e2dSJohn Marino gen_namespace_elt (struct expression *exp,
1657cf7f2e2dSJohn Marino 		   struct agent_expr *ax, struct axs_value *value,
1658cf7f2e2dSJohn Marino 		   const struct type *curtype, char *name)
1659cf7f2e2dSJohn Marino {
1660cf7f2e2dSJohn Marino   int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1661cf7f2e2dSJohn Marino 
1662cf7f2e2dSJohn Marino   if (!found)
1663cf7f2e2dSJohn Marino     error (_("No symbol \"%s\" in namespace \"%s\"."),
1664cf7f2e2dSJohn Marino 	   name, TYPE_TAG_NAME (curtype));
1665cf7f2e2dSJohn Marino 
1666cf7f2e2dSJohn Marino   return found;
1667cf7f2e2dSJohn Marino }
1668cf7f2e2dSJohn Marino 
1669cf7f2e2dSJohn Marino /* A helper function used by value_namespace_elt and
1670cf7f2e2dSJohn Marino    value_struct_elt_for_reference.  It looks up NAME inside the
1671cf7f2e2dSJohn Marino    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1672cf7f2e2dSJohn Marino    is a class and NAME refers to a type in CURTYPE itself (as opposed
1673cf7f2e2dSJohn Marino    to, say, some base class of CURTYPE).  */
1674cf7f2e2dSJohn Marino 
1675cf7f2e2dSJohn Marino static int
gen_maybe_namespace_elt(struct expression * exp,struct agent_expr * ax,struct axs_value * value,const struct type * curtype,char * name)1676cf7f2e2dSJohn Marino gen_maybe_namespace_elt (struct expression *exp,
1677cf7f2e2dSJohn Marino 			 struct agent_expr *ax, struct axs_value *value,
1678cf7f2e2dSJohn Marino 			 const struct type *curtype, char *name)
1679cf7f2e2dSJohn Marino {
1680cf7f2e2dSJohn Marino   const char *namespace_name = TYPE_TAG_NAME (curtype);
1681cf7f2e2dSJohn Marino   struct symbol *sym;
1682cf7f2e2dSJohn Marino 
1683cf7f2e2dSJohn Marino   sym = cp_lookup_symbol_namespace (namespace_name, name,
1684cf7f2e2dSJohn Marino 				    block_for_pc (ax->scope),
1685cf7f2e2dSJohn Marino 				    VAR_DOMAIN);
1686cf7f2e2dSJohn Marino 
1687cf7f2e2dSJohn Marino   if (sym == NULL)
1688cf7f2e2dSJohn Marino     return 0;
1689cf7f2e2dSJohn Marino 
1690cf7f2e2dSJohn Marino   gen_var_ref (exp->gdbarch, ax, value, sym);
1691cf7f2e2dSJohn Marino 
1692cf7f2e2dSJohn Marino   if (value->optimized_out)
1693cf7f2e2dSJohn Marino     error (_("`%s' has been optimized out, cannot use"),
1694cf7f2e2dSJohn Marino 	   SYMBOL_PRINT_NAME (sym));
1695cf7f2e2dSJohn Marino 
1696cf7f2e2dSJohn Marino   return 1;
1697cf7f2e2dSJohn Marino }
1698cf7f2e2dSJohn Marino 
1699cf7f2e2dSJohn Marino 
1700cf7f2e2dSJohn Marino static int
gen_aggregate_elt_ref(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * type,char * field,char * operator_name,char * operand_name)1701cf7f2e2dSJohn Marino gen_aggregate_elt_ref (struct expression *exp,
1702cf7f2e2dSJohn Marino 		       struct agent_expr *ax, struct axs_value *value,
1703cf7f2e2dSJohn Marino 		       struct type *type, char *field,
1704cf7f2e2dSJohn Marino 		       char *operator_name, char *operand_name)
1705cf7f2e2dSJohn Marino {
1706cf7f2e2dSJohn Marino   switch (TYPE_CODE (type))
1707cf7f2e2dSJohn Marino     {
1708cf7f2e2dSJohn Marino     case TYPE_CODE_STRUCT:
1709cf7f2e2dSJohn Marino     case TYPE_CODE_UNION:
1710cf7f2e2dSJohn Marino       return gen_struct_elt_for_reference (exp, ax, value, type, field);
1711cf7f2e2dSJohn Marino       break;
1712cf7f2e2dSJohn Marino     case TYPE_CODE_NAMESPACE:
1713cf7f2e2dSJohn Marino       return gen_namespace_elt (exp, ax, value, type, field);
1714cf7f2e2dSJohn Marino       break;
1715cf7f2e2dSJohn Marino     default:
1716cf7f2e2dSJohn Marino       internal_error (__FILE__, __LINE__,
1717cf7f2e2dSJohn Marino 		      _("non-aggregate type in gen_aggregate_elt_ref"));
1718cf7f2e2dSJohn Marino     }
1719cf7f2e2dSJohn Marino 
1720cf7f2e2dSJohn Marino   return 0;
1721cf7f2e2dSJohn Marino }
17225796c8dcSSimon Schubert 
17235796c8dcSSimon Schubert /* Generate code for GDB's magical `repeat' operator.
17245796c8dcSSimon Schubert    LVALUE @ INT creates an array INT elements long, and whose elements
17255796c8dcSSimon Schubert    have the same type as LVALUE, located in memory so that LVALUE is
17265796c8dcSSimon Schubert    its first element.  For example, argv[0]@argc gives you the array
17275796c8dcSSimon Schubert    of command-line arguments.
17285796c8dcSSimon Schubert 
17295796c8dcSSimon Schubert    Unfortunately, because we have to know the types before we actually
17305796c8dcSSimon Schubert    have a value for the expression, we can't implement this perfectly
17315796c8dcSSimon Schubert    without changing the type system, having values that occupy two
17325796c8dcSSimon Schubert    stack slots, doing weird things with sizeof, etc.  So we require
17335796c8dcSSimon Schubert    the right operand to be a constant expression.  */
17345796c8dcSSimon Schubert static void
gen_repeat(struct expression * exp,union exp_element ** pc,struct agent_expr * ax,struct axs_value * value)17355796c8dcSSimon Schubert gen_repeat (struct expression *exp, union exp_element **pc,
17365796c8dcSSimon Schubert 	    struct agent_expr *ax, struct axs_value *value)
17375796c8dcSSimon Schubert {
17385796c8dcSSimon Schubert   struct axs_value value1;
1739cf7f2e2dSJohn Marino 
17405796c8dcSSimon Schubert   /* We don't want to turn this into an rvalue, so no conversions
17415796c8dcSSimon Schubert      here.  */
17425796c8dcSSimon Schubert   gen_expr (exp, pc, ax, &value1);
17435796c8dcSSimon Schubert   if (value1.kind != axs_lvalue_memory)
17445796c8dcSSimon Schubert     error (_("Left operand of `@' must be an object in memory."));
17455796c8dcSSimon Schubert 
17465796c8dcSSimon Schubert   /* Evaluate the length; it had better be a constant.  */
17475796c8dcSSimon Schubert   {
17485796c8dcSSimon Schubert     struct value *v = const_expr (pc);
17495796c8dcSSimon Schubert     int length;
17505796c8dcSSimon Schubert 
17515796c8dcSSimon Schubert     if (!v)
1752c50c785cSJohn Marino       error (_("Right operand of `@' must be a "
1753c50c785cSJohn Marino 	       "constant, in agent expressions."));
17545796c8dcSSimon Schubert     if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
17555796c8dcSSimon Schubert       error (_("Right operand of `@' must be an integer."));
17565796c8dcSSimon Schubert     length = value_as_long (v);
17575796c8dcSSimon Schubert     if (length <= 0)
17585796c8dcSSimon Schubert       error (_("Right operand of `@' must be positive."));
17595796c8dcSSimon Schubert 
17605796c8dcSSimon Schubert     /* The top of the stack is already the address of the object, so
17615796c8dcSSimon Schubert        all we need to do is frob the type of the lvalue.  */
17625796c8dcSSimon Schubert     {
17635796c8dcSSimon Schubert       /* FIXME-type-allocation: need a way to free this type when we are
17645796c8dcSSimon Schubert          done with it.  */
17655796c8dcSSimon Schubert       struct type *array
17665796c8dcSSimon Schubert 	= lookup_array_range_type (value1.type, 0, length - 1);
17675796c8dcSSimon Schubert 
17685796c8dcSSimon Schubert       value->kind = axs_lvalue_memory;
17695796c8dcSSimon Schubert       value->type = array;
17705796c8dcSSimon Schubert     }
17715796c8dcSSimon Schubert   }
17725796c8dcSSimon Schubert }
17735796c8dcSSimon Schubert 
17745796c8dcSSimon Schubert 
17755796c8dcSSimon Schubert /* Emit code for the `sizeof' operator.
17765796c8dcSSimon Schubert    *PC should point at the start of the operand expression; we advance it
17775796c8dcSSimon Schubert    to the first instruction after the operand.  */
17785796c8dcSSimon Schubert static void
gen_sizeof(struct expression * exp,union exp_element ** pc,struct agent_expr * ax,struct axs_value * value,struct type * size_type)17795796c8dcSSimon Schubert gen_sizeof (struct expression *exp, union exp_element **pc,
17805796c8dcSSimon Schubert 	    struct agent_expr *ax, struct axs_value *value,
17815796c8dcSSimon Schubert 	    struct type *size_type)
17825796c8dcSSimon Schubert {
17835796c8dcSSimon Schubert   /* We don't care about the value of the operand expression; we only
17845796c8dcSSimon Schubert      care about its type.  However, in the current arrangement, the
17855796c8dcSSimon Schubert      only way to find an expression's type is to generate code for it.
17865796c8dcSSimon Schubert      So we generate code for the operand, and then throw it away,
17875796c8dcSSimon Schubert      replacing it with code that simply pushes its size.  */
17885796c8dcSSimon Schubert   int start = ax->len;
1789cf7f2e2dSJohn Marino 
17905796c8dcSSimon Schubert   gen_expr (exp, pc, ax, value);
17915796c8dcSSimon Schubert 
17925796c8dcSSimon Schubert   /* Throw away the code we just generated.  */
17935796c8dcSSimon Schubert   ax->len = start;
17945796c8dcSSimon Schubert 
17955796c8dcSSimon Schubert   ax_const_l (ax, TYPE_LENGTH (value->type));
17965796c8dcSSimon Schubert   value->kind = axs_rvalue;
17975796c8dcSSimon Schubert   value->type = size_type;
17985796c8dcSSimon Schubert }
17995796c8dcSSimon Schubert 
18005796c8dcSSimon Schubert 
18015796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: general recursive thingy  */
18025796c8dcSSimon Schubert 
18035796c8dcSSimon Schubert /* XXX: i18n */
18045796c8dcSSimon Schubert /* A gen_expr function written by a Gen-X'er guy.
18055796c8dcSSimon Schubert    Append code for the subexpression of EXPR starting at *POS_P to AX.  */
1806*ef5ccd6cSJohn Marino void
gen_expr(struct expression * exp,union exp_element ** pc,struct agent_expr * ax,struct axs_value * value)18075796c8dcSSimon Schubert gen_expr (struct expression *exp, union exp_element **pc,
18085796c8dcSSimon Schubert 	  struct agent_expr *ax, struct axs_value *value)
18095796c8dcSSimon Schubert {
18105796c8dcSSimon Schubert   /* Used to hold the descriptions of operand expressions.  */
1811cf7f2e2dSJohn Marino   struct axs_value value1, value2, value3;
1812cf7f2e2dSJohn Marino   enum exp_opcode op = (*pc)[0].opcode, op2;
1813cf7f2e2dSJohn Marino   int if1, go1, if2, go2, end;
1814cf7f2e2dSJohn Marino   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
18155796c8dcSSimon Schubert 
18165796c8dcSSimon Schubert   /* If we're looking at a constant expression, just push its value.  */
18175796c8dcSSimon Schubert   {
18185796c8dcSSimon Schubert     struct value *v = maybe_const_expr (pc);
18195796c8dcSSimon Schubert 
18205796c8dcSSimon Schubert     if (v)
18215796c8dcSSimon Schubert       {
18225796c8dcSSimon Schubert 	ax_const_l (ax, value_as_long (v));
18235796c8dcSSimon Schubert 	value->kind = axs_rvalue;
18245796c8dcSSimon Schubert 	value->type = check_typedef (value_type (v));
18255796c8dcSSimon Schubert 	return;
18265796c8dcSSimon Schubert       }
18275796c8dcSSimon Schubert   }
18285796c8dcSSimon Schubert 
18295796c8dcSSimon Schubert   /* Otherwise, go ahead and generate code for it.  */
18305796c8dcSSimon Schubert   switch (op)
18315796c8dcSSimon Schubert     {
18325796c8dcSSimon Schubert       /* Binary arithmetic operators.  */
18335796c8dcSSimon Schubert     case BINOP_ADD:
18345796c8dcSSimon Schubert     case BINOP_SUB:
18355796c8dcSSimon Schubert     case BINOP_MUL:
18365796c8dcSSimon Schubert     case BINOP_DIV:
18375796c8dcSSimon Schubert     case BINOP_REM:
1838cf7f2e2dSJohn Marino     case BINOP_LSH:
1839cf7f2e2dSJohn Marino     case BINOP_RSH:
18405796c8dcSSimon Schubert     case BINOP_SUBSCRIPT:
18415796c8dcSSimon Schubert     case BINOP_BITWISE_AND:
18425796c8dcSSimon Schubert     case BINOP_BITWISE_IOR:
18435796c8dcSSimon Schubert     case BINOP_BITWISE_XOR:
18445796c8dcSSimon Schubert     case BINOP_EQUAL:
18455796c8dcSSimon Schubert     case BINOP_NOTEQUAL:
18465796c8dcSSimon Schubert     case BINOP_LESS:
18475796c8dcSSimon Schubert     case BINOP_GTR:
18485796c8dcSSimon Schubert     case BINOP_LEQ:
18495796c8dcSSimon Schubert     case BINOP_GEQ:
18505796c8dcSSimon Schubert       (*pc)++;
18515796c8dcSSimon Schubert       gen_expr (exp, pc, ax, &value1);
18525796c8dcSSimon Schubert       gen_usual_unary (exp, ax, &value1);
1853cf7f2e2dSJohn Marino       gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1854cf7f2e2dSJohn Marino       break;
1855cf7f2e2dSJohn Marino 
1856cf7f2e2dSJohn Marino     case BINOP_LOGICAL_AND:
1857cf7f2e2dSJohn Marino       (*pc)++;
1858cf7f2e2dSJohn Marino       /* Generate the obvious sequence of tests and jumps.  */
1859cf7f2e2dSJohn Marino       gen_expr (exp, pc, ax, &value1);
1860cf7f2e2dSJohn Marino       gen_usual_unary (exp, ax, &value1);
1861cf7f2e2dSJohn Marino       if1 = ax_goto (ax, aop_if_goto);
1862cf7f2e2dSJohn Marino       go1 = ax_goto (ax, aop_goto);
1863cf7f2e2dSJohn Marino       ax_label (ax, if1, ax->len);
18645796c8dcSSimon Schubert       gen_expr (exp, pc, ax, &value2);
18655796c8dcSSimon Schubert       gen_usual_unary (exp, ax, &value2);
1866cf7f2e2dSJohn Marino       if2 = ax_goto (ax, aop_if_goto);
1867cf7f2e2dSJohn Marino       go2 = ax_goto (ax, aop_goto);
1868cf7f2e2dSJohn Marino       ax_label (ax, if2, ax->len);
1869cf7f2e2dSJohn Marino       ax_const_l (ax, 1);
1870cf7f2e2dSJohn Marino       end = ax_goto (ax, aop_goto);
1871cf7f2e2dSJohn Marino       ax_label (ax, go1, ax->len);
1872cf7f2e2dSJohn Marino       ax_label (ax, go2, ax->len);
1873cf7f2e2dSJohn Marino       ax_const_l (ax, 0);
1874cf7f2e2dSJohn Marino       ax_label (ax, end, ax->len);
1875cf7f2e2dSJohn Marino       value->kind = axs_rvalue;
1876cf7f2e2dSJohn Marino       value->type = int_type;
1877cf7f2e2dSJohn Marino       break;
1878cf7f2e2dSJohn Marino 
1879cf7f2e2dSJohn Marino     case BINOP_LOGICAL_OR:
1880cf7f2e2dSJohn Marino       (*pc)++;
1881cf7f2e2dSJohn Marino       /* Generate the obvious sequence of tests and jumps.  */
1882cf7f2e2dSJohn Marino       gen_expr (exp, pc, ax, &value1);
1883cf7f2e2dSJohn Marino       gen_usual_unary (exp, ax, &value1);
1884cf7f2e2dSJohn Marino       if1 = ax_goto (ax, aop_if_goto);
1885cf7f2e2dSJohn Marino       gen_expr (exp, pc, ax, &value2);
1886cf7f2e2dSJohn Marino       gen_usual_unary (exp, ax, &value2);
1887cf7f2e2dSJohn Marino       if2 = ax_goto (ax, aop_if_goto);
1888cf7f2e2dSJohn Marino       ax_const_l (ax, 0);
1889cf7f2e2dSJohn Marino       end = ax_goto (ax, aop_goto);
1890cf7f2e2dSJohn Marino       ax_label (ax, if1, ax->len);
1891cf7f2e2dSJohn Marino       ax_label (ax, if2, ax->len);
1892cf7f2e2dSJohn Marino       ax_const_l (ax, 1);
1893cf7f2e2dSJohn Marino       ax_label (ax, end, ax->len);
1894cf7f2e2dSJohn Marino       value->kind = axs_rvalue;
1895cf7f2e2dSJohn Marino       value->type = int_type;
1896cf7f2e2dSJohn Marino       break;
1897cf7f2e2dSJohn Marino 
1898cf7f2e2dSJohn Marino     case TERNOP_COND:
1899cf7f2e2dSJohn Marino       (*pc)++;
1900cf7f2e2dSJohn Marino       gen_expr (exp, pc, ax, &value1);
1901cf7f2e2dSJohn Marino       gen_usual_unary (exp, ax, &value1);
1902cf7f2e2dSJohn Marino       /* For (A ? B : C), it's easiest to generate subexpression
1903cf7f2e2dSJohn Marino 	 bytecodes in order, but if_goto jumps on true, so we invert
1904cf7f2e2dSJohn Marino 	 the sense of A.  Then we can do B by dropping through, and
1905cf7f2e2dSJohn Marino 	 jump to do C.  */
1906cf7f2e2dSJohn Marino       gen_logical_not (ax, &value1, int_type);
1907cf7f2e2dSJohn Marino       if1 = ax_goto (ax, aop_if_goto);
1908cf7f2e2dSJohn Marino       gen_expr (exp, pc, ax, &value2);
1909cf7f2e2dSJohn Marino       gen_usual_unary (exp, ax, &value2);
1910cf7f2e2dSJohn Marino       end = ax_goto (ax, aop_goto);
1911cf7f2e2dSJohn Marino       ax_label (ax, if1, ax->len);
1912cf7f2e2dSJohn Marino       gen_expr (exp, pc, ax, &value3);
1913cf7f2e2dSJohn Marino       gen_usual_unary (exp, ax, &value3);
1914cf7f2e2dSJohn Marino       ax_label (ax, end, ax->len);
1915cf7f2e2dSJohn Marino       /* This is arbitary - what if B and C are incompatible types? */
1916cf7f2e2dSJohn Marino       value->type = value2.type;
1917cf7f2e2dSJohn Marino       value->kind = value2.kind;
1918cf7f2e2dSJohn Marino       break;
1919cf7f2e2dSJohn Marino 
1920cf7f2e2dSJohn Marino     case BINOP_ASSIGN:
1921cf7f2e2dSJohn Marino       (*pc)++;
1922cf7f2e2dSJohn Marino       if ((*pc)[0].opcode == OP_INTERNALVAR)
19235796c8dcSSimon Schubert 	{
1924cf7f2e2dSJohn Marino 	  char *name = internalvar_name ((*pc)[1].internalvar);
1925cf7f2e2dSJohn Marino 	  struct trace_state_variable *tsv;
1926cf7f2e2dSJohn Marino 
1927cf7f2e2dSJohn Marino 	  (*pc) += 3;
1928cf7f2e2dSJohn Marino 	  gen_expr (exp, pc, ax, value);
1929cf7f2e2dSJohn Marino 	  tsv = find_trace_state_variable (name);
1930cf7f2e2dSJohn Marino 	  if (tsv)
19315796c8dcSSimon Schubert 	    {
1932cf7f2e2dSJohn Marino 	      ax_tsv (ax, aop_setv, tsv->number);
1933cf7f2e2dSJohn Marino 	      if (trace_kludge)
1934cf7f2e2dSJohn Marino 		ax_tsv (ax, aop_tracev, tsv->number);
19355796c8dcSSimon Schubert 	    }
19365796c8dcSSimon Schubert 	  else
1937c50c785cSJohn Marino 	    error (_("$%s is not a trace state variable, "
1938c50c785cSJohn Marino 		     "may not assign to it"), name);
19395796c8dcSSimon Schubert 	}
1940cf7f2e2dSJohn Marino       else
1941cf7f2e2dSJohn Marino 	error (_("May only assign to trace state variables"));
1942cf7f2e2dSJohn Marino       break;
1943cf7f2e2dSJohn Marino 
1944cf7f2e2dSJohn Marino     case BINOP_ASSIGN_MODIFY:
1945cf7f2e2dSJohn Marino       (*pc)++;
1946cf7f2e2dSJohn Marino       op2 = (*pc)[0].opcode;
1947cf7f2e2dSJohn Marino       (*pc)++;
1948cf7f2e2dSJohn Marino       (*pc)++;
1949cf7f2e2dSJohn Marino       if ((*pc)[0].opcode == OP_INTERNALVAR)
1950cf7f2e2dSJohn Marino 	{
1951cf7f2e2dSJohn Marino 	  char *name = internalvar_name ((*pc)[1].internalvar);
1952cf7f2e2dSJohn Marino 	  struct trace_state_variable *tsv;
1953cf7f2e2dSJohn Marino 
1954cf7f2e2dSJohn Marino 	  (*pc) += 3;
1955cf7f2e2dSJohn Marino 	  tsv = find_trace_state_variable (name);
1956cf7f2e2dSJohn Marino 	  if (tsv)
1957cf7f2e2dSJohn Marino 	    {
1958cf7f2e2dSJohn Marino 	      /* The tsv will be the left half of the binary operation.  */
1959cf7f2e2dSJohn Marino 	      ax_tsv (ax, aop_getv, tsv->number);
1960cf7f2e2dSJohn Marino 	      if (trace_kludge)
1961cf7f2e2dSJohn Marino 		ax_tsv (ax, aop_tracev, tsv->number);
1962cf7f2e2dSJohn Marino 	      /* Trace state variables are always 64-bit integers.  */
1963cf7f2e2dSJohn Marino 	      value1.kind = axs_rvalue;
1964cf7f2e2dSJohn Marino 	      value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1965cf7f2e2dSJohn Marino 	      /* Now do right half of expression.  */
1966cf7f2e2dSJohn Marino 	      gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1967cf7f2e2dSJohn Marino 	      /* We have a result of the binary op, set the tsv.  */
1968cf7f2e2dSJohn Marino 	      ax_tsv (ax, aop_setv, tsv->number);
1969cf7f2e2dSJohn Marino 	      if (trace_kludge)
1970cf7f2e2dSJohn Marino 		ax_tsv (ax, aop_tracev, tsv->number);
1971cf7f2e2dSJohn Marino 	    }
1972cf7f2e2dSJohn Marino 	  else
1973c50c785cSJohn Marino 	    error (_("$%s is not a trace state variable, "
1974c50c785cSJohn Marino 		     "may not assign to it"), name);
1975cf7f2e2dSJohn Marino 	}
1976cf7f2e2dSJohn Marino       else
1977cf7f2e2dSJohn Marino 	error (_("May only assign to trace state variables"));
19785796c8dcSSimon Schubert       break;
19795796c8dcSSimon Schubert 
19805796c8dcSSimon Schubert       /* Note that we need to be a little subtle about generating code
19815796c8dcSSimon Schubert          for comma.  In C, we can do some optimizations here because
19825796c8dcSSimon Schubert          we know the left operand is only being evaluated for effect.
19835796c8dcSSimon Schubert          However, if the tracing kludge is in effect, then we always
19845796c8dcSSimon Schubert          need to evaluate the left hand side fully, so that all the
19855796c8dcSSimon Schubert          variables it mentions get traced.  */
19865796c8dcSSimon Schubert     case BINOP_COMMA:
19875796c8dcSSimon Schubert       (*pc)++;
19885796c8dcSSimon Schubert       gen_expr (exp, pc, ax, &value1);
19895796c8dcSSimon Schubert       /* Don't just dispose of the left operand.  We might be tracing,
19905796c8dcSSimon Schubert          in which case we want to emit code to trace it if it's an
19915796c8dcSSimon Schubert          lvalue.  */
1992cf7f2e2dSJohn Marino       gen_traced_pop (exp->gdbarch, ax, &value1);
19935796c8dcSSimon Schubert       gen_expr (exp, pc, ax, value);
19945796c8dcSSimon Schubert       /* It's the consumer's responsibility to trace the right operand.  */
19955796c8dcSSimon Schubert       break;
19965796c8dcSSimon Schubert 
19975796c8dcSSimon Schubert     case OP_LONG:		/* some integer constant */
19985796c8dcSSimon Schubert       {
19995796c8dcSSimon Schubert 	struct type *type = (*pc)[1].type;
20005796c8dcSSimon Schubert 	LONGEST k = (*pc)[2].longconst;
2001cf7f2e2dSJohn Marino 
20025796c8dcSSimon Schubert 	(*pc) += 4;
20035796c8dcSSimon Schubert 	gen_int_literal (ax, value, k, type);
20045796c8dcSSimon Schubert       }
20055796c8dcSSimon Schubert       break;
20065796c8dcSSimon Schubert 
20075796c8dcSSimon Schubert     case OP_VAR_VALUE:
20085796c8dcSSimon Schubert       gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
2009cf7f2e2dSJohn Marino 
2010cf7f2e2dSJohn Marino       if (value->optimized_out)
2011cf7f2e2dSJohn Marino 	error (_("`%s' has been optimized out, cannot use"),
2012cf7f2e2dSJohn Marino 	       SYMBOL_PRINT_NAME ((*pc)[2].symbol));
2013cf7f2e2dSJohn Marino 
20145796c8dcSSimon Schubert       (*pc) += 4;
20155796c8dcSSimon Schubert       break;
20165796c8dcSSimon Schubert 
20175796c8dcSSimon Schubert     case OP_REGISTER:
20185796c8dcSSimon Schubert       {
20195796c8dcSSimon Schubert 	const char *name = &(*pc)[2].string;
20205796c8dcSSimon Schubert 	int reg;
2021cf7f2e2dSJohn Marino 
20225796c8dcSSimon Schubert 	(*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
20235796c8dcSSimon Schubert 	reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
20245796c8dcSSimon Schubert 	if (reg == -1)
20255796c8dcSSimon Schubert 	  internal_error (__FILE__, __LINE__,
20265796c8dcSSimon Schubert 			  _("Register $%s not available"), name);
2027c50c785cSJohn Marino 	/* No support for tracing user registers yet.  */
2028c50c785cSJohn Marino 	if (reg >= gdbarch_num_regs (exp->gdbarch)
2029c50c785cSJohn Marino 	    + gdbarch_num_pseudo_regs (exp->gdbarch))
2030c50c785cSJohn Marino 	  error (_("'%s' is a user-register; "
2031c50c785cSJohn Marino 		   "GDB cannot yet trace user-register contents."),
20325796c8dcSSimon Schubert 		 name);
20335796c8dcSSimon Schubert 	value->kind = axs_lvalue_register;
20345796c8dcSSimon Schubert 	value->u.reg = reg;
20355796c8dcSSimon Schubert 	value->type = register_type (exp->gdbarch, reg);
20365796c8dcSSimon Schubert       }
20375796c8dcSSimon Schubert       break;
20385796c8dcSSimon Schubert 
20395796c8dcSSimon Schubert     case OP_INTERNALVAR:
2040cf7f2e2dSJohn Marino       {
2041*ef5ccd6cSJohn Marino 	struct internalvar *var = (*pc)[1].internalvar;
2042*ef5ccd6cSJohn Marino 	const char *name = internalvar_name (var);
2043cf7f2e2dSJohn Marino 	struct trace_state_variable *tsv;
2044cf7f2e2dSJohn Marino 
2045cf7f2e2dSJohn Marino 	(*pc) += 3;
2046cf7f2e2dSJohn Marino 	tsv = find_trace_state_variable (name);
2047cf7f2e2dSJohn Marino 	if (tsv)
2048cf7f2e2dSJohn Marino 	  {
2049cf7f2e2dSJohn Marino 	    ax_tsv (ax, aop_getv, tsv->number);
2050cf7f2e2dSJohn Marino 	    if (trace_kludge)
2051cf7f2e2dSJohn Marino 	      ax_tsv (ax, aop_tracev, tsv->number);
2052cf7f2e2dSJohn Marino 	    /* Trace state variables are always 64-bit integers.  */
2053cf7f2e2dSJohn Marino 	    value->kind = axs_rvalue;
2054cf7f2e2dSJohn Marino 	    value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2055cf7f2e2dSJohn Marino 	  }
2056*ef5ccd6cSJohn Marino 	else if (! compile_internalvar_to_ax (var, ax, value))
2057c50c785cSJohn Marino 	  error (_("$%s is not a trace state variable; GDB agent "
2058c50c785cSJohn Marino 		   "expressions cannot use convenience variables."), name);
2059cf7f2e2dSJohn Marino       }
2060cf7f2e2dSJohn Marino       break;
20615796c8dcSSimon Schubert 
20625796c8dcSSimon Schubert       /* Weirdo operator: see comments for gen_repeat for details.  */
20635796c8dcSSimon Schubert     case BINOP_REPEAT:
20645796c8dcSSimon Schubert       /* Note that gen_repeat handles its own argument evaluation.  */
20655796c8dcSSimon Schubert       (*pc)++;
20665796c8dcSSimon Schubert       gen_repeat (exp, pc, ax, value);
20675796c8dcSSimon Schubert       break;
20685796c8dcSSimon Schubert 
20695796c8dcSSimon Schubert     case UNOP_CAST:
20705796c8dcSSimon Schubert       {
20715796c8dcSSimon Schubert 	struct type *type = (*pc)[1].type;
2072cf7f2e2dSJohn Marino 
20735796c8dcSSimon Schubert 	(*pc) += 3;
20745796c8dcSSimon Schubert 	gen_expr (exp, pc, ax, value);
20755796c8dcSSimon Schubert 	gen_cast (ax, value, type);
20765796c8dcSSimon Schubert       }
20775796c8dcSSimon Schubert       break;
20785796c8dcSSimon Schubert 
2079*ef5ccd6cSJohn Marino     case UNOP_CAST_TYPE:
2080*ef5ccd6cSJohn Marino       {
2081*ef5ccd6cSJohn Marino 	int offset;
2082*ef5ccd6cSJohn Marino 	struct value *val;
2083*ef5ccd6cSJohn Marino 	struct type *type;
2084*ef5ccd6cSJohn Marino 
2085*ef5ccd6cSJohn Marino 	++*pc;
2086*ef5ccd6cSJohn Marino 	offset = *pc - exp->elts;
2087*ef5ccd6cSJohn Marino 	val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2088*ef5ccd6cSJohn Marino 	type = value_type (val);
2089*ef5ccd6cSJohn Marino 	*pc = &exp->elts[offset];
2090*ef5ccd6cSJohn Marino 
2091*ef5ccd6cSJohn Marino 	gen_expr (exp, pc, ax, value);
2092*ef5ccd6cSJohn Marino 	gen_cast (ax, value, type);
2093*ef5ccd6cSJohn Marino       }
2094*ef5ccd6cSJohn Marino       break;
2095*ef5ccd6cSJohn Marino 
20965796c8dcSSimon Schubert     case UNOP_MEMVAL:
20975796c8dcSSimon Schubert       {
20985796c8dcSSimon Schubert 	struct type *type = check_typedef ((*pc)[1].type);
2099cf7f2e2dSJohn Marino 
21005796c8dcSSimon Schubert 	(*pc) += 3;
21015796c8dcSSimon Schubert 	gen_expr (exp, pc, ax, value);
2102c50c785cSJohn Marino 
2103c50c785cSJohn Marino 	/* If we have an axs_rvalue or an axs_lvalue_memory, then we
2104c50c785cSJohn Marino 	   already have the right value on the stack.  For
2105c50c785cSJohn Marino 	   axs_lvalue_register, we must convert.  */
2106c50c785cSJohn Marino 	if (value->kind == axs_lvalue_register)
2107c50c785cSJohn Marino 	  require_rvalue (ax, value);
2108c50c785cSJohn Marino 
21095796c8dcSSimon Schubert 	value->type = type;
21105796c8dcSSimon Schubert 	value->kind = axs_lvalue_memory;
21115796c8dcSSimon Schubert       }
21125796c8dcSSimon Schubert       break;
21135796c8dcSSimon Schubert 
2114*ef5ccd6cSJohn Marino     case UNOP_MEMVAL_TYPE:
2115*ef5ccd6cSJohn Marino       {
2116*ef5ccd6cSJohn Marino 	int offset;
2117*ef5ccd6cSJohn Marino 	struct value *val;
2118*ef5ccd6cSJohn Marino 	struct type *type;
2119*ef5ccd6cSJohn Marino 
2120*ef5ccd6cSJohn Marino 	++*pc;
2121*ef5ccd6cSJohn Marino 	offset = *pc - exp->elts;
2122*ef5ccd6cSJohn Marino 	val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2123*ef5ccd6cSJohn Marino 	type = value_type (val);
2124*ef5ccd6cSJohn Marino 	*pc = &exp->elts[offset];
2125*ef5ccd6cSJohn Marino 
2126*ef5ccd6cSJohn Marino 	gen_expr (exp, pc, ax, value);
2127*ef5ccd6cSJohn Marino 
2128*ef5ccd6cSJohn Marino 	/* If we have an axs_rvalue or an axs_lvalue_memory, then we
2129*ef5ccd6cSJohn Marino 	   already have the right value on the stack.  For
2130*ef5ccd6cSJohn Marino 	   axs_lvalue_register, we must convert.  */
2131*ef5ccd6cSJohn Marino 	if (value->kind == axs_lvalue_register)
2132*ef5ccd6cSJohn Marino 	  require_rvalue (ax, value);
2133*ef5ccd6cSJohn Marino 
2134*ef5ccd6cSJohn Marino 	value->type = type;
2135*ef5ccd6cSJohn Marino 	value->kind = axs_lvalue_memory;
2136*ef5ccd6cSJohn Marino       }
2137*ef5ccd6cSJohn Marino       break;
2138*ef5ccd6cSJohn Marino 
21395796c8dcSSimon Schubert     case UNOP_PLUS:
21405796c8dcSSimon Schubert       (*pc)++;
21415796c8dcSSimon Schubert       /* + FOO is equivalent to 0 + FOO, which can be optimized.  */
21425796c8dcSSimon Schubert       gen_expr (exp, pc, ax, value);
21435796c8dcSSimon Schubert       gen_usual_unary (exp, ax, value);
21445796c8dcSSimon Schubert       break;
21455796c8dcSSimon Schubert 
21465796c8dcSSimon Schubert     case UNOP_NEG:
21475796c8dcSSimon Schubert       (*pc)++;
21485796c8dcSSimon Schubert       /* -FOO is equivalent to 0 - FOO.  */
21495796c8dcSSimon Schubert       gen_int_literal (ax, &value1, 0,
21505796c8dcSSimon Schubert 		       builtin_type (exp->gdbarch)->builtin_int);
21515796c8dcSSimon Schubert       gen_usual_unary (exp, ax, &value1);	/* shouldn't do much */
21525796c8dcSSimon Schubert       gen_expr (exp, pc, ax, &value2);
21535796c8dcSSimon Schubert       gen_usual_unary (exp, ax, &value2);
21545796c8dcSSimon Schubert       gen_usual_arithmetic (exp, ax, &value1, &value2);
21555796c8dcSSimon Schubert       gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
21565796c8dcSSimon Schubert       break;
21575796c8dcSSimon Schubert 
21585796c8dcSSimon Schubert     case UNOP_LOGICAL_NOT:
21595796c8dcSSimon Schubert       (*pc)++;
21605796c8dcSSimon Schubert       gen_expr (exp, pc, ax, value);
21615796c8dcSSimon Schubert       gen_usual_unary (exp, ax, value);
2162cf7f2e2dSJohn Marino       gen_logical_not (ax, value, int_type);
21635796c8dcSSimon Schubert       break;
21645796c8dcSSimon Schubert 
21655796c8dcSSimon Schubert     case UNOP_COMPLEMENT:
21665796c8dcSSimon Schubert       (*pc)++;
21675796c8dcSSimon Schubert       gen_expr (exp, pc, ax, value);
21685796c8dcSSimon Schubert       gen_usual_unary (exp, ax, value);
21695796c8dcSSimon Schubert       gen_integral_promotions (exp, ax, value);
21705796c8dcSSimon Schubert       gen_complement (ax, value);
21715796c8dcSSimon Schubert       break;
21725796c8dcSSimon Schubert 
21735796c8dcSSimon Schubert     case UNOP_IND:
21745796c8dcSSimon Schubert       (*pc)++;
21755796c8dcSSimon Schubert       gen_expr (exp, pc, ax, value);
21765796c8dcSSimon Schubert       gen_usual_unary (exp, ax, value);
2177cf7f2e2dSJohn Marino       if (!pointer_type (value->type))
21785796c8dcSSimon Schubert 	error (_("Argument of unary `*' is not a pointer."));
21795796c8dcSSimon Schubert       gen_deref (ax, value);
21805796c8dcSSimon Schubert       break;
21815796c8dcSSimon Schubert 
21825796c8dcSSimon Schubert     case UNOP_ADDR:
21835796c8dcSSimon Schubert       (*pc)++;
21845796c8dcSSimon Schubert       gen_expr (exp, pc, ax, value);
21855796c8dcSSimon Schubert       gen_address_of (ax, value);
21865796c8dcSSimon Schubert       break;
21875796c8dcSSimon Schubert 
21885796c8dcSSimon Schubert     case UNOP_SIZEOF:
21895796c8dcSSimon Schubert       (*pc)++;
21905796c8dcSSimon Schubert       /* Notice that gen_sizeof handles its own operand, unlike most
21915796c8dcSSimon Schubert          of the other unary operator functions.  This is because we
21925796c8dcSSimon Schubert          have to throw away the code we generate.  */
21935796c8dcSSimon Schubert       gen_sizeof (exp, pc, ax, value,
21945796c8dcSSimon Schubert 		  builtin_type (exp->gdbarch)->builtin_int);
21955796c8dcSSimon Schubert       break;
21965796c8dcSSimon Schubert 
21975796c8dcSSimon Schubert     case STRUCTOP_STRUCT:
21985796c8dcSSimon Schubert     case STRUCTOP_PTR:
21995796c8dcSSimon Schubert       {
22005796c8dcSSimon Schubert 	int length = (*pc)[1].longconst;
22015796c8dcSSimon Schubert 	char *name = &(*pc)[2].string;
22025796c8dcSSimon Schubert 
22035796c8dcSSimon Schubert 	(*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
22045796c8dcSSimon Schubert 	gen_expr (exp, pc, ax, value);
22055796c8dcSSimon Schubert 	if (op == STRUCTOP_STRUCT)
22065796c8dcSSimon Schubert 	  gen_struct_ref (exp, ax, value, name, ".", "structure or union");
22075796c8dcSSimon Schubert 	else if (op == STRUCTOP_PTR)
22085796c8dcSSimon Schubert 	  gen_struct_ref (exp, ax, value, name, "->",
22095796c8dcSSimon Schubert 			  "pointer to a structure or union");
22105796c8dcSSimon Schubert 	else
22115796c8dcSSimon Schubert 	  /* If this `if' chain doesn't handle it, then the case list
22125796c8dcSSimon Schubert 	     shouldn't mention it, and we shouldn't be here.  */
22135796c8dcSSimon Schubert 	  internal_error (__FILE__, __LINE__,
22145796c8dcSSimon Schubert 			  _("gen_expr: unhandled struct case"));
22155796c8dcSSimon Schubert       }
22165796c8dcSSimon Schubert       break;
22175796c8dcSSimon Schubert 
2218cf7f2e2dSJohn Marino     case OP_THIS:
2219cf7f2e2dSJohn Marino       {
2220a45ae5f8SJohn Marino 	struct symbol *sym, *func;
2221cf7f2e2dSJohn Marino 	struct block *b;
2222a45ae5f8SJohn Marino 	const struct language_defn *lang;
2223cf7f2e2dSJohn Marino 
2224a45ae5f8SJohn Marino 	b = block_for_pc (ax->scope);
2225a45ae5f8SJohn Marino 	func = block_linkage_function (b);
2226a45ae5f8SJohn Marino 	lang = language_def (SYMBOL_LANGUAGE (func));
2227cf7f2e2dSJohn Marino 
2228a45ae5f8SJohn Marino 	sym = lookup_language_this (lang, b);
2229cf7f2e2dSJohn Marino 	if (!sym)
2230a45ae5f8SJohn Marino 	  error (_("no `%s' found"), lang->la_name_of_this);
2231cf7f2e2dSJohn Marino 
2232cf7f2e2dSJohn Marino 	gen_var_ref (exp->gdbarch, ax, value, sym);
2233cf7f2e2dSJohn Marino 
2234cf7f2e2dSJohn Marino 	if (value->optimized_out)
2235cf7f2e2dSJohn Marino 	  error (_("`%s' has been optimized out, cannot use"),
2236cf7f2e2dSJohn Marino 		 SYMBOL_PRINT_NAME (sym));
2237cf7f2e2dSJohn Marino 
2238cf7f2e2dSJohn Marino 	(*pc) += 2;
2239cf7f2e2dSJohn Marino       }
2240cf7f2e2dSJohn Marino       break;
2241cf7f2e2dSJohn Marino 
2242cf7f2e2dSJohn Marino     case OP_SCOPE:
2243cf7f2e2dSJohn Marino       {
2244cf7f2e2dSJohn Marino 	struct type *type = (*pc)[1].type;
2245cf7f2e2dSJohn Marino 	int length = longest_to_int ((*pc)[2].longconst);
2246cf7f2e2dSJohn Marino 	char *name = &(*pc)[3].string;
2247cf7f2e2dSJohn Marino 	int found;
2248cf7f2e2dSJohn Marino 
2249cf7f2e2dSJohn Marino 	found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2250cf7f2e2dSJohn Marino 				       "?", "??");
2251cf7f2e2dSJohn Marino 	if (!found)
2252cf7f2e2dSJohn Marino 	  error (_("There is no field named %s"), name);
2253cf7f2e2dSJohn Marino 	(*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2254cf7f2e2dSJohn Marino       }
2255cf7f2e2dSJohn Marino       break;
2256cf7f2e2dSJohn Marino 
22575796c8dcSSimon Schubert     case OP_TYPE:
2258*ef5ccd6cSJohn Marino     case OP_TYPEOF:
2259*ef5ccd6cSJohn Marino     case OP_DECLTYPE:
22605796c8dcSSimon Schubert       error (_("Attempt to use a type name as an expression."));
22615796c8dcSSimon Schubert 
22625796c8dcSSimon Schubert     default:
2263cf7f2e2dSJohn Marino       error (_("Unsupported operator %s (%d) in expression."),
2264*ef5ccd6cSJohn Marino 	     op_name (exp, op), op);
2265cf7f2e2dSJohn Marino     }
2266cf7f2e2dSJohn Marino }
2267cf7f2e2dSJohn Marino 
2268cf7f2e2dSJohn Marino /* This handles the middle-to-right-side of code generation for binary
2269cf7f2e2dSJohn Marino    expressions, which is shared between regular binary operations and
2270cf7f2e2dSJohn Marino    assign-modify (+= and friends) expressions.  */
2271cf7f2e2dSJohn Marino 
2272cf7f2e2dSJohn Marino static void
gen_expr_binop_rest(struct expression * exp,enum exp_opcode op,union exp_element ** pc,struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2)2273cf7f2e2dSJohn Marino gen_expr_binop_rest (struct expression *exp,
2274cf7f2e2dSJohn Marino 		     enum exp_opcode op, union exp_element **pc,
2275cf7f2e2dSJohn Marino 		     struct agent_expr *ax, struct axs_value *value,
2276cf7f2e2dSJohn Marino 		     struct axs_value *value1, struct axs_value *value2)
2277cf7f2e2dSJohn Marino {
2278cf7f2e2dSJohn Marino   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2279cf7f2e2dSJohn Marino 
2280cf7f2e2dSJohn Marino   gen_expr (exp, pc, ax, value2);
2281cf7f2e2dSJohn Marino   gen_usual_unary (exp, ax, value2);
2282cf7f2e2dSJohn Marino   gen_usual_arithmetic (exp, ax, value1, value2);
2283cf7f2e2dSJohn Marino   switch (op)
2284cf7f2e2dSJohn Marino     {
2285cf7f2e2dSJohn Marino     case BINOP_ADD:
2286cf7f2e2dSJohn Marino       if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2287cf7f2e2dSJohn Marino 	  && pointer_type (value2->type))
2288cf7f2e2dSJohn Marino 	{
2289cf7f2e2dSJohn Marino 	  /* Swap the values and proceed normally.  */
2290cf7f2e2dSJohn Marino 	  ax_simple (ax, aop_swap);
2291cf7f2e2dSJohn Marino 	  gen_ptradd (ax, value, value2, value1);
2292cf7f2e2dSJohn Marino 	}
2293cf7f2e2dSJohn Marino       else if (pointer_type (value1->type)
2294cf7f2e2dSJohn Marino 	       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2295cf7f2e2dSJohn Marino 	gen_ptradd (ax, value, value1, value2);
2296cf7f2e2dSJohn Marino       else
2297cf7f2e2dSJohn Marino 	gen_binop (ax, value, value1, value2,
2298cf7f2e2dSJohn Marino 		   aop_add, aop_add, 1, "addition");
2299cf7f2e2dSJohn Marino       break;
2300cf7f2e2dSJohn Marino     case BINOP_SUB:
2301cf7f2e2dSJohn Marino       if (pointer_type (value1->type)
2302cf7f2e2dSJohn Marino 	  && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2303cf7f2e2dSJohn Marino 	gen_ptrsub (ax,value, value1, value2);
2304cf7f2e2dSJohn Marino       else if (pointer_type (value1->type)
2305cf7f2e2dSJohn Marino 	       && pointer_type (value2->type))
2306cf7f2e2dSJohn Marino 	/* FIXME --- result type should be ptrdiff_t */
2307cf7f2e2dSJohn Marino 	gen_ptrdiff (ax, value, value1, value2,
2308cf7f2e2dSJohn Marino 		     builtin_type (exp->gdbarch)->builtin_long);
2309cf7f2e2dSJohn Marino       else
2310cf7f2e2dSJohn Marino 	gen_binop (ax, value, value1, value2,
2311cf7f2e2dSJohn Marino 		   aop_sub, aop_sub, 1, "subtraction");
2312cf7f2e2dSJohn Marino       break;
2313cf7f2e2dSJohn Marino     case BINOP_MUL:
2314cf7f2e2dSJohn Marino       gen_binop (ax, value, value1, value2,
2315cf7f2e2dSJohn Marino 		 aop_mul, aop_mul, 1, "multiplication");
2316cf7f2e2dSJohn Marino       break;
2317cf7f2e2dSJohn Marino     case BINOP_DIV:
2318cf7f2e2dSJohn Marino       gen_binop (ax, value, value1, value2,
2319cf7f2e2dSJohn Marino 		 aop_div_signed, aop_div_unsigned, 1, "division");
2320cf7f2e2dSJohn Marino       break;
2321cf7f2e2dSJohn Marino     case BINOP_REM:
2322cf7f2e2dSJohn Marino       gen_binop (ax, value, value1, value2,
2323cf7f2e2dSJohn Marino 		 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2324cf7f2e2dSJohn Marino       break;
2325cf7f2e2dSJohn Marino     case BINOP_LSH:
2326cf7f2e2dSJohn Marino       gen_binop (ax, value, value1, value2,
2327cf7f2e2dSJohn Marino 		 aop_lsh, aop_lsh, 1, "left shift");
2328cf7f2e2dSJohn Marino       break;
2329cf7f2e2dSJohn Marino     case BINOP_RSH:
2330cf7f2e2dSJohn Marino       gen_binop (ax, value, value1, value2,
2331cf7f2e2dSJohn Marino 		 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2332cf7f2e2dSJohn Marino       break;
2333cf7f2e2dSJohn Marino     case BINOP_SUBSCRIPT:
2334cf7f2e2dSJohn Marino       {
2335cf7f2e2dSJohn Marino 	struct type *type;
2336cf7f2e2dSJohn Marino 
2337cf7f2e2dSJohn Marino 	if (binop_types_user_defined_p (op, value1->type, value2->type))
2338cf7f2e2dSJohn Marino 	  {
2339c50c785cSJohn Marino 	    error (_("cannot subscript requested type: "
2340c50c785cSJohn Marino 		     "cannot call user defined functions"));
2341cf7f2e2dSJohn Marino 	  }
2342cf7f2e2dSJohn Marino 	else
2343cf7f2e2dSJohn Marino 	  {
2344cf7f2e2dSJohn Marino 	    /* If the user attempts to subscript something that is not
2345cf7f2e2dSJohn Marino 	       an array or pointer type (like a plain int variable for
2346cf7f2e2dSJohn Marino 	       example), then report this as an error.  */
2347cf7f2e2dSJohn Marino 	    type = check_typedef (value1->type);
2348cf7f2e2dSJohn Marino 	    if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2349cf7f2e2dSJohn Marino 		&& TYPE_CODE (type) != TYPE_CODE_PTR)
2350cf7f2e2dSJohn Marino 	      {
2351cf7f2e2dSJohn Marino 		if (TYPE_NAME (type))
2352cf7f2e2dSJohn Marino 		  error (_("cannot subscript something of type `%s'"),
2353cf7f2e2dSJohn Marino 			 TYPE_NAME (type));
2354cf7f2e2dSJohn Marino 		else
2355cf7f2e2dSJohn Marino 		  error (_("cannot subscript requested type"));
2356cf7f2e2dSJohn Marino 	      }
2357cf7f2e2dSJohn Marino 	  }
2358cf7f2e2dSJohn Marino 
2359cf7f2e2dSJohn Marino 	if (!is_integral_type (value2->type))
2360c50c785cSJohn Marino 	  error (_("Argument to arithmetic operation "
2361c50c785cSJohn Marino 		   "not a number or boolean."));
2362cf7f2e2dSJohn Marino 
2363cf7f2e2dSJohn Marino 	gen_ptradd (ax, value, value1, value2);
2364cf7f2e2dSJohn Marino 	gen_deref (ax, value);
2365cf7f2e2dSJohn Marino 	break;
2366cf7f2e2dSJohn Marino       }
2367cf7f2e2dSJohn Marino     case BINOP_BITWISE_AND:
2368cf7f2e2dSJohn Marino       gen_binop (ax, value, value1, value2,
2369cf7f2e2dSJohn Marino 		 aop_bit_and, aop_bit_and, 0, "bitwise and");
2370cf7f2e2dSJohn Marino       break;
2371cf7f2e2dSJohn Marino 
2372cf7f2e2dSJohn Marino     case BINOP_BITWISE_IOR:
2373cf7f2e2dSJohn Marino       gen_binop (ax, value, value1, value2,
2374cf7f2e2dSJohn Marino 		 aop_bit_or, aop_bit_or, 0, "bitwise or");
2375cf7f2e2dSJohn Marino       break;
2376cf7f2e2dSJohn Marino 
2377cf7f2e2dSJohn Marino     case BINOP_BITWISE_XOR:
2378cf7f2e2dSJohn Marino       gen_binop (ax, value, value1, value2,
2379cf7f2e2dSJohn Marino 		 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2380cf7f2e2dSJohn Marino       break;
2381cf7f2e2dSJohn Marino 
2382cf7f2e2dSJohn Marino     case BINOP_EQUAL:
2383cf7f2e2dSJohn Marino       gen_equal (ax, value, value1, value2, int_type);
2384cf7f2e2dSJohn Marino       break;
2385cf7f2e2dSJohn Marino 
2386cf7f2e2dSJohn Marino     case BINOP_NOTEQUAL:
2387cf7f2e2dSJohn Marino       gen_equal (ax, value, value1, value2, int_type);
2388cf7f2e2dSJohn Marino       gen_logical_not (ax, value, int_type);
2389cf7f2e2dSJohn Marino       break;
2390cf7f2e2dSJohn Marino 
2391cf7f2e2dSJohn Marino     case BINOP_LESS:
2392cf7f2e2dSJohn Marino       gen_less (ax, value, value1, value2, int_type);
2393cf7f2e2dSJohn Marino       break;
2394cf7f2e2dSJohn Marino 
2395cf7f2e2dSJohn Marino     case BINOP_GTR:
2396cf7f2e2dSJohn Marino       ax_simple (ax, aop_swap);
2397cf7f2e2dSJohn Marino       gen_less (ax, value, value1, value2, int_type);
2398cf7f2e2dSJohn Marino       break;
2399cf7f2e2dSJohn Marino 
2400cf7f2e2dSJohn Marino     case BINOP_LEQ:
2401cf7f2e2dSJohn Marino       ax_simple (ax, aop_swap);
2402cf7f2e2dSJohn Marino       gen_less (ax, value, value1, value2, int_type);
2403cf7f2e2dSJohn Marino       gen_logical_not (ax, value, int_type);
2404cf7f2e2dSJohn Marino       break;
2405cf7f2e2dSJohn Marino 
2406cf7f2e2dSJohn Marino     case BINOP_GEQ:
2407cf7f2e2dSJohn Marino       gen_less (ax, value, value1, value2, int_type);
2408cf7f2e2dSJohn Marino       gen_logical_not (ax, value, int_type);
2409cf7f2e2dSJohn Marino       break;
2410cf7f2e2dSJohn Marino 
2411cf7f2e2dSJohn Marino     default:
2412cf7f2e2dSJohn Marino       /* We should only list operators in the outer case statement
2413cf7f2e2dSJohn Marino 	 that we actually handle in the inner case statement.  */
2414cf7f2e2dSJohn Marino       internal_error (__FILE__, __LINE__,
2415cf7f2e2dSJohn Marino 		      _("gen_expr: op case sets don't match"));
24165796c8dcSSimon Schubert     }
24175796c8dcSSimon Schubert }
24185796c8dcSSimon Schubert 
24195796c8dcSSimon Schubert 
2420cf7f2e2dSJohn Marino /* Given a single variable and a scope, generate bytecodes to trace
2421cf7f2e2dSJohn Marino    its value.  This is for use in situations where we have only a
2422cf7f2e2dSJohn Marino    variable's name, and no parsed expression; for instance, when the
2423cf7f2e2dSJohn Marino    name comes from a list of local variables of a function.  */
2424cf7f2e2dSJohn Marino 
2425cf7f2e2dSJohn Marino struct agent_expr *
gen_trace_for_var(CORE_ADDR scope,struct gdbarch * gdbarch,struct symbol * var)2426cf7f2e2dSJohn Marino gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2427cf7f2e2dSJohn Marino 		   struct symbol *var)
2428cf7f2e2dSJohn Marino {
2429cf7f2e2dSJohn Marino   struct cleanup *old_chain = 0;
2430cf7f2e2dSJohn Marino   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2431cf7f2e2dSJohn Marino   struct axs_value value;
2432cf7f2e2dSJohn Marino 
2433cf7f2e2dSJohn Marino   old_chain = make_cleanup_free_agent_expr (ax);
2434cf7f2e2dSJohn Marino 
2435cf7f2e2dSJohn Marino   trace_kludge = 1;
2436cf7f2e2dSJohn Marino   gen_var_ref (gdbarch, ax, &value, var);
2437cf7f2e2dSJohn Marino 
2438cf7f2e2dSJohn Marino   /* If there is no actual variable to trace, flag it by returning
2439cf7f2e2dSJohn Marino      an empty agent expression.  */
2440cf7f2e2dSJohn Marino   if (value.optimized_out)
2441cf7f2e2dSJohn Marino     {
2442cf7f2e2dSJohn Marino       do_cleanups (old_chain);
2443cf7f2e2dSJohn Marino       return NULL;
2444cf7f2e2dSJohn Marino     }
2445cf7f2e2dSJohn Marino 
2446cf7f2e2dSJohn Marino   /* Make sure we record the final object, and get rid of it.  */
2447cf7f2e2dSJohn Marino   gen_traced_pop (gdbarch, ax, &value);
2448cf7f2e2dSJohn Marino 
2449cf7f2e2dSJohn Marino   /* Oh, and terminate.  */
2450cf7f2e2dSJohn Marino   ax_simple (ax, aop_end);
2451cf7f2e2dSJohn Marino 
2452cf7f2e2dSJohn Marino   /* We have successfully built the agent expr, so cancel the cleanup
2453cf7f2e2dSJohn Marino      request.  If we add more cleanups that we always want done, this
2454cf7f2e2dSJohn Marino      will have to get more complicated.  */
2455cf7f2e2dSJohn Marino   discard_cleanups (old_chain);
2456cf7f2e2dSJohn Marino   return ax;
2457cf7f2e2dSJohn Marino }
24585796c8dcSSimon Schubert 
24595796c8dcSSimon Schubert /* Generating bytecode from GDB expressions: driver */
24605796c8dcSSimon Schubert 
24615796c8dcSSimon Schubert /* Given a GDB expression EXPR, return bytecode to trace its value.
24625796c8dcSSimon Schubert    The result will use the `trace' and `trace_quick' bytecodes to
24635796c8dcSSimon Schubert    record the value of all memory touched by the expression.  The
24645796c8dcSSimon Schubert    caller can then use the ax_reqs function to discover which
24655796c8dcSSimon Schubert    registers it relies upon.  */
24665796c8dcSSimon Schubert struct agent_expr *
gen_trace_for_expr(CORE_ADDR scope,struct expression * expr)24675796c8dcSSimon Schubert gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
24685796c8dcSSimon Schubert {
24695796c8dcSSimon Schubert   struct cleanup *old_chain = 0;
2470cf7f2e2dSJohn Marino   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
24715796c8dcSSimon Schubert   union exp_element *pc;
24725796c8dcSSimon Schubert   struct axs_value value;
24735796c8dcSSimon Schubert 
24745796c8dcSSimon Schubert   old_chain = make_cleanup_free_agent_expr (ax);
24755796c8dcSSimon Schubert 
24765796c8dcSSimon Schubert   pc = expr->elts;
24775796c8dcSSimon Schubert   trace_kludge = 1;
2478cf7f2e2dSJohn Marino   value.optimized_out = 0;
24795796c8dcSSimon Schubert   gen_expr (expr, &pc, ax, &value);
24805796c8dcSSimon Schubert 
24815796c8dcSSimon Schubert   /* Make sure we record the final object, and get rid of it.  */
2482cf7f2e2dSJohn Marino   gen_traced_pop (expr->gdbarch, ax, &value);
24835796c8dcSSimon Schubert 
24845796c8dcSSimon Schubert   /* Oh, and terminate.  */
24855796c8dcSSimon Schubert   ax_simple (ax, aop_end);
24865796c8dcSSimon Schubert 
24875796c8dcSSimon Schubert   /* We have successfully built the agent expr, so cancel the cleanup
24885796c8dcSSimon Schubert      request.  If we add more cleanups that we always want done, this
24895796c8dcSSimon Schubert      will have to get more complicated.  */
24905796c8dcSSimon Schubert   discard_cleanups (old_chain);
24915796c8dcSSimon Schubert   return ax;
24925796c8dcSSimon Schubert }
24935796c8dcSSimon Schubert 
24945796c8dcSSimon Schubert /* Given a GDB expression EXPR, return a bytecode sequence that will
24955796c8dcSSimon Schubert    evaluate and return a result.  The bytecodes will do a direct
24965796c8dcSSimon Schubert    evaluation, using the current data on the target, rather than
24975796c8dcSSimon Schubert    recording blocks of memory and registers for later use, as
24985796c8dcSSimon Schubert    gen_trace_for_expr does.  The generated bytecode sequence leaves
24995796c8dcSSimon Schubert    the result of expression evaluation on the top of the stack.  */
25005796c8dcSSimon Schubert 
25015796c8dcSSimon Schubert struct agent_expr *
gen_eval_for_expr(CORE_ADDR scope,struct expression * expr)25025796c8dcSSimon Schubert gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
25035796c8dcSSimon Schubert {
25045796c8dcSSimon Schubert   struct cleanup *old_chain = 0;
2505cf7f2e2dSJohn Marino   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
25065796c8dcSSimon Schubert   union exp_element *pc;
25075796c8dcSSimon Schubert   struct axs_value value;
25085796c8dcSSimon Schubert 
25095796c8dcSSimon Schubert   old_chain = make_cleanup_free_agent_expr (ax);
25105796c8dcSSimon Schubert 
25115796c8dcSSimon Schubert   pc = expr->elts;
25125796c8dcSSimon Schubert   trace_kludge = 0;
2513cf7f2e2dSJohn Marino   value.optimized_out = 0;
25145796c8dcSSimon Schubert   gen_expr (expr, &pc, ax, &value);
25155796c8dcSSimon Schubert 
2516cf7f2e2dSJohn Marino   require_rvalue (ax, &value);
2517cf7f2e2dSJohn Marino 
25185796c8dcSSimon Schubert   /* Oh, and terminate.  */
25195796c8dcSSimon Schubert   ax_simple (ax, aop_end);
25205796c8dcSSimon Schubert 
25215796c8dcSSimon Schubert   /* We have successfully built the agent expr, so cancel the cleanup
25225796c8dcSSimon Schubert      request.  If we add more cleanups that we always want done, this
25235796c8dcSSimon Schubert      will have to get more complicated.  */
25245796c8dcSSimon Schubert   discard_cleanups (old_chain);
25255796c8dcSSimon Schubert   return ax;
25265796c8dcSSimon Schubert }
25275796c8dcSSimon Schubert 
2528a45ae5f8SJohn Marino struct agent_expr *
gen_trace_for_return_address(CORE_ADDR scope,struct gdbarch * gdbarch)2529a45ae5f8SJohn Marino gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch)
2530a45ae5f8SJohn Marino {
2531a45ae5f8SJohn Marino   struct cleanup *old_chain = 0;
2532a45ae5f8SJohn Marino   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2533a45ae5f8SJohn Marino   struct axs_value value;
2534a45ae5f8SJohn Marino 
2535a45ae5f8SJohn Marino   old_chain = make_cleanup_free_agent_expr (ax);
2536a45ae5f8SJohn Marino 
2537a45ae5f8SJohn Marino   trace_kludge = 1;
2538a45ae5f8SJohn Marino 
2539a45ae5f8SJohn Marino   gdbarch_gen_return_address (gdbarch, ax, &value, scope);
2540a45ae5f8SJohn Marino 
2541a45ae5f8SJohn Marino   /* Make sure we record the final object, and get rid of it.  */
2542a45ae5f8SJohn Marino   gen_traced_pop (gdbarch, ax, &value);
2543a45ae5f8SJohn Marino 
2544a45ae5f8SJohn Marino   /* Oh, and terminate.  */
2545a45ae5f8SJohn Marino   ax_simple (ax, aop_end);
2546a45ae5f8SJohn Marino 
2547a45ae5f8SJohn Marino   /* We have successfully built the agent expr, so cancel the cleanup
2548a45ae5f8SJohn Marino      request.  If we add more cleanups that we always want done, this
2549a45ae5f8SJohn Marino      will have to get more complicated.  */
2550a45ae5f8SJohn Marino   discard_cleanups (old_chain);
2551a45ae5f8SJohn Marino   return ax;
2552a45ae5f8SJohn Marino }
2553a45ae5f8SJohn Marino 
2554*ef5ccd6cSJohn Marino /* Given a collection of printf-style arguments, generate code to
2555*ef5ccd6cSJohn Marino    evaluate the arguments and pass everything to a special
2556*ef5ccd6cSJohn Marino    bytecode.  */
2557*ef5ccd6cSJohn Marino 
2558*ef5ccd6cSJohn Marino struct agent_expr *
gen_printf(CORE_ADDR scope,struct gdbarch * gdbarch,CORE_ADDR function,LONGEST channel,const char * format,int fmtlen,struct format_piece * frags,int nargs,struct expression ** exprs)2559*ef5ccd6cSJohn Marino gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2560*ef5ccd6cSJohn Marino 	    CORE_ADDR function, LONGEST channel,
2561*ef5ccd6cSJohn Marino 	    const char *format, int fmtlen,
2562*ef5ccd6cSJohn Marino 	    struct format_piece *frags,
2563*ef5ccd6cSJohn Marino 	    int nargs, struct expression **exprs)
2564*ef5ccd6cSJohn Marino {
2565*ef5ccd6cSJohn Marino   struct cleanup *old_chain = 0;
2566*ef5ccd6cSJohn Marino   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2567*ef5ccd6cSJohn Marino   union exp_element *pc;
2568*ef5ccd6cSJohn Marino   struct axs_value value;
2569*ef5ccd6cSJohn Marino   int tem;
2570*ef5ccd6cSJohn Marino 
2571*ef5ccd6cSJohn Marino   old_chain = make_cleanup_free_agent_expr (ax);
2572*ef5ccd6cSJohn Marino 
2573*ef5ccd6cSJohn Marino   /* Evaluate and push the args on the stack in reverse order,
2574*ef5ccd6cSJohn Marino      for simplicity of collecting them on the target side.  */
2575*ef5ccd6cSJohn Marino   for (tem = nargs - 1; tem >= 0; --tem)
2576*ef5ccd6cSJohn Marino     {
2577*ef5ccd6cSJohn Marino       pc = exprs[tem]->elts;
2578*ef5ccd6cSJohn Marino       /* We're computing values, not doing side effects.  */
2579*ef5ccd6cSJohn Marino       trace_kludge = 0;
2580*ef5ccd6cSJohn Marino       value.optimized_out = 0;
2581*ef5ccd6cSJohn Marino       gen_expr (exprs[tem], &pc, ax, &value);
2582*ef5ccd6cSJohn Marino       require_rvalue (ax, &value);
2583*ef5ccd6cSJohn Marino     }
2584*ef5ccd6cSJohn Marino 
2585*ef5ccd6cSJohn Marino   /* Push function and channel.  */
2586*ef5ccd6cSJohn Marino   ax_const_l (ax, channel);
2587*ef5ccd6cSJohn Marino   ax_const_l (ax, function);
2588*ef5ccd6cSJohn Marino 
2589*ef5ccd6cSJohn Marino   /* Issue the printf bytecode proper.  */
2590*ef5ccd6cSJohn Marino   ax_simple (ax, aop_printf);
2591*ef5ccd6cSJohn Marino   ax_simple (ax, nargs);
2592*ef5ccd6cSJohn Marino   ax_string (ax, format, fmtlen);
2593*ef5ccd6cSJohn Marino 
2594*ef5ccd6cSJohn Marino   /* And terminate.  */
2595*ef5ccd6cSJohn Marino   ax_simple (ax, aop_end);
2596*ef5ccd6cSJohn Marino 
2597*ef5ccd6cSJohn Marino   /* We have successfully built the agent expr, so cancel the cleanup
2598*ef5ccd6cSJohn Marino      request.  If we add more cleanups that we always want done, this
2599*ef5ccd6cSJohn Marino      will have to get more complicated.  */
2600*ef5ccd6cSJohn Marino   discard_cleanups (old_chain);
2601*ef5ccd6cSJohn Marino 
2602*ef5ccd6cSJohn Marino   return ax;
2603*ef5ccd6cSJohn Marino }
2604*ef5ccd6cSJohn Marino 
26055796c8dcSSimon Schubert static void
agent_eval_command_one(char * exp,int eval,CORE_ADDR pc)2606*ef5ccd6cSJohn Marino agent_eval_command_one (char *exp, int eval, CORE_ADDR pc)
26075796c8dcSSimon Schubert {
26085796c8dcSSimon Schubert   struct cleanup *old_chain = 0;
26095796c8dcSSimon Schubert   struct expression *expr;
26105796c8dcSSimon Schubert   struct agent_expr *agent;
2611*ef5ccd6cSJohn Marino   const char *arg;
26125796c8dcSSimon Schubert 
2613*ef5ccd6cSJohn Marino   if (!eval)
2614*ef5ccd6cSJohn Marino     {
2615a45ae5f8SJohn Marino       trace_string_kludge = 0;
2616a45ae5f8SJohn Marino       if (*exp == '/')
2617a45ae5f8SJohn Marino         exp = decode_agent_options (exp);
2618*ef5ccd6cSJohn Marino     }
2619a45ae5f8SJohn Marino 
2620*ef5ccd6cSJohn Marino   arg = exp;
2621*ef5ccd6cSJohn Marino   if (!eval && strcmp (arg, "$_ret") == 0)
2622a45ae5f8SJohn Marino     {
2623*ef5ccd6cSJohn Marino       agent = gen_trace_for_return_address (pc, get_current_arch ());
2624a45ae5f8SJohn Marino       old_chain = make_cleanup_free_agent_expr (agent);
2625a45ae5f8SJohn Marino     }
2626a45ae5f8SJohn Marino   else
2627a45ae5f8SJohn Marino     {
2628*ef5ccd6cSJohn Marino       expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
26295796c8dcSSimon Schubert       old_chain = make_cleanup (free_current_contents, &expr);
2630*ef5ccd6cSJohn Marino       if (eval)
2631*ef5ccd6cSJohn Marino 	agent = gen_eval_for_expr (pc, expr);
2632*ef5ccd6cSJohn Marino       else
2633*ef5ccd6cSJohn Marino 	agent = gen_trace_for_expr (pc, expr);
26345796c8dcSSimon Schubert       make_cleanup_free_agent_expr (agent);
2635a45ae5f8SJohn Marino     }
2636a45ae5f8SJohn Marino 
2637cf7f2e2dSJohn Marino   ax_reqs (agent);
26385796c8dcSSimon Schubert   ax_print (gdb_stdout, agent);
26395796c8dcSSimon Schubert 
26405796c8dcSSimon Schubert   /* It would be nice to call ax_reqs here to gather some general info
26415796c8dcSSimon Schubert      about the expression, and then print out the result.  */
26425796c8dcSSimon Schubert 
26435796c8dcSSimon Schubert   do_cleanups (old_chain);
26445796c8dcSSimon Schubert   dont_repeat ();
26455796c8dcSSimon Schubert }
26465796c8dcSSimon Schubert 
2647*ef5ccd6cSJohn Marino static void
agent_command_1(char * exp,int eval)2648*ef5ccd6cSJohn Marino agent_command_1 (char *exp, int eval)
2649*ef5ccd6cSJohn Marino {
2650*ef5ccd6cSJohn Marino   /* We don't deal with overlay debugging at the moment.  We need to
2651*ef5ccd6cSJohn Marino      think more carefully about this.  If you copy this code into
2652*ef5ccd6cSJohn Marino      another command, change the error message; the user shouldn't
2653*ef5ccd6cSJohn Marino      have to know anything about agent expressions.  */
2654*ef5ccd6cSJohn Marino   if (overlay_debugging)
2655*ef5ccd6cSJohn Marino     error (_("GDB can't do agent expression translation with overlays."));
2656*ef5ccd6cSJohn Marino 
2657*ef5ccd6cSJohn Marino   if (exp == 0)
2658*ef5ccd6cSJohn Marino     error_no_arg (_("expression to translate"));
2659*ef5ccd6cSJohn Marino 
2660*ef5ccd6cSJohn Marino   if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2661*ef5ccd6cSJohn Marino     {
2662*ef5ccd6cSJohn Marino       struct linespec_result canonical;
2663*ef5ccd6cSJohn Marino       int ix;
2664*ef5ccd6cSJohn Marino       struct linespec_sals *iter;
2665*ef5ccd6cSJohn Marino       struct cleanup *old_chain;
2666*ef5ccd6cSJohn Marino 
2667*ef5ccd6cSJohn Marino       exp = skip_spaces (exp);
2668*ef5ccd6cSJohn Marino       init_linespec_result (&canonical);
2669*ef5ccd6cSJohn Marino       decode_line_full (&exp, DECODE_LINE_FUNFIRSTLINE,
2670*ef5ccd6cSJohn Marino 			(struct symtab *) NULL, 0, &canonical,
2671*ef5ccd6cSJohn Marino 			NULL, NULL);
2672*ef5ccd6cSJohn Marino       old_chain = make_cleanup_destroy_linespec_result (&canonical);
2673*ef5ccd6cSJohn Marino       exp = skip_spaces (exp);
2674*ef5ccd6cSJohn Marino       if (exp[0] == ',')
2675*ef5ccd6cSJohn Marino         {
2676*ef5ccd6cSJohn Marino 	  exp++;
2677*ef5ccd6cSJohn Marino 	  exp = skip_spaces (exp);
2678*ef5ccd6cSJohn Marino 	}
2679*ef5ccd6cSJohn Marino       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
2680*ef5ccd6cSJohn Marino         {
2681*ef5ccd6cSJohn Marino 	  int i;
2682*ef5ccd6cSJohn Marino 
2683*ef5ccd6cSJohn Marino 	  for (i = 0; i < iter->sals.nelts; i++)
2684*ef5ccd6cSJohn Marino 	    agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
2685*ef5ccd6cSJohn Marino         }
2686*ef5ccd6cSJohn Marino       do_cleanups (old_chain);
2687*ef5ccd6cSJohn Marino     }
2688*ef5ccd6cSJohn Marino   else
2689*ef5ccd6cSJohn Marino     agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
2690*ef5ccd6cSJohn Marino 
2691*ef5ccd6cSJohn Marino   dont_repeat ();
2692*ef5ccd6cSJohn Marino }
2693*ef5ccd6cSJohn Marino 
2694*ef5ccd6cSJohn Marino static void
agent_command(char * exp,int from_tty)2695*ef5ccd6cSJohn Marino agent_command (char *exp, int from_tty)
2696*ef5ccd6cSJohn Marino {
2697*ef5ccd6cSJohn Marino   agent_command_1 (exp, 0);
2698*ef5ccd6cSJohn Marino }
2699*ef5ccd6cSJohn Marino 
27005796c8dcSSimon Schubert /* Parse the given expression, compile it into an agent expression
27015796c8dcSSimon Schubert    that does direct evaluation, and display the resulting
27025796c8dcSSimon Schubert    expression.  */
27035796c8dcSSimon Schubert 
27045796c8dcSSimon Schubert static void
agent_eval_command(char * exp,int from_tty)27055796c8dcSSimon Schubert agent_eval_command (char *exp, int from_tty)
27065796c8dcSSimon Schubert {
2707*ef5ccd6cSJohn Marino   agent_command_1 (exp, 1);
2708*ef5ccd6cSJohn Marino }
2709*ef5ccd6cSJohn Marino 
2710*ef5ccd6cSJohn Marino /* Parse the given expression, compile it into an agent expression
2711*ef5ccd6cSJohn Marino    that does a printf, and display the resulting expression.  */
2712*ef5ccd6cSJohn Marino 
2713*ef5ccd6cSJohn Marino static void
maint_agent_printf_command(char * exp,int from_tty)2714*ef5ccd6cSJohn Marino maint_agent_printf_command (char *exp, int from_tty)
2715*ef5ccd6cSJohn Marino {
27165796c8dcSSimon Schubert   struct cleanup *old_chain = 0;
27175796c8dcSSimon Schubert   struct expression *expr;
2718*ef5ccd6cSJohn Marino   struct expression *argvec[100];
27195796c8dcSSimon Schubert   struct agent_expr *agent;
27205796c8dcSSimon Schubert   struct frame_info *fi = get_current_frame ();	/* need current scope */
2721*ef5ccd6cSJohn Marino   const char *cmdrest;
2722*ef5ccd6cSJohn Marino   const char *format_start, *format_end;
2723*ef5ccd6cSJohn Marino   struct format_piece *fpieces;
2724*ef5ccd6cSJohn Marino   int nargs;
27255796c8dcSSimon Schubert 
27265796c8dcSSimon Schubert   /* We don't deal with overlay debugging at the moment.  We need to
27275796c8dcSSimon Schubert      think more carefully about this.  If you copy this code into
27285796c8dcSSimon Schubert      another command, change the error message; the user shouldn't
27295796c8dcSSimon Schubert      have to know anything about agent expressions.  */
27305796c8dcSSimon Schubert   if (overlay_debugging)
27315796c8dcSSimon Schubert     error (_("GDB can't do agent expression translation with overlays."));
27325796c8dcSSimon Schubert 
27335796c8dcSSimon Schubert   if (exp == 0)
27345796c8dcSSimon Schubert     error_no_arg (_("expression to translate"));
27355796c8dcSSimon Schubert 
2736*ef5ccd6cSJohn Marino   cmdrest = exp;
2737*ef5ccd6cSJohn Marino 
2738*ef5ccd6cSJohn Marino   cmdrest = skip_spaces_const (cmdrest);
2739*ef5ccd6cSJohn Marino 
2740*ef5ccd6cSJohn Marino   if (*cmdrest++ != '"')
2741*ef5ccd6cSJohn Marino     error (_("Must start with a format string."));
2742*ef5ccd6cSJohn Marino 
2743*ef5ccd6cSJohn Marino   format_start = cmdrest;
2744*ef5ccd6cSJohn Marino 
2745*ef5ccd6cSJohn Marino   fpieces = parse_format_string (&cmdrest);
2746*ef5ccd6cSJohn Marino 
2747*ef5ccd6cSJohn Marino   old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2748*ef5ccd6cSJohn Marino 
2749*ef5ccd6cSJohn Marino   format_end = cmdrest;
2750*ef5ccd6cSJohn Marino 
2751*ef5ccd6cSJohn Marino   if (*cmdrest++ != '"')
2752*ef5ccd6cSJohn Marino     error (_("Bad format string, non-terminated '\"'."));
2753*ef5ccd6cSJohn Marino 
2754*ef5ccd6cSJohn Marino   cmdrest = skip_spaces_const (cmdrest);
2755*ef5ccd6cSJohn Marino 
2756*ef5ccd6cSJohn Marino   if (*cmdrest != ',' && *cmdrest != 0)
2757*ef5ccd6cSJohn Marino     error (_("Invalid argument syntax"));
2758*ef5ccd6cSJohn Marino 
2759*ef5ccd6cSJohn Marino   if (*cmdrest == ',')
2760*ef5ccd6cSJohn Marino     cmdrest++;
2761*ef5ccd6cSJohn Marino   cmdrest = skip_spaces_const (cmdrest);
2762*ef5ccd6cSJohn Marino 
2763*ef5ccd6cSJohn Marino   nargs = 0;
2764*ef5ccd6cSJohn Marino   while (*cmdrest != '\0')
2765*ef5ccd6cSJohn Marino     {
2766*ef5ccd6cSJohn Marino       const char *cmd1;
2767*ef5ccd6cSJohn Marino 
2768*ef5ccd6cSJohn Marino       cmd1 = cmdrest;
2769*ef5ccd6cSJohn Marino       expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2770*ef5ccd6cSJohn Marino       argvec[nargs] = expr;
2771*ef5ccd6cSJohn Marino       ++nargs;
2772*ef5ccd6cSJohn Marino       cmdrest = cmd1;
2773*ef5ccd6cSJohn Marino       if (*cmdrest == ',')
2774*ef5ccd6cSJohn Marino 	++cmdrest;
2775*ef5ccd6cSJohn Marino       /* else complain? */
2776*ef5ccd6cSJohn Marino     }
2777*ef5ccd6cSJohn Marino 
2778*ef5ccd6cSJohn Marino 
2779*ef5ccd6cSJohn Marino   agent = gen_printf (get_frame_pc (fi), get_current_arch (), 0, 0,
2780*ef5ccd6cSJohn Marino 		      format_start, format_end - format_start,
2781*ef5ccd6cSJohn Marino 		      fpieces, nargs, argvec);
27825796c8dcSSimon Schubert   make_cleanup_free_agent_expr (agent);
2783cf7f2e2dSJohn Marino   ax_reqs (agent);
27845796c8dcSSimon Schubert   ax_print (gdb_stdout, agent);
27855796c8dcSSimon Schubert 
27865796c8dcSSimon Schubert   /* It would be nice to call ax_reqs here to gather some general info
27875796c8dcSSimon Schubert      about the expression, and then print out the result.  */
27885796c8dcSSimon Schubert 
27895796c8dcSSimon Schubert   do_cleanups (old_chain);
27905796c8dcSSimon Schubert   dont_repeat ();
27915796c8dcSSimon Schubert }
27925796c8dcSSimon Schubert 
27935796c8dcSSimon Schubert 
27945796c8dcSSimon Schubert /* Initialization code.  */
27955796c8dcSSimon Schubert 
27965796c8dcSSimon Schubert void _initialize_ax_gdb (void);
27975796c8dcSSimon Schubert void
_initialize_ax_gdb(void)27985796c8dcSSimon Schubert _initialize_ax_gdb (void)
27995796c8dcSSimon Schubert {
28005796c8dcSSimon Schubert   add_cmd ("agent", class_maintenance, agent_command,
2801*ef5ccd6cSJohn Marino 	   _("\
2802*ef5ccd6cSJohn Marino Translate an expression into remote agent bytecode for tracing.\n\
2803*ef5ccd6cSJohn Marino Usage: maint agent [-at location,] EXPRESSION\n\
2804*ef5ccd6cSJohn Marino If -at is given, generate remote agent bytecode for this location.\n\
2805*ef5ccd6cSJohn Marino If not, generate remote agent bytecode for current frame pc address."),
28065796c8dcSSimon Schubert 	   &maintenancelist);
28075796c8dcSSimon Schubert 
28085796c8dcSSimon Schubert   add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2809*ef5ccd6cSJohn Marino 	   _("\
2810*ef5ccd6cSJohn Marino Translate an expression into remote agent bytecode for evaluation.\n\
2811*ef5ccd6cSJohn Marino Usage: maint agent-eval [-at location,] EXPRESSION\n\
2812*ef5ccd6cSJohn Marino If -at is given, generate remote agent bytecode for this location.\n\
2813*ef5ccd6cSJohn Marino If not, generate remote agent bytecode for current frame pc address."),
2814*ef5ccd6cSJohn Marino 	   &maintenancelist);
2815*ef5ccd6cSJohn Marino 
2816*ef5ccd6cSJohn Marino   add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2817c50c785cSJohn Marino 	   _("Translate an expression into remote "
2818*ef5ccd6cSJohn Marino 	     "agent bytecode for evaluation and display the bytecodes."),
28195796c8dcSSimon Schubert 	   &maintenancelist);
28205796c8dcSSimon Schubert }
2821