xref: /openbsd-src/gnu/usr.bin/binutils/gdb/ax-gdb.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1*b725ae77Skettenis /* GDB-specific functions for operating on agent expressions.
2*b725ae77Skettenis 
3*b725ae77Skettenis    Copyright 1998, 1999, 2000, 2001, 2003 Free Software Foundation,
4*b725ae77Skettenis    Inc.
5*b725ae77Skettenis 
6*b725ae77Skettenis    This file is part of GDB.
7*b725ae77Skettenis 
8*b725ae77Skettenis    This program is free software; you can redistribute it and/or modify
9*b725ae77Skettenis    it under the terms of the GNU General Public License as published by
10*b725ae77Skettenis    the Free Software Foundation; either version 2 of the License, or
11*b725ae77Skettenis    (at your option) any later version.
12*b725ae77Skettenis 
13*b725ae77Skettenis    This program is distributed in the hope that it will be useful,
14*b725ae77Skettenis    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*b725ae77Skettenis    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*b725ae77Skettenis    GNU General Public License for more details.
17*b725ae77Skettenis 
18*b725ae77Skettenis    You should have received a copy of the GNU General Public License
19*b725ae77Skettenis    along with this program; if not, write to the Free Software
20*b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
21*b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
22*b725ae77Skettenis 
23*b725ae77Skettenis #include "defs.h"
24*b725ae77Skettenis #include "symtab.h"
25*b725ae77Skettenis #include "symfile.h"
26*b725ae77Skettenis #include "gdbtypes.h"
27*b725ae77Skettenis #include "value.h"
28*b725ae77Skettenis #include "expression.h"
29*b725ae77Skettenis #include "command.h"
30*b725ae77Skettenis #include "gdbcmd.h"
31*b725ae77Skettenis #include "frame.h"
32*b725ae77Skettenis #include "target.h"
33*b725ae77Skettenis #include "ax.h"
34*b725ae77Skettenis #include "ax-gdb.h"
35*b725ae77Skettenis #include "gdb_string.h"
36*b725ae77Skettenis #include "block.h"
37*b725ae77Skettenis #include "regcache.h"
38*b725ae77Skettenis 
39*b725ae77Skettenis /* To make sense of this file, you should read doc/agentexpr.texi.
40*b725ae77Skettenis    Then look at the types and enums in ax-gdb.h.  For the code itself,
41*b725ae77Skettenis    look at gen_expr, towards the bottom; that's the main function that
42*b725ae77Skettenis    looks at the GDB expressions and calls everything else to generate
43*b725ae77Skettenis    code.
44*b725ae77Skettenis 
45*b725ae77Skettenis    I'm beginning to wonder whether it wouldn't be nicer to internally
46*b725ae77Skettenis    generate trees, with types, and then spit out the bytecode in
47*b725ae77Skettenis    linear form afterwards; we could generate fewer `swap', `ext', and
48*b725ae77Skettenis    `zero_ext' bytecodes that way; it would make good constant folding
49*b725ae77Skettenis    easier, too.  But at the moment, I think we should be willing to
50*b725ae77Skettenis    pay for the simplicity of this code with less-than-optimal bytecode
51*b725ae77Skettenis    strings.
52*b725ae77Skettenis 
53*b725ae77Skettenis    Remember, "GBD" stands for "Great Britain, Dammit!"  So be careful.  */
54*b725ae77Skettenis 
55*b725ae77Skettenis 
56*b725ae77Skettenis 
57*b725ae77Skettenis /* Prototypes for local functions. */
58*b725ae77Skettenis 
59*b725ae77Skettenis /* There's a standard order to the arguments of these functions:
60*b725ae77Skettenis    union exp_element ** --- pointer into expression
61*b725ae77Skettenis    struct agent_expr * --- agent expression buffer to generate code into
62*b725ae77Skettenis    struct axs_value * --- describes value left on top of stack  */
63*b725ae77Skettenis 
64*b725ae77Skettenis static struct value *const_var_ref (struct symbol *var);
65*b725ae77Skettenis static struct value *const_expr (union exp_element **pc);
66*b725ae77Skettenis static struct value *maybe_const_expr (union exp_element **pc);
67*b725ae77Skettenis 
68*b725ae77Skettenis static void gen_traced_pop (struct agent_expr *, struct axs_value *);
69*b725ae77Skettenis 
70*b725ae77Skettenis static void gen_sign_extend (struct agent_expr *, struct type *);
71*b725ae77Skettenis static void gen_extend (struct agent_expr *, struct type *);
72*b725ae77Skettenis static void gen_fetch (struct agent_expr *, struct type *);
73*b725ae77Skettenis static void gen_left_shift (struct agent_expr *, int);
74*b725ae77Skettenis 
75*b725ae77Skettenis 
76*b725ae77Skettenis static void gen_frame_args_address (struct agent_expr *);
77*b725ae77Skettenis static void gen_frame_locals_address (struct agent_expr *);
78*b725ae77Skettenis static void gen_offset (struct agent_expr *ax, int offset);
79*b725ae77Skettenis static void gen_sym_offset (struct agent_expr *, struct symbol *);
80*b725ae77Skettenis static void gen_var_ref (struct agent_expr *ax,
81*b725ae77Skettenis 			 struct axs_value *value, struct symbol *var);
82*b725ae77Skettenis 
83*b725ae77Skettenis 
84*b725ae77Skettenis static void gen_int_literal (struct agent_expr *ax,
85*b725ae77Skettenis 			     struct axs_value *value,
86*b725ae77Skettenis 			     LONGEST k, struct type *type);
87*b725ae77Skettenis 
88*b725ae77Skettenis 
89*b725ae77Skettenis static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
90*b725ae77Skettenis static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
91*b725ae77Skettenis static int type_wider_than (struct type *type1, struct type *type2);
92*b725ae77Skettenis static struct type *max_type (struct type *type1, struct type *type2);
93*b725ae77Skettenis static void gen_conversion (struct agent_expr *ax,
94*b725ae77Skettenis 			    struct type *from, struct type *to);
95*b725ae77Skettenis static int is_nontrivial_conversion (struct type *from, struct type *to);
96*b725ae77Skettenis static void gen_usual_arithmetic (struct agent_expr *ax,
97*b725ae77Skettenis 				  struct axs_value *value1,
98*b725ae77Skettenis 				  struct axs_value *value2);
99*b725ae77Skettenis static void gen_integral_promotions (struct agent_expr *ax,
100*b725ae77Skettenis 				     struct axs_value *value);
101*b725ae77Skettenis static void gen_cast (struct agent_expr *ax,
102*b725ae77Skettenis 		      struct axs_value *value, struct type *type);
103*b725ae77Skettenis static void gen_scale (struct agent_expr *ax,
104*b725ae77Skettenis 		       enum agent_op op, struct type *type);
105*b725ae77Skettenis static void gen_add (struct agent_expr *ax,
106*b725ae77Skettenis 		     struct axs_value *value,
107*b725ae77Skettenis 		     struct axs_value *value1,
108*b725ae77Skettenis 		     struct axs_value *value2, char *name);
109*b725ae77Skettenis static void gen_sub (struct agent_expr *ax,
110*b725ae77Skettenis 		     struct axs_value *value,
111*b725ae77Skettenis 		     struct axs_value *value1, struct axs_value *value2);
112*b725ae77Skettenis static void gen_binop (struct agent_expr *ax,
113*b725ae77Skettenis 		       struct axs_value *value,
114*b725ae77Skettenis 		       struct axs_value *value1,
115*b725ae77Skettenis 		       struct axs_value *value2,
116*b725ae77Skettenis 		       enum agent_op op,
117*b725ae77Skettenis 		       enum agent_op op_unsigned, int may_carry, char *name);
118*b725ae77Skettenis static void gen_logical_not (struct agent_expr *ax, struct axs_value *value);
119*b725ae77Skettenis static void gen_complement (struct agent_expr *ax, struct axs_value *value);
120*b725ae77Skettenis static void gen_deref (struct agent_expr *, struct axs_value *);
121*b725ae77Skettenis static void gen_address_of (struct agent_expr *, struct axs_value *);
122*b725ae77Skettenis static int find_field (struct type *type, char *name);
123*b725ae77Skettenis static void gen_bitfield_ref (struct agent_expr *ax,
124*b725ae77Skettenis 			      struct axs_value *value,
125*b725ae77Skettenis 			      struct type *type, int start, int end);
126*b725ae77Skettenis static void gen_struct_ref (struct agent_expr *ax,
127*b725ae77Skettenis 			    struct axs_value *value,
128*b725ae77Skettenis 			    char *field,
129*b725ae77Skettenis 			    char *operator_name, char *operand_name);
130*b725ae77Skettenis static void gen_repeat (union exp_element **pc,
131*b725ae77Skettenis 			struct agent_expr *ax, struct axs_value *value);
132*b725ae77Skettenis static void gen_sizeof (union exp_element **pc,
133*b725ae77Skettenis 			struct agent_expr *ax, struct axs_value *value);
134*b725ae77Skettenis static void gen_expr (union exp_element **pc,
135*b725ae77Skettenis 		      struct agent_expr *ax, struct axs_value *value);
136*b725ae77Skettenis 
137*b725ae77Skettenis static void agent_command (char *exp, int from_tty);
138*b725ae77Skettenis 
139*b725ae77Skettenis 
140*b725ae77Skettenis /* Detecting constant expressions.  */
141*b725ae77Skettenis 
142*b725ae77Skettenis /* If the variable reference at *PC is a constant, return its value.
143*b725ae77Skettenis    Otherwise, return zero.
144*b725ae77Skettenis 
145*b725ae77Skettenis    Hey, Wally!  How can a variable reference be a constant?
146*b725ae77Skettenis 
147*b725ae77Skettenis    Well, Beav, this function really handles the OP_VAR_VALUE operator,
148*b725ae77Skettenis    not specifically variable references.  GDB uses OP_VAR_VALUE to
149*b725ae77Skettenis    refer to any kind of symbolic reference: function names, enum
150*b725ae77Skettenis    elements, and goto labels are all handled through the OP_VAR_VALUE
151*b725ae77Skettenis    operator, even though they're constants.  It makes sense given the
152*b725ae77Skettenis    situation.
153*b725ae77Skettenis 
154*b725ae77Skettenis    Gee, Wally, don'cha wonder sometimes if data representations that
155*b725ae77Skettenis    subvert commonly accepted definitions of terms in favor of heavily
156*b725ae77Skettenis    context-specific interpretations are really just a tool of the
157*b725ae77Skettenis    programming hegemony to preserve their power and exclude the
158*b725ae77Skettenis    proletariat?  */
159*b725ae77Skettenis 
160*b725ae77Skettenis static struct value *
const_var_ref(struct symbol * var)161*b725ae77Skettenis const_var_ref (struct symbol *var)
162*b725ae77Skettenis {
163*b725ae77Skettenis   struct type *type = SYMBOL_TYPE (var);
164*b725ae77Skettenis 
165*b725ae77Skettenis   switch (SYMBOL_CLASS (var))
166*b725ae77Skettenis     {
167*b725ae77Skettenis     case LOC_CONST:
168*b725ae77Skettenis       return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
169*b725ae77Skettenis 
170*b725ae77Skettenis     case LOC_LABEL:
171*b725ae77Skettenis       return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
172*b725ae77Skettenis 
173*b725ae77Skettenis     default:
174*b725ae77Skettenis       return 0;
175*b725ae77Skettenis     }
176*b725ae77Skettenis }
177*b725ae77Skettenis 
178*b725ae77Skettenis 
179*b725ae77Skettenis /* If the expression starting at *PC has a constant value, return it.
180*b725ae77Skettenis    Otherwise, return zero.  If we return a value, then *PC will be
181*b725ae77Skettenis    advanced to the end of it.  If we return zero, *PC could be
182*b725ae77Skettenis    anywhere.  */
183*b725ae77Skettenis static struct value *
const_expr(union exp_element ** pc)184*b725ae77Skettenis const_expr (union exp_element **pc)
185*b725ae77Skettenis {
186*b725ae77Skettenis   enum exp_opcode op = (*pc)->opcode;
187*b725ae77Skettenis   struct value *v1;
188*b725ae77Skettenis 
189*b725ae77Skettenis   switch (op)
190*b725ae77Skettenis     {
191*b725ae77Skettenis     case OP_LONG:
192*b725ae77Skettenis       {
193*b725ae77Skettenis 	struct type *type = (*pc)[1].type;
194*b725ae77Skettenis 	LONGEST k = (*pc)[2].longconst;
195*b725ae77Skettenis 	(*pc) += 4;
196*b725ae77Skettenis 	return value_from_longest (type, k);
197*b725ae77Skettenis       }
198*b725ae77Skettenis 
199*b725ae77Skettenis     case OP_VAR_VALUE:
200*b725ae77Skettenis       {
201*b725ae77Skettenis 	struct value *v = const_var_ref ((*pc)[2].symbol);
202*b725ae77Skettenis 	(*pc) += 4;
203*b725ae77Skettenis 	return v;
204*b725ae77Skettenis       }
205*b725ae77Skettenis 
206*b725ae77Skettenis       /* We could add more operators in here.  */
207*b725ae77Skettenis 
208*b725ae77Skettenis     case UNOP_NEG:
209*b725ae77Skettenis       (*pc)++;
210*b725ae77Skettenis       v1 = const_expr (pc);
211*b725ae77Skettenis       if (v1)
212*b725ae77Skettenis 	return value_neg (v1);
213*b725ae77Skettenis       else
214*b725ae77Skettenis 	return 0;
215*b725ae77Skettenis 
216*b725ae77Skettenis     default:
217*b725ae77Skettenis       return 0;
218*b725ae77Skettenis     }
219*b725ae77Skettenis }
220*b725ae77Skettenis 
221*b725ae77Skettenis 
222*b725ae77Skettenis /* Like const_expr, but guarantee also that *PC is undisturbed if the
223*b725ae77Skettenis    expression is not constant.  */
224*b725ae77Skettenis static struct value *
maybe_const_expr(union exp_element ** pc)225*b725ae77Skettenis maybe_const_expr (union exp_element **pc)
226*b725ae77Skettenis {
227*b725ae77Skettenis   union exp_element *tentative_pc = *pc;
228*b725ae77Skettenis   struct value *v = const_expr (&tentative_pc);
229*b725ae77Skettenis 
230*b725ae77Skettenis   /* If we got a value, then update the real PC.  */
231*b725ae77Skettenis   if (v)
232*b725ae77Skettenis     *pc = tentative_pc;
233*b725ae77Skettenis 
234*b725ae77Skettenis   return v;
235*b725ae77Skettenis }
236*b725ae77Skettenis 
237*b725ae77Skettenis 
238*b725ae77Skettenis /* Generating bytecode from GDB expressions: general assumptions */
239*b725ae77Skettenis 
240*b725ae77Skettenis /* Here are a few general assumptions made throughout the code; if you
241*b725ae77Skettenis    want to make a change that contradicts one of these, then you'd
242*b725ae77Skettenis    better scan things pretty thoroughly.
243*b725ae77Skettenis 
244*b725ae77Skettenis    - We assume that all values occupy one stack element.  For example,
245*b725ae77Skettenis    sometimes we'll swap to get at the left argument to a binary
246*b725ae77Skettenis    operator.  If we decide that void values should occupy no stack
247*b725ae77Skettenis    elements, or that synthetic arrays (whose size is determined at
248*b725ae77Skettenis    run time, created by the `@' operator) should occupy two stack
249*b725ae77Skettenis    elements (address and length), then this will cause trouble.
250*b725ae77Skettenis 
251*b725ae77Skettenis    - We assume the stack elements are infinitely wide, and that we
252*b725ae77Skettenis    don't have to worry what happens if the user requests an
253*b725ae77Skettenis    operation that is wider than the actual interpreter's stack.
254*b725ae77Skettenis    That is, it's up to the interpreter to handle directly all the
255*b725ae77Skettenis    integer widths the user has access to.  (Woe betide the language
256*b725ae77Skettenis    with bignums!)
257*b725ae77Skettenis 
258*b725ae77Skettenis    - We don't support side effects.  Thus, we don't have to worry about
259*b725ae77Skettenis    GCC's generalized lvalues, function calls, etc.
260*b725ae77Skettenis 
261*b725ae77Skettenis    - We don't support floating point.  Many places where we switch on
262*b725ae77Skettenis    some type don't bother to include cases for floating point; there
263*b725ae77Skettenis    may be even more subtle ways this assumption exists.  For
264*b725ae77Skettenis    example, the arguments to % must be integers.
265*b725ae77Skettenis 
266*b725ae77Skettenis    - We assume all subexpressions have a static, unchanging type.  If
267*b725ae77Skettenis    we tried to support convenience variables, this would be a
268*b725ae77Skettenis    problem.
269*b725ae77Skettenis 
270*b725ae77Skettenis    - All values on the stack should always be fully zero- or
271*b725ae77Skettenis    sign-extended.
272*b725ae77Skettenis 
273*b725ae77Skettenis    (I wasn't sure whether to choose this or its opposite --- that
274*b725ae77Skettenis    only addresses are assumed extended --- but it turns out that
275*b725ae77Skettenis    neither convention completely eliminates spurious extend
276*b725ae77Skettenis    operations (if everything is always extended, then you have to
277*b725ae77Skettenis    extend after add, because it could overflow; if nothing is
278*b725ae77Skettenis    extended, then you end up producing extends whenever you change
279*b725ae77Skettenis    sizes), and this is simpler.)  */
280*b725ae77Skettenis 
281*b725ae77Skettenis 
282*b725ae77Skettenis /* Generating bytecode from GDB expressions: the `trace' kludge  */
283*b725ae77Skettenis 
284*b725ae77Skettenis /* The compiler in this file is a general-purpose mechanism for
285*b725ae77Skettenis    translating GDB expressions into bytecode.  One ought to be able to
286*b725ae77Skettenis    find a million and one uses for it.
287*b725ae77Skettenis 
288*b725ae77Skettenis    However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
289*b725ae77Skettenis    of expediency.  Let he who is without sin cast the first stone.
290*b725ae77Skettenis 
291*b725ae77Skettenis    For the data tracing facility, we need to insert `trace' bytecodes
292*b725ae77Skettenis    before each data fetch; this records all the memory that the
293*b725ae77Skettenis    expression touches in the course of evaluation, so that memory will
294*b725ae77Skettenis    be available when the user later tries to evaluate the expression
295*b725ae77Skettenis    in GDB.
296*b725ae77Skettenis 
297*b725ae77Skettenis    This should be done (I think) in a post-processing pass, that walks
298*b725ae77Skettenis    an arbitrary agent expression and inserts `trace' operations at the
299*b725ae77Skettenis    appropriate points.  But it's much faster to just hack them
300*b725ae77Skettenis    directly into the code.  And since we're in a crunch, that's what
301*b725ae77Skettenis    I've done.
302*b725ae77Skettenis 
303*b725ae77Skettenis    Setting the flag trace_kludge to non-zero enables the code that
304*b725ae77Skettenis    emits the trace bytecodes at the appropriate points.  */
305*b725ae77Skettenis static int trace_kludge;
306*b725ae77Skettenis 
307*b725ae77Skettenis /* Trace the lvalue on the stack, if it needs it.  In either case, pop
308*b725ae77Skettenis    the value.  Useful on the left side of a comma, and at the end of
309*b725ae77Skettenis    an expression being used for tracing.  */
310*b725ae77Skettenis static void
gen_traced_pop(struct agent_expr * ax,struct axs_value * value)311*b725ae77Skettenis gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
312*b725ae77Skettenis {
313*b725ae77Skettenis   if (trace_kludge)
314*b725ae77Skettenis     switch (value->kind)
315*b725ae77Skettenis       {
316*b725ae77Skettenis       case axs_rvalue:
317*b725ae77Skettenis 	/* We don't trace rvalues, just the lvalues necessary to
318*b725ae77Skettenis 	   produce them.  So just dispose of this value.  */
319*b725ae77Skettenis 	ax_simple (ax, aop_pop);
320*b725ae77Skettenis 	break;
321*b725ae77Skettenis 
322*b725ae77Skettenis       case axs_lvalue_memory:
323*b725ae77Skettenis 	{
324*b725ae77Skettenis 	  int length = TYPE_LENGTH (value->type);
325*b725ae77Skettenis 
326*b725ae77Skettenis 	  /* There's no point in trying to use a trace_quick bytecode
327*b725ae77Skettenis 	     here, since "trace_quick SIZE pop" is three bytes, whereas
328*b725ae77Skettenis 	     "const8 SIZE trace" is also three bytes, does the same
329*b725ae77Skettenis 	     thing, and the simplest code which generates that will also
330*b725ae77Skettenis 	     work correctly for objects with large sizes.  */
331*b725ae77Skettenis 	  ax_const_l (ax, length);
332*b725ae77Skettenis 	  ax_simple (ax, aop_trace);
333*b725ae77Skettenis 	}
334*b725ae77Skettenis 	break;
335*b725ae77Skettenis 
336*b725ae77Skettenis       case axs_lvalue_register:
337*b725ae77Skettenis 	/* We need to mention the register somewhere in the bytecode,
338*b725ae77Skettenis 	   so ax_reqs will pick it up and add it to the mask of
339*b725ae77Skettenis 	   registers used.  */
340*b725ae77Skettenis 	ax_reg (ax, value->u.reg);
341*b725ae77Skettenis 	ax_simple (ax, aop_pop);
342*b725ae77Skettenis 	break;
343*b725ae77Skettenis       }
344*b725ae77Skettenis   else
345*b725ae77Skettenis     /* If we're not tracing, just pop the value.  */
346*b725ae77Skettenis     ax_simple (ax, aop_pop);
347*b725ae77Skettenis }
348*b725ae77Skettenis 
349*b725ae77Skettenis 
350*b725ae77Skettenis 
351*b725ae77Skettenis /* Generating bytecode from GDB expressions: helper functions */
352*b725ae77Skettenis 
353*b725ae77Skettenis /* Assume that the lower bits of the top of the stack is a value of
354*b725ae77Skettenis    type TYPE, and the upper bits are zero.  Sign-extend if necessary.  */
355*b725ae77Skettenis static void
gen_sign_extend(struct agent_expr * ax,struct type * type)356*b725ae77Skettenis gen_sign_extend (struct agent_expr *ax, struct type *type)
357*b725ae77Skettenis {
358*b725ae77Skettenis   /* Do we need to sign-extend this?  */
359*b725ae77Skettenis   if (!TYPE_UNSIGNED (type))
360*b725ae77Skettenis     ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
361*b725ae77Skettenis }
362*b725ae77Skettenis 
363*b725ae77Skettenis 
364*b725ae77Skettenis /* Assume the lower bits of the top of the stack hold a value of type
365*b725ae77Skettenis    TYPE, and the upper bits are garbage.  Sign-extend or truncate as
366*b725ae77Skettenis    needed.  */
367*b725ae77Skettenis static void
gen_extend(struct agent_expr * ax,struct type * type)368*b725ae77Skettenis gen_extend (struct agent_expr *ax, struct type *type)
369*b725ae77Skettenis {
370*b725ae77Skettenis   int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
371*b725ae77Skettenis   /* I just had to.  */
372*b725ae77Skettenis   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
373*b725ae77Skettenis }
374*b725ae77Skettenis 
375*b725ae77Skettenis 
376*b725ae77Skettenis /* Assume that the top of the stack contains a value of type "pointer
377*b725ae77Skettenis    to TYPE"; generate code to fetch its value.  Note that TYPE is the
378*b725ae77Skettenis    target type, not the pointer type.  */
379*b725ae77Skettenis static void
gen_fetch(struct agent_expr * ax,struct type * type)380*b725ae77Skettenis gen_fetch (struct agent_expr *ax, struct type *type)
381*b725ae77Skettenis {
382*b725ae77Skettenis   if (trace_kludge)
383*b725ae77Skettenis     {
384*b725ae77Skettenis       /* Record the area of memory we're about to fetch.  */
385*b725ae77Skettenis       ax_trace_quick (ax, TYPE_LENGTH (type));
386*b725ae77Skettenis     }
387*b725ae77Skettenis 
388*b725ae77Skettenis   switch (TYPE_CODE (type))
389*b725ae77Skettenis     {
390*b725ae77Skettenis     case TYPE_CODE_PTR:
391*b725ae77Skettenis     case TYPE_CODE_ENUM:
392*b725ae77Skettenis     case TYPE_CODE_INT:
393*b725ae77Skettenis     case TYPE_CODE_CHAR:
394*b725ae77Skettenis       /* It's a scalar value, so we know how to dereference it.  How
395*b725ae77Skettenis          many bytes long is it?  */
396*b725ae77Skettenis       switch (TYPE_LENGTH (type))
397*b725ae77Skettenis 	{
398*b725ae77Skettenis 	case 8 / TARGET_CHAR_BIT:
399*b725ae77Skettenis 	  ax_simple (ax, aop_ref8);
400*b725ae77Skettenis 	  break;
401*b725ae77Skettenis 	case 16 / TARGET_CHAR_BIT:
402*b725ae77Skettenis 	  ax_simple (ax, aop_ref16);
403*b725ae77Skettenis 	  break;
404*b725ae77Skettenis 	case 32 / TARGET_CHAR_BIT:
405*b725ae77Skettenis 	  ax_simple (ax, aop_ref32);
406*b725ae77Skettenis 	  break;
407*b725ae77Skettenis 	case 64 / TARGET_CHAR_BIT:
408*b725ae77Skettenis 	  ax_simple (ax, aop_ref64);
409*b725ae77Skettenis 	  break;
410*b725ae77Skettenis 
411*b725ae77Skettenis 	  /* Either our caller shouldn't have asked us to dereference
412*b725ae77Skettenis 	     that pointer (other code's fault), or we're not
413*b725ae77Skettenis 	     implementing something we should be (this code's fault).
414*b725ae77Skettenis 	     In any case, it's a bug the user shouldn't see.  */
415*b725ae77Skettenis 	default:
416*b725ae77Skettenis 	  internal_error (__FILE__, __LINE__,
417*b725ae77Skettenis 			  "gen_fetch: strange size");
418*b725ae77Skettenis 	}
419*b725ae77Skettenis 
420*b725ae77Skettenis       gen_sign_extend (ax, type);
421*b725ae77Skettenis       break;
422*b725ae77Skettenis 
423*b725ae77Skettenis     default:
424*b725ae77Skettenis       /* Either our caller shouldn't have asked us to dereference that
425*b725ae77Skettenis          pointer (other code's fault), or we're not implementing
426*b725ae77Skettenis          something we should be (this code's fault).  In any case,
427*b725ae77Skettenis          it's a bug the user shouldn't see.  */
428*b725ae77Skettenis       internal_error (__FILE__, __LINE__,
429*b725ae77Skettenis 		      "gen_fetch: bad type code");
430*b725ae77Skettenis     }
431*b725ae77Skettenis }
432*b725ae77Skettenis 
433*b725ae77Skettenis 
434*b725ae77Skettenis /* Generate code to left shift the top of the stack by DISTANCE bits, or
435*b725ae77Skettenis    right shift it by -DISTANCE bits if DISTANCE < 0.  This generates
436*b725ae77Skettenis    unsigned (logical) right shifts.  */
437*b725ae77Skettenis static void
gen_left_shift(struct agent_expr * ax,int distance)438*b725ae77Skettenis gen_left_shift (struct agent_expr *ax, int distance)
439*b725ae77Skettenis {
440*b725ae77Skettenis   if (distance > 0)
441*b725ae77Skettenis     {
442*b725ae77Skettenis       ax_const_l (ax, distance);
443*b725ae77Skettenis       ax_simple (ax, aop_lsh);
444*b725ae77Skettenis     }
445*b725ae77Skettenis   else if (distance < 0)
446*b725ae77Skettenis     {
447*b725ae77Skettenis       ax_const_l (ax, -distance);
448*b725ae77Skettenis       ax_simple (ax, aop_rsh_unsigned);
449*b725ae77Skettenis     }
450*b725ae77Skettenis }
451*b725ae77Skettenis 
452*b725ae77Skettenis 
453*b725ae77Skettenis 
454*b725ae77Skettenis /* Generating bytecode from GDB expressions: symbol references */
455*b725ae77Skettenis 
456*b725ae77Skettenis /* Generate code to push the base address of the argument portion of
457*b725ae77Skettenis    the top stack frame.  */
458*b725ae77Skettenis static void
gen_frame_args_address(struct agent_expr * ax)459*b725ae77Skettenis gen_frame_args_address (struct agent_expr *ax)
460*b725ae77Skettenis {
461*b725ae77Skettenis   int frame_reg;
462*b725ae77Skettenis   LONGEST frame_offset;
463*b725ae77Skettenis 
464*b725ae77Skettenis   TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
465*b725ae77Skettenis   ax_reg (ax, frame_reg);
466*b725ae77Skettenis   gen_offset (ax, frame_offset);
467*b725ae77Skettenis }
468*b725ae77Skettenis 
469*b725ae77Skettenis 
470*b725ae77Skettenis /* Generate code to push the base address of the locals portion of the
471*b725ae77Skettenis    top stack frame.  */
472*b725ae77Skettenis static void
gen_frame_locals_address(struct agent_expr * ax)473*b725ae77Skettenis gen_frame_locals_address (struct agent_expr *ax)
474*b725ae77Skettenis {
475*b725ae77Skettenis   int frame_reg;
476*b725ae77Skettenis   LONGEST frame_offset;
477*b725ae77Skettenis 
478*b725ae77Skettenis   TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
479*b725ae77Skettenis   ax_reg (ax, frame_reg);
480*b725ae77Skettenis   gen_offset (ax, frame_offset);
481*b725ae77Skettenis }
482*b725ae77Skettenis 
483*b725ae77Skettenis 
484*b725ae77Skettenis /* Generate code to add OFFSET to the top of the stack.  Try to
485*b725ae77Skettenis    generate short and readable code.  We use this for getting to
486*b725ae77Skettenis    variables on the stack, and structure members.  If we were
487*b725ae77Skettenis    programming in ML, it would be clearer why these are the same
488*b725ae77Skettenis    thing.  */
489*b725ae77Skettenis static void
gen_offset(struct agent_expr * ax,int offset)490*b725ae77Skettenis gen_offset (struct agent_expr *ax, int offset)
491*b725ae77Skettenis {
492*b725ae77Skettenis   /* It would suffice to simply push the offset and add it, but this
493*b725ae77Skettenis      makes it easier to read positive and negative offsets in the
494*b725ae77Skettenis      bytecode.  */
495*b725ae77Skettenis   if (offset > 0)
496*b725ae77Skettenis     {
497*b725ae77Skettenis       ax_const_l (ax, offset);
498*b725ae77Skettenis       ax_simple (ax, aop_add);
499*b725ae77Skettenis     }
500*b725ae77Skettenis   else if (offset < 0)
501*b725ae77Skettenis     {
502*b725ae77Skettenis       ax_const_l (ax, -offset);
503*b725ae77Skettenis       ax_simple (ax, aop_sub);
504*b725ae77Skettenis     }
505*b725ae77Skettenis }
506*b725ae77Skettenis 
507*b725ae77Skettenis 
508*b725ae77Skettenis /* In many cases, a symbol's value is the offset from some other
509*b725ae77Skettenis    address (stack frame, base register, etc.)  Generate code to add
510*b725ae77Skettenis    VAR's value to the top of the stack.  */
511*b725ae77Skettenis static void
gen_sym_offset(struct agent_expr * ax,struct symbol * var)512*b725ae77Skettenis gen_sym_offset (struct agent_expr *ax, struct symbol *var)
513*b725ae77Skettenis {
514*b725ae77Skettenis   gen_offset (ax, SYMBOL_VALUE (var));
515*b725ae77Skettenis }
516*b725ae77Skettenis 
517*b725ae77Skettenis 
518*b725ae77Skettenis /* Generate code for a variable reference to AX.  The variable is the
519*b725ae77Skettenis    symbol VAR.  Set VALUE to describe the result.  */
520*b725ae77Skettenis 
521*b725ae77Skettenis static void
gen_var_ref(struct agent_expr * ax,struct axs_value * value,struct symbol * var)522*b725ae77Skettenis gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
523*b725ae77Skettenis {
524*b725ae77Skettenis   /* Dereference any typedefs. */
525*b725ae77Skettenis   value->type = check_typedef (SYMBOL_TYPE (var));
526*b725ae77Skettenis 
527*b725ae77Skettenis   /* I'm imitating the code in read_var_value.  */
528*b725ae77Skettenis   switch (SYMBOL_CLASS (var))
529*b725ae77Skettenis     {
530*b725ae77Skettenis     case LOC_CONST:		/* A constant, like an enum value.  */
531*b725ae77Skettenis       ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
532*b725ae77Skettenis       value->kind = axs_rvalue;
533*b725ae77Skettenis       break;
534*b725ae77Skettenis 
535*b725ae77Skettenis     case LOC_LABEL:		/* A goto label, being used as a value.  */
536*b725ae77Skettenis       ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
537*b725ae77Skettenis       value->kind = axs_rvalue;
538*b725ae77Skettenis       break;
539*b725ae77Skettenis 
540*b725ae77Skettenis     case LOC_CONST_BYTES:
541*b725ae77Skettenis       internal_error (__FILE__, __LINE__,
542*b725ae77Skettenis 		      "gen_var_ref: LOC_CONST_BYTES symbols are not supported");
543*b725ae77Skettenis 
544*b725ae77Skettenis       /* Variable at a fixed location in memory.  Easy.  */
545*b725ae77Skettenis     case LOC_STATIC:
546*b725ae77Skettenis       /* Push the address of the variable.  */
547*b725ae77Skettenis       ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
548*b725ae77Skettenis       value->kind = axs_lvalue_memory;
549*b725ae77Skettenis       break;
550*b725ae77Skettenis 
551*b725ae77Skettenis     case LOC_ARG:		/* var lives in argument area of frame */
552*b725ae77Skettenis       gen_frame_args_address (ax);
553*b725ae77Skettenis       gen_sym_offset (ax, var);
554*b725ae77Skettenis       value->kind = axs_lvalue_memory;
555*b725ae77Skettenis       break;
556*b725ae77Skettenis 
557*b725ae77Skettenis     case LOC_REF_ARG:		/* As above, but the frame slot really
558*b725ae77Skettenis 				   holds the address of the variable.  */
559*b725ae77Skettenis       gen_frame_args_address (ax);
560*b725ae77Skettenis       gen_sym_offset (ax, var);
561*b725ae77Skettenis       /* Don't assume any particular pointer size.  */
562*b725ae77Skettenis       gen_fetch (ax, lookup_pointer_type (builtin_type_void));
563*b725ae77Skettenis       value->kind = axs_lvalue_memory;
564*b725ae77Skettenis       break;
565*b725ae77Skettenis 
566*b725ae77Skettenis     case LOC_LOCAL:		/* var lives in locals area of frame */
567*b725ae77Skettenis     case LOC_LOCAL_ARG:
568*b725ae77Skettenis       gen_frame_locals_address (ax);
569*b725ae77Skettenis       gen_sym_offset (ax, var);
570*b725ae77Skettenis       value->kind = axs_lvalue_memory;
571*b725ae77Skettenis       break;
572*b725ae77Skettenis 
573*b725ae77Skettenis     case LOC_BASEREG:		/* relative to some base register */
574*b725ae77Skettenis     case LOC_BASEREG_ARG:
575*b725ae77Skettenis       ax_reg (ax, SYMBOL_BASEREG (var));
576*b725ae77Skettenis       gen_sym_offset (ax, var);
577*b725ae77Skettenis       value->kind = axs_lvalue_memory;
578*b725ae77Skettenis       break;
579*b725ae77Skettenis 
580*b725ae77Skettenis     case LOC_TYPEDEF:
581*b725ae77Skettenis       error ("Cannot compute value of typedef `%s'.",
582*b725ae77Skettenis 	     SYMBOL_PRINT_NAME (var));
583*b725ae77Skettenis       break;
584*b725ae77Skettenis 
585*b725ae77Skettenis     case LOC_BLOCK:
586*b725ae77Skettenis       ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
587*b725ae77Skettenis       value->kind = axs_rvalue;
588*b725ae77Skettenis       break;
589*b725ae77Skettenis 
590*b725ae77Skettenis     case LOC_REGISTER:
591*b725ae77Skettenis     case LOC_REGPARM:
592*b725ae77Skettenis       /* Don't generate any code at all; in the process of treating
593*b725ae77Skettenis          this as an lvalue or rvalue, the caller will generate the
594*b725ae77Skettenis          right code.  */
595*b725ae77Skettenis       value->kind = axs_lvalue_register;
596*b725ae77Skettenis       value->u.reg = SYMBOL_VALUE (var);
597*b725ae77Skettenis       break;
598*b725ae77Skettenis 
599*b725ae77Skettenis       /* A lot like LOC_REF_ARG, but the pointer lives directly in a
600*b725ae77Skettenis          register, not on the stack.  Simpler than LOC_REGISTER and
601*b725ae77Skettenis          LOC_REGPARM, because it's just like any other case where the
602*b725ae77Skettenis          thing has a real address.  */
603*b725ae77Skettenis     case LOC_REGPARM_ADDR:
604*b725ae77Skettenis       ax_reg (ax, SYMBOL_VALUE (var));
605*b725ae77Skettenis       value->kind = axs_lvalue_memory;
606*b725ae77Skettenis       break;
607*b725ae77Skettenis 
608*b725ae77Skettenis     case LOC_UNRESOLVED:
609*b725ae77Skettenis       {
610*b725ae77Skettenis 	struct minimal_symbol *msym
611*b725ae77Skettenis 	= lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (var), NULL, NULL);
612*b725ae77Skettenis 	if (!msym)
613*b725ae77Skettenis 	  error ("Couldn't resolve symbol `%s'.", SYMBOL_PRINT_NAME (var));
614*b725ae77Skettenis 
615*b725ae77Skettenis 	/* Push the address of the variable.  */
616*b725ae77Skettenis 	ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
617*b725ae77Skettenis 	value->kind = axs_lvalue_memory;
618*b725ae77Skettenis       }
619*b725ae77Skettenis       break;
620*b725ae77Skettenis 
621*b725ae77Skettenis     case LOC_COMPUTED:
622*b725ae77Skettenis     case LOC_COMPUTED_ARG:
623*b725ae77Skettenis       /* FIXME: cagney/2004-01-26: It should be possible to
624*b725ae77Skettenis 	 unconditionally call the SYMBOL_OPS method when available.
625*b725ae77Skettenis 	 Unfortunately DWARF 2 stores the frame-base (instead of the
626*b725ae77Skettenis 	 function) location in a function's symbol.  Oops!  For the
627*b725ae77Skettenis 	 moment enable this when/where applicable.  */
628*b725ae77Skettenis       SYMBOL_OPS (var)->tracepoint_var_ref (var, ax, value);
629*b725ae77Skettenis       break;
630*b725ae77Skettenis 
631*b725ae77Skettenis     case LOC_OPTIMIZED_OUT:
632*b725ae77Skettenis       error ("The variable `%s' has been optimized out.",
633*b725ae77Skettenis 	     SYMBOL_PRINT_NAME (var));
634*b725ae77Skettenis       break;
635*b725ae77Skettenis 
636*b725ae77Skettenis     default:
637*b725ae77Skettenis       error ("Cannot find value of botched symbol `%s'.",
638*b725ae77Skettenis 	     SYMBOL_PRINT_NAME (var));
639*b725ae77Skettenis       break;
640*b725ae77Skettenis     }
641*b725ae77Skettenis }
642*b725ae77Skettenis 
643*b725ae77Skettenis 
644*b725ae77Skettenis 
645*b725ae77Skettenis /* Generating bytecode from GDB expressions: literals */
646*b725ae77Skettenis 
647*b725ae77Skettenis static void
gen_int_literal(struct agent_expr * ax,struct axs_value * value,LONGEST k,struct type * type)648*b725ae77Skettenis gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
649*b725ae77Skettenis 		 struct type *type)
650*b725ae77Skettenis {
651*b725ae77Skettenis   ax_const_l (ax, k);
652*b725ae77Skettenis   value->kind = axs_rvalue;
653*b725ae77Skettenis   value->type = type;
654*b725ae77Skettenis }
655*b725ae77Skettenis 
656*b725ae77Skettenis 
657*b725ae77Skettenis 
658*b725ae77Skettenis /* Generating bytecode from GDB expressions: unary conversions, casts */
659*b725ae77Skettenis 
660*b725ae77Skettenis /* Take what's on the top of the stack (as described by VALUE), and
661*b725ae77Skettenis    try to make an rvalue out of it.  Signal an error if we can't do
662*b725ae77Skettenis    that.  */
663*b725ae77Skettenis static void
require_rvalue(struct agent_expr * ax,struct axs_value * value)664*b725ae77Skettenis require_rvalue (struct agent_expr *ax, struct axs_value *value)
665*b725ae77Skettenis {
666*b725ae77Skettenis   switch (value->kind)
667*b725ae77Skettenis     {
668*b725ae77Skettenis     case axs_rvalue:
669*b725ae77Skettenis       /* It's already an rvalue.  */
670*b725ae77Skettenis       break;
671*b725ae77Skettenis 
672*b725ae77Skettenis     case axs_lvalue_memory:
673*b725ae77Skettenis       /* The top of stack is the address of the object.  Dereference.  */
674*b725ae77Skettenis       gen_fetch (ax, value->type);
675*b725ae77Skettenis       break;
676*b725ae77Skettenis 
677*b725ae77Skettenis     case axs_lvalue_register:
678*b725ae77Skettenis       /* There's nothing on the stack, but value->u.reg is the
679*b725ae77Skettenis          register number containing the value.
680*b725ae77Skettenis 
681*b725ae77Skettenis          When we add floating-point support, this is going to have to
682*b725ae77Skettenis          change.  What about SPARC register pairs, for example?  */
683*b725ae77Skettenis       ax_reg (ax, value->u.reg);
684*b725ae77Skettenis       gen_extend (ax, value->type);
685*b725ae77Skettenis       break;
686*b725ae77Skettenis     }
687*b725ae77Skettenis 
688*b725ae77Skettenis   value->kind = axs_rvalue;
689*b725ae77Skettenis }
690*b725ae77Skettenis 
691*b725ae77Skettenis 
692*b725ae77Skettenis /* Assume the top of the stack is described by VALUE, and perform the
693*b725ae77Skettenis    usual unary conversions.  This is motivated by ANSI 6.2.2, but of
694*b725ae77Skettenis    course GDB expressions are not ANSI; they're the mishmash union of
695*b725ae77Skettenis    a bunch of languages.  Rah.
696*b725ae77Skettenis 
697*b725ae77Skettenis    NOTE!  This function promises to produce an rvalue only when the
698*b725ae77Skettenis    incoming value is of an appropriate type.  In other words, the
699*b725ae77Skettenis    consumer of the value this function produces may assume the value
700*b725ae77Skettenis    is an rvalue only after checking its type.
701*b725ae77Skettenis 
702*b725ae77Skettenis    The immediate issue is that if the user tries to use a structure or
703*b725ae77Skettenis    union as an operand of, say, the `+' operator, we don't want to try
704*b725ae77Skettenis    to convert that structure to an rvalue; require_rvalue will bomb on
705*b725ae77Skettenis    structs and unions.  Rather, we want to simply pass the struct
706*b725ae77Skettenis    lvalue through unchanged, and let `+' raise an error.  */
707*b725ae77Skettenis 
708*b725ae77Skettenis static void
gen_usual_unary(struct agent_expr * ax,struct axs_value * value)709*b725ae77Skettenis gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
710*b725ae77Skettenis {
711*b725ae77Skettenis   /* We don't have to generate any code for the usual integral
712*b725ae77Skettenis      conversions, since values are always represented as full-width on
713*b725ae77Skettenis      the stack.  Should we tweak the type?  */
714*b725ae77Skettenis 
715*b725ae77Skettenis   /* Some types require special handling.  */
716*b725ae77Skettenis   switch (TYPE_CODE (value->type))
717*b725ae77Skettenis     {
718*b725ae77Skettenis       /* Functions get converted to a pointer to the function.  */
719*b725ae77Skettenis     case TYPE_CODE_FUNC:
720*b725ae77Skettenis       value->type = lookup_pointer_type (value->type);
721*b725ae77Skettenis       value->kind = axs_rvalue;	/* Should always be true, but just in case.  */
722*b725ae77Skettenis       break;
723*b725ae77Skettenis 
724*b725ae77Skettenis       /* Arrays get converted to a pointer to their first element, and
725*b725ae77Skettenis          are no longer an lvalue.  */
726*b725ae77Skettenis     case TYPE_CODE_ARRAY:
727*b725ae77Skettenis       {
728*b725ae77Skettenis 	struct type *elements = TYPE_TARGET_TYPE (value->type);
729*b725ae77Skettenis 	value->type = lookup_pointer_type (elements);
730*b725ae77Skettenis 	value->kind = axs_rvalue;
731*b725ae77Skettenis 	/* We don't need to generate any code; the address of the array
732*b725ae77Skettenis 	   is also the address of its first element.  */
733*b725ae77Skettenis       }
734*b725ae77Skettenis       break;
735*b725ae77Skettenis 
736*b725ae77Skettenis       /* Don't try to convert structures and unions to rvalues.  Let the
737*b725ae77Skettenis          consumer signal an error.  */
738*b725ae77Skettenis     case TYPE_CODE_STRUCT:
739*b725ae77Skettenis     case TYPE_CODE_UNION:
740*b725ae77Skettenis       return;
741*b725ae77Skettenis 
742*b725ae77Skettenis       /* If the value is an enum, call it an integer.  */
743*b725ae77Skettenis     case TYPE_CODE_ENUM:
744*b725ae77Skettenis       value->type = builtin_type_int;
745*b725ae77Skettenis       break;
746*b725ae77Skettenis     }
747*b725ae77Skettenis 
748*b725ae77Skettenis   /* If the value is an lvalue, dereference it.  */
749*b725ae77Skettenis   require_rvalue (ax, value);
750*b725ae77Skettenis }
751*b725ae77Skettenis 
752*b725ae77Skettenis 
753*b725ae77Skettenis /* Return non-zero iff the type TYPE1 is considered "wider" than the
754*b725ae77Skettenis    type TYPE2, according to the rules described in gen_usual_arithmetic.  */
755*b725ae77Skettenis static int
type_wider_than(struct type * type1,struct type * type2)756*b725ae77Skettenis type_wider_than (struct type *type1, struct type *type2)
757*b725ae77Skettenis {
758*b725ae77Skettenis   return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
759*b725ae77Skettenis 	  || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
760*b725ae77Skettenis 	      && TYPE_UNSIGNED (type1)
761*b725ae77Skettenis 	      && !TYPE_UNSIGNED (type2)));
762*b725ae77Skettenis }
763*b725ae77Skettenis 
764*b725ae77Skettenis 
765*b725ae77Skettenis /* Return the "wider" of the two types TYPE1 and TYPE2.  */
766*b725ae77Skettenis static struct type *
max_type(struct type * type1,struct type * type2)767*b725ae77Skettenis max_type (struct type *type1, struct type *type2)
768*b725ae77Skettenis {
769*b725ae77Skettenis   return type_wider_than (type1, type2) ? type1 : type2;
770*b725ae77Skettenis }
771*b725ae77Skettenis 
772*b725ae77Skettenis 
773*b725ae77Skettenis /* Generate code to convert a scalar value of type FROM to type TO.  */
774*b725ae77Skettenis static void
gen_conversion(struct agent_expr * ax,struct type * from,struct type * to)775*b725ae77Skettenis gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
776*b725ae77Skettenis {
777*b725ae77Skettenis   /* Perhaps there is a more graceful way to state these rules.  */
778*b725ae77Skettenis 
779*b725ae77Skettenis   /* If we're converting to a narrower type, then we need to clear out
780*b725ae77Skettenis      the upper bits.  */
781*b725ae77Skettenis   if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
782*b725ae77Skettenis     gen_extend (ax, from);
783*b725ae77Skettenis 
784*b725ae77Skettenis   /* If the two values have equal width, but different signednesses,
785*b725ae77Skettenis      then we need to extend.  */
786*b725ae77Skettenis   else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
787*b725ae77Skettenis     {
788*b725ae77Skettenis       if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
789*b725ae77Skettenis 	gen_extend (ax, to);
790*b725ae77Skettenis     }
791*b725ae77Skettenis 
792*b725ae77Skettenis   /* If we're converting to a wider type, and becoming unsigned, then
793*b725ae77Skettenis      we need to zero out any possible sign bits.  */
794*b725ae77Skettenis   else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
795*b725ae77Skettenis     {
796*b725ae77Skettenis       if (TYPE_UNSIGNED (to))
797*b725ae77Skettenis 	gen_extend (ax, to);
798*b725ae77Skettenis     }
799*b725ae77Skettenis }
800*b725ae77Skettenis 
801*b725ae77Skettenis 
802*b725ae77Skettenis /* Return non-zero iff the type FROM will require any bytecodes to be
803*b725ae77Skettenis    emitted to be converted to the type TO.  */
804*b725ae77Skettenis static int
is_nontrivial_conversion(struct type * from,struct type * to)805*b725ae77Skettenis is_nontrivial_conversion (struct type *from, struct type *to)
806*b725ae77Skettenis {
807*b725ae77Skettenis   struct agent_expr *ax = new_agent_expr (0);
808*b725ae77Skettenis   int nontrivial;
809*b725ae77Skettenis 
810*b725ae77Skettenis   /* Actually generate the code, and see if anything came out.  At the
811*b725ae77Skettenis      moment, it would be trivial to replicate the code in
812*b725ae77Skettenis      gen_conversion here, but in the future, when we're supporting
813*b725ae77Skettenis      floating point and the like, it may not be.  Doing things this
814*b725ae77Skettenis      way allows this function to be independent of the logic in
815*b725ae77Skettenis      gen_conversion.  */
816*b725ae77Skettenis   gen_conversion (ax, from, to);
817*b725ae77Skettenis   nontrivial = ax->len > 0;
818*b725ae77Skettenis   free_agent_expr (ax);
819*b725ae77Skettenis   return nontrivial;
820*b725ae77Skettenis }
821*b725ae77Skettenis 
822*b725ae77Skettenis 
823*b725ae77Skettenis /* Generate code to perform the "usual arithmetic conversions" (ANSI C
824*b725ae77Skettenis    6.2.1.5) for the two operands of an arithmetic operator.  This
825*b725ae77Skettenis    effectively finds a "least upper bound" type for the two arguments,
826*b725ae77Skettenis    and promotes each argument to that type.  *VALUE1 and *VALUE2
827*b725ae77Skettenis    describe the values as they are passed in, and as they are left.  */
828*b725ae77Skettenis static void
gen_usual_arithmetic(struct agent_expr * ax,struct axs_value * value1,struct axs_value * value2)829*b725ae77Skettenis gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
830*b725ae77Skettenis 		      struct axs_value *value2)
831*b725ae77Skettenis {
832*b725ae77Skettenis   /* Do the usual binary conversions.  */
833*b725ae77Skettenis   if (TYPE_CODE (value1->type) == TYPE_CODE_INT
834*b725ae77Skettenis       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
835*b725ae77Skettenis     {
836*b725ae77Skettenis       /* The ANSI integral promotions seem to work this way: Order the
837*b725ae77Skettenis          integer types by size, and then by signedness: an n-bit
838*b725ae77Skettenis          unsigned type is considered "wider" than an n-bit signed
839*b725ae77Skettenis          type.  Promote to the "wider" of the two types, and always
840*b725ae77Skettenis          promote at least to int.  */
841*b725ae77Skettenis       struct type *target = max_type (builtin_type_int,
842*b725ae77Skettenis 				      max_type (value1->type, value2->type));
843*b725ae77Skettenis 
844*b725ae77Skettenis       /* Deal with value2, on the top of the stack.  */
845*b725ae77Skettenis       gen_conversion (ax, value2->type, target);
846*b725ae77Skettenis 
847*b725ae77Skettenis       /* Deal with value1, not on the top of the stack.  Don't
848*b725ae77Skettenis          generate the `swap' instructions if we're not actually going
849*b725ae77Skettenis          to do anything.  */
850*b725ae77Skettenis       if (is_nontrivial_conversion (value1->type, target))
851*b725ae77Skettenis 	{
852*b725ae77Skettenis 	  ax_simple (ax, aop_swap);
853*b725ae77Skettenis 	  gen_conversion (ax, value1->type, target);
854*b725ae77Skettenis 	  ax_simple (ax, aop_swap);
855*b725ae77Skettenis 	}
856*b725ae77Skettenis 
857*b725ae77Skettenis       value1->type = value2->type = target;
858*b725ae77Skettenis     }
859*b725ae77Skettenis }
860*b725ae77Skettenis 
861*b725ae77Skettenis 
862*b725ae77Skettenis /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
863*b725ae77Skettenis    the value on the top of the stack, as described by VALUE.  Assume
864*b725ae77Skettenis    the value has integral type.  */
865*b725ae77Skettenis static void
gen_integral_promotions(struct agent_expr * ax,struct axs_value * value)866*b725ae77Skettenis gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
867*b725ae77Skettenis {
868*b725ae77Skettenis   if (!type_wider_than (value->type, builtin_type_int))
869*b725ae77Skettenis     {
870*b725ae77Skettenis       gen_conversion (ax, value->type, builtin_type_int);
871*b725ae77Skettenis       value->type = builtin_type_int;
872*b725ae77Skettenis     }
873*b725ae77Skettenis   else if (!type_wider_than (value->type, builtin_type_unsigned_int))
874*b725ae77Skettenis     {
875*b725ae77Skettenis       gen_conversion (ax, value->type, builtin_type_unsigned_int);
876*b725ae77Skettenis       value->type = builtin_type_unsigned_int;
877*b725ae77Skettenis     }
878*b725ae77Skettenis }
879*b725ae77Skettenis 
880*b725ae77Skettenis 
881*b725ae77Skettenis /* Generate code for a cast to TYPE.  */
882*b725ae77Skettenis static void
gen_cast(struct agent_expr * ax,struct axs_value * value,struct type * type)883*b725ae77Skettenis gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
884*b725ae77Skettenis {
885*b725ae77Skettenis   /* GCC does allow casts to yield lvalues, so this should be fixed
886*b725ae77Skettenis      before merging these changes into the trunk.  */
887*b725ae77Skettenis   require_rvalue (ax, value);
888*b725ae77Skettenis   /* Dereference typedefs. */
889*b725ae77Skettenis   type = check_typedef (type);
890*b725ae77Skettenis 
891*b725ae77Skettenis   switch (TYPE_CODE (type))
892*b725ae77Skettenis     {
893*b725ae77Skettenis     case TYPE_CODE_PTR:
894*b725ae77Skettenis       /* It's implementation-defined, and I'll bet this is what GCC
895*b725ae77Skettenis          does.  */
896*b725ae77Skettenis       break;
897*b725ae77Skettenis 
898*b725ae77Skettenis     case TYPE_CODE_ARRAY:
899*b725ae77Skettenis     case TYPE_CODE_STRUCT:
900*b725ae77Skettenis     case TYPE_CODE_UNION:
901*b725ae77Skettenis     case TYPE_CODE_FUNC:
902*b725ae77Skettenis       error ("Illegal type cast: intended type must be scalar.");
903*b725ae77Skettenis 
904*b725ae77Skettenis     case TYPE_CODE_ENUM:
905*b725ae77Skettenis       /* We don't have to worry about the size of the value, because
906*b725ae77Skettenis          all our integral values are fully sign-extended, and when
907*b725ae77Skettenis          casting pointers we can do anything we like.  Is there any
908*b725ae77Skettenis          way for us to actually know what GCC actually does with a
909*b725ae77Skettenis          cast like this?  */
910*b725ae77Skettenis       value->type = type;
911*b725ae77Skettenis       break;
912*b725ae77Skettenis 
913*b725ae77Skettenis     case TYPE_CODE_INT:
914*b725ae77Skettenis       gen_conversion (ax, value->type, type);
915*b725ae77Skettenis       break;
916*b725ae77Skettenis 
917*b725ae77Skettenis     case TYPE_CODE_VOID:
918*b725ae77Skettenis       /* We could pop the value, and rely on everyone else to check
919*b725ae77Skettenis          the type and notice that this value doesn't occupy a stack
920*b725ae77Skettenis          slot.  But for now, leave the value on the stack, and
921*b725ae77Skettenis          preserve the "value == stack element" assumption.  */
922*b725ae77Skettenis       break;
923*b725ae77Skettenis 
924*b725ae77Skettenis     default:
925*b725ae77Skettenis       error ("Casts to requested type are not yet implemented.");
926*b725ae77Skettenis     }
927*b725ae77Skettenis 
928*b725ae77Skettenis   value->type = type;
929*b725ae77Skettenis }
930*b725ae77Skettenis 
931*b725ae77Skettenis 
932*b725ae77Skettenis 
933*b725ae77Skettenis /* Generating bytecode from GDB expressions: arithmetic */
934*b725ae77Skettenis 
935*b725ae77Skettenis /* Scale the integer on the top of the stack by the size of the target
936*b725ae77Skettenis    of the pointer type TYPE.  */
937*b725ae77Skettenis static void
gen_scale(struct agent_expr * ax,enum agent_op op,struct type * type)938*b725ae77Skettenis gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
939*b725ae77Skettenis {
940*b725ae77Skettenis   struct type *element = TYPE_TARGET_TYPE (type);
941*b725ae77Skettenis 
942*b725ae77Skettenis   if (TYPE_LENGTH (element) != 1)
943*b725ae77Skettenis     {
944*b725ae77Skettenis       ax_const_l (ax, TYPE_LENGTH (element));
945*b725ae77Skettenis       ax_simple (ax, op);
946*b725ae77Skettenis     }
947*b725ae77Skettenis }
948*b725ae77Skettenis 
949*b725ae77Skettenis 
950*b725ae77Skettenis /* Generate code for an addition; non-trivial because we deal with
951*b725ae77Skettenis    pointer arithmetic.  We set VALUE to describe the result value; we
952*b725ae77Skettenis    assume VALUE1 and VALUE2 describe the two operands, and that
953*b725ae77Skettenis    they've undergone the usual binary conversions.  Used by both
954*b725ae77Skettenis    BINOP_ADD and BINOP_SUBSCRIPT.  NAME is used in error messages.  */
955*b725ae77Skettenis static void
gen_add(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,char * name)956*b725ae77Skettenis gen_add (struct agent_expr *ax, struct axs_value *value,
957*b725ae77Skettenis 	 struct axs_value *value1, struct axs_value *value2, char *name)
958*b725ae77Skettenis {
959*b725ae77Skettenis   /* Is it INT+PTR?  */
960*b725ae77Skettenis   if (TYPE_CODE (value1->type) == TYPE_CODE_INT
961*b725ae77Skettenis       && TYPE_CODE (value2->type) == TYPE_CODE_PTR)
962*b725ae77Skettenis     {
963*b725ae77Skettenis       /* Swap the values and proceed normally.  */
964*b725ae77Skettenis       ax_simple (ax, aop_swap);
965*b725ae77Skettenis       gen_scale (ax, aop_mul, value2->type);
966*b725ae77Skettenis       ax_simple (ax, aop_add);
967*b725ae77Skettenis       gen_extend (ax, value2->type);	/* Catch overflow.  */
968*b725ae77Skettenis       value->type = value2->type;
969*b725ae77Skettenis     }
970*b725ae77Skettenis 
971*b725ae77Skettenis   /* Is it PTR+INT?  */
972*b725ae77Skettenis   else if (TYPE_CODE (value1->type) == TYPE_CODE_PTR
973*b725ae77Skettenis 	   && TYPE_CODE (value2->type) == TYPE_CODE_INT)
974*b725ae77Skettenis     {
975*b725ae77Skettenis       gen_scale (ax, aop_mul, value1->type);
976*b725ae77Skettenis       ax_simple (ax, aop_add);
977*b725ae77Skettenis       gen_extend (ax, value1->type);	/* Catch overflow.  */
978*b725ae77Skettenis       value->type = value1->type;
979*b725ae77Skettenis     }
980*b725ae77Skettenis 
981*b725ae77Skettenis   /* Must be number + number; the usual binary conversions will have
982*b725ae77Skettenis      brought them both to the same width.  */
983*b725ae77Skettenis   else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
984*b725ae77Skettenis 	   && TYPE_CODE (value2->type) == TYPE_CODE_INT)
985*b725ae77Skettenis     {
986*b725ae77Skettenis       ax_simple (ax, aop_add);
987*b725ae77Skettenis       gen_extend (ax, value1->type);	/* Catch overflow.  */
988*b725ae77Skettenis       value->type = value1->type;
989*b725ae77Skettenis     }
990*b725ae77Skettenis 
991*b725ae77Skettenis   else
992*b725ae77Skettenis     error ("Illegal combination of types in %s.", name);
993*b725ae77Skettenis 
994*b725ae77Skettenis   value->kind = axs_rvalue;
995*b725ae77Skettenis }
996*b725ae77Skettenis 
997*b725ae77Skettenis 
998*b725ae77Skettenis /* Generate code for an addition; non-trivial because we have to deal
999*b725ae77Skettenis    with pointer arithmetic.  We set VALUE to describe the result
1000*b725ae77Skettenis    value; we assume VALUE1 and VALUE2 describe the two operands, and
1001*b725ae77Skettenis    that they've undergone the usual binary conversions.  */
1002*b725ae77Skettenis static void
gen_sub(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2)1003*b725ae77Skettenis gen_sub (struct agent_expr *ax, struct axs_value *value,
1004*b725ae77Skettenis 	 struct axs_value *value1, struct axs_value *value2)
1005*b725ae77Skettenis {
1006*b725ae77Skettenis   if (TYPE_CODE (value1->type) == TYPE_CODE_PTR)
1007*b725ae77Skettenis     {
1008*b725ae77Skettenis       /* Is it PTR - INT?  */
1009*b725ae77Skettenis       if (TYPE_CODE (value2->type) == TYPE_CODE_INT)
1010*b725ae77Skettenis 	{
1011*b725ae77Skettenis 	  gen_scale (ax, aop_mul, value1->type);
1012*b725ae77Skettenis 	  ax_simple (ax, aop_sub);
1013*b725ae77Skettenis 	  gen_extend (ax, value1->type);	/* Catch overflow.  */
1014*b725ae77Skettenis 	  value->type = value1->type;
1015*b725ae77Skettenis 	}
1016*b725ae77Skettenis 
1017*b725ae77Skettenis       /* Is it PTR - PTR?  Strictly speaking, the types ought to
1018*b725ae77Skettenis          match, but this is what the normal GDB expression evaluator
1019*b725ae77Skettenis          tests for.  */
1020*b725ae77Skettenis       else if (TYPE_CODE (value2->type) == TYPE_CODE_PTR
1021*b725ae77Skettenis 	       && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1022*b725ae77Skettenis 		   == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1023*b725ae77Skettenis 	{
1024*b725ae77Skettenis 	  ax_simple (ax, aop_sub);
1025*b725ae77Skettenis 	  gen_scale (ax, aop_div_unsigned, value1->type);
1026*b725ae77Skettenis 	  value->type = builtin_type_long;	/* FIXME --- should be ptrdiff_t */
1027*b725ae77Skettenis 	}
1028*b725ae77Skettenis       else
1029*b725ae77Skettenis 	error ("\
1030*b725ae77Skettenis First argument of `-' is a pointer, but second argument is neither\n\
1031*b725ae77Skettenis an integer nor a pointer of the same type.");
1032*b725ae77Skettenis     }
1033*b725ae77Skettenis 
1034*b725ae77Skettenis   /* Must be number + number.  */
1035*b725ae77Skettenis   else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
1036*b725ae77Skettenis 	   && TYPE_CODE (value2->type) == TYPE_CODE_INT)
1037*b725ae77Skettenis     {
1038*b725ae77Skettenis       ax_simple (ax, aop_sub);
1039*b725ae77Skettenis       gen_extend (ax, value1->type);	/* Catch overflow.  */
1040*b725ae77Skettenis       value->type = value1->type;
1041*b725ae77Skettenis     }
1042*b725ae77Skettenis 
1043*b725ae77Skettenis   else
1044*b725ae77Skettenis     error ("Illegal combination of types in subtraction.");
1045*b725ae77Skettenis 
1046*b725ae77Skettenis   value->kind = axs_rvalue;
1047*b725ae77Skettenis }
1048*b725ae77Skettenis 
1049*b725ae77Skettenis /* Generate code for a binary operator that doesn't do pointer magic.
1050*b725ae77Skettenis    We set VALUE to describe the result value; we assume VALUE1 and
1051*b725ae77Skettenis    VALUE2 describe the two operands, and that they've undergone the
1052*b725ae77Skettenis    usual binary conversions.  MAY_CARRY should be non-zero iff the
1053*b725ae77Skettenis    result needs to be extended.  NAME is the English name of the
1054*b725ae77Skettenis    operator, used in error messages */
1055*b725ae77Skettenis 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)1056*b725ae77Skettenis gen_binop (struct agent_expr *ax, struct axs_value *value,
1057*b725ae77Skettenis 	   struct axs_value *value1, struct axs_value *value2, enum agent_op op,
1058*b725ae77Skettenis 	   enum agent_op op_unsigned, int may_carry, char *name)
1059*b725ae77Skettenis {
1060*b725ae77Skettenis   /* We only handle INT op INT.  */
1061*b725ae77Skettenis   if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1062*b725ae77Skettenis       || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1063*b725ae77Skettenis     error ("Illegal combination of types in %s.", name);
1064*b725ae77Skettenis 
1065*b725ae77Skettenis   ax_simple (ax,
1066*b725ae77Skettenis 	     TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1067*b725ae77Skettenis   if (may_carry)
1068*b725ae77Skettenis     gen_extend (ax, value1->type);	/* catch overflow */
1069*b725ae77Skettenis   value->type = value1->type;
1070*b725ae77Skettenis   value->kind = axs_rvalue;
1071*b725ae77Skettenis }
1072*b725ae77Skettenis 
1073*b725ae77Skettenis 
1074*b725ae77Skettenis static void
gen_logical_not(struct agent_expr * ax,struct axs_value * value)1075*b725ae77Skettenis gen_logical_not (struct agent_expr *ax, struct axs_value *value)
1076*b725ae77Skettenis {
1077*b725ae77Skettenis   if (TYPE_CODE (value->type) != TYPE_CODE_INT
1078*b725ae77Skettenis       && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1079*b725ae77Skettenis     error ("Illegal type of operand to `!'.");
1080*b725ae77Skettenis 
1081*b725ae77Skettenis   gen_usual_unary (ax, value);
1082*b725ae77Skettenis   ax_simple (ax, aop_log_not);
1083*b725ae77Skettenis   value->type = builtin_type_int;
1084*b725ae77Skettenis }
1085*b725ae77Skettenis 
1086*b725ae77Skettenis 
1087*b725ae77Skettenis static void
gen_complement(struct agent_expr * ax,struct axs_value * value)1088*b725ae77Skettenis gen_complement (struct agent_expr *ax, struct axs_value *value)
1089*b725ae77Skettenis {
1090*b725ae77Skettenis   if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1091*b725ae77Skettenis     error ("Illegal type of operand to `~'.");
1092*b725ae77Skettenis 
1093*b725ae77Skettenis   gen_usual_unary (ax, value);
1094*b725ae77Skettenis   gen_integral_promotions (ax, value);
1095*b725ae77Skettenis   ax_simple (ax, aop_bit_not);
1096*b725ae77Skettenis   gen_extend (ax, value->type);
1097*b725ae77Skettenis }
1098*b725ae77Skettenis 
1099*b725ae77Skettenis 
1100*b725ae77Skettenis 
1101*b725ae77Skettenis /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1102*b725ae77Skettenis 
1103*b725ae77Skettenis /* Dereference the value on the top of the stack.  */
1104*b725ae77Skettenis static void
gen_deref(struct agent_expr * ax,struct axs_value * value)1105*b725ae77Skettenis gen_deref (struct agent_expr *ax, struct axs_value *value)
1106*b725ae77Skettenis {
1107*b725ae77Skettenis   /* The caller should check the type, because several operators use
1108*b725ae77Skettenis      this, and we don't know what error message to generate.  */
1109*b725ae77Skettenis   if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1110*b725ae77Skettenis     internal_error (__FILE__, __LINE__,
1111*b725ae77Skettenis 		    "gen_deref: expected a pointer");
1112*b725ae77Skettenis 
1113*b725ae77Skettenis   /* We've got an rvalue now, which is a pointer.  We want to yield an
1114*b725ae77Skettenis      lvalue, whose address is exactly that pointer.  So we don't
1115*b725ae77Skettenis      actually emit any code; we just change the type from "Pointer to
1116*b725ae77Skettenis      T" to "T", and mark the value as an lvalue in memory.  Leave it
1117*b725ae77Skettenis      to the consumer to actually dereference it.  */
1118*b725ae77Skettenis   value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1119*b725ae77Skettenis   value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1120*b725ae77Skettenis 		 ? axs_rvalue : axs_lvalue_memory);
1121*b725ae77Skettenis }
1122*b725ae77Skettenis 
1123*b725ae77Skettenis 
1124*b725ae77Skettenis /* Produce the address of the lvalue on the top of the stack.  */
1125*b725ae77Skettenis static void
gen_address_of(struct agent_expr * ax,struct axs_value * value)1126*b725ae77Skettenis gen_address_of (struct agent_expr *ax, struct axs_value *value)
1127*b725ae77Skettenis {
1128*b725ae77Skettenis   /* Special case for taking the address of a function.  The ANSI
1129*b725ae77Skettenis      standard describes this as a special case, too, so this
1130*b725ae77Skettenis      arrangement is not without motivation.  */
1131*b725ae77Skettenis   if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1132*b725ae77Skettenis     /* The value's already an rvalue on the stack, so we just need to
1133*b725ae77Skettenis        change the type.  */
1134*b725ae77Skettenis     value->type = lookup_pointer_type (value->type);
1135*b725ae77Skettenis   else
1136*b725ae77Skettenis     switch (value->kind)
1137*b725ae77Skettenis       {
1138*b725ae77Skettenis       case axs_rvalue:
1139*b725ae77Skettenis 	error ("Operand of `&' is an rvalue, which has no address.");
1140*b725ae77Skettenis 
1141*b725ae77Skettenis       case axs_lvalue_register:
1142*b725ae77Skettenis 	error ("Operand of `&' is in a register, and has no address.");
1143*b725ae77Skettenis 
1144*b725ae77Skettenis       case axs_lvalue_memory:
1145*b725ae77Skettenis 	value->kind = axs_rvalue;
1146*b725ae77Skettenis 	value->type = lookup_pointer_type (value->type);
1147*b725ae77Skettenis 	break;
1148*b725ae77Skettenis       }
1149*b725ae77Skettenis }
1150*b725ae77Skettenis 
1151*b725ae77Skettenis 
1152*b725ae77Skettenis /* A lot of this stuff will have to change to support C++.  But we're
1153*b725ae77Skettenis    not going to deal with that at the moment.  */
1154*b725ae77Skettenis 
1155*b725ae77Skettenis /* Find the field in the structure type TYPE named NAME, and return
1156*b725ae77Skettenis    its index in TYPE's field array.  */
1157*b725ae77Skettenis static int
find_field(struct type * type,char * name)1158*b725ae77Skettenis find_field (struct type *type, char *name)
1159*b725ae77Skettenis {
1160*b725ae77Skettenis   int i;
1161*b725ae77Skettenis 
1162*b725ae77Skettenis   CHECK_TYPEDEF (type);
1163*b725ae77Skettenis 
1164*b725ae77Skettenis   /* Make sure this isn't C++.  */
1165*b725ae77Skettenis   if (TYPE_N_BASECLASSES (type) != 0)
1166*b725ae77Skettenis     internal_error (__FILE__, __LINE__,
1167*b725ae77Skettenis 		    "find_field: derived classes supported");
1168*b725ae77Skettenis 
1169*b725ae77Skettenis   for (i = 0; i < TYPE_NFIELDS (type); i++)
1170*b725ae77Skettenis     {
1171*b725ae77Skettenis       char *this_name = TYPE_FIELD_NAME (type, i);
1172*b725ae77Skettenis 
1173*b725ae77Skettenis       if (this_name && strcmp (name, this_name) == 0)
1174*b725ae77Skettenis 	return i;
1175*b725ae77Skettenis 
1176*b725ae77Skettenis       if (this_name[0] == '\0')
1177*b725ae77Skettenis 	internal_error (__FILE__, __LINE__,
1178*b725ae77Skettenis 			"find_field: anonymous unions not supported");
1179*b725ae77Skettenis     }
1180*b725ae77Skettenis 
1181*b725ae77Skettenis   error ("Couldn't find member named `%s' in struct/union `%s'",
1182*b725ae77Skettenis 	 name, TYPE_TAG_NAME (type));
1183*b725ae77Skettenis 
1184*b725ae77Skettenis   return 0;
1185*b725ae77Skettenis }
1186*b725ae77Skettenis 
1187*b725ae77Skettenis 
1188*b725ae77Skettenis /* Generate code to push the value of a bitfield of a structure whose
1189*b725ae77Skettenis    address is on the top of the stack.  START and END give the
1190*b725ae77Skettenis    starting and one-past-ending *bit* numbers of the field within the
1191*b725ae77Skettenis    structure.  */
1192*b725ae77Skettenis static void
gen_bitfield_ref(struct agent_expr * ax,struct axs_value * value,struct type * type,int start,int end)1193*b725ae77Skettenis gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1194*b725ae77Skettenis 		  struct type *type, int start, int end)
1195*b725ae77Skettenis {
1196*b725ae77Skettenis   /* Note that ops[i] fetches 8 << i bits.  */
1197*b725ae77Skettenis   static enum agent_op ops[]
1198*b725ae77Skettenis   =
1199*b725ae77Skettenis   {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1200*b725ae77Skettenis   static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1201*b725ae77Skettenis 
1202*b725ae77Skettenis   /* We don't want to touch any byte that the bitfield doesn't
1203*b725ae77Skettenis      actually occupy; we shouldn't make any accesses we're not
1204*b725ae77Skettenis      explicitly permitted to.  We rely here on the fact that the
1205*b725ae77Skettenis      bytecode `ref' operators work on unaligned addresses.
1206*b725ae77Skettenis 
1207*b725ae77Skettenis      It takes some fancy footwork to get the stack to work the way
1208*b725ae77Skettenis      we'd like.  Say we're retrieving a bitfield that requires three
1209*b725ae77Skettenis      fetches.  Initially, the stack just contains the address:
1210*b725ae77Skettenis      addr
1211*b725ae77Skettenis      For the first fetch, we duplicate the address
1212*b725ae77Skettenis      addr addr
1213*b725ae77Skettenis      then add the byte offset, do the fetch, and shift and mask as
1214*b725ae77Skettenis      needed, yielding a fragment of the value, properly aligned for
1215*b725ae77Skettenis      the final bitwise or:
1216*b725ae77Skettenis      addr frag1
1217*b725ae77Skettenis      then we swap, and repeat the process:
1218*b725ae77Skettenis      frag1 addr                    --- address on top
1219*b725ae77Skettenis      frag1 addr addr               --- duplicate it
1220*b725ae77Skettenis      frag1 addr frag2              --- get second fragment
1221*b725ae77Skettenis      frag1 frag2 addr              --- swap again
1222*b725ae77Skettenis      frag1 frag2 frag3             --- get third fragment
1223*b725ae77Skettenis      Notice that, since the third fragment is the last one, we don't
1224*b725ae77Skettenis      bother duplicating the address this time.  Now we have all the
1225*b725ae77Skettenis      fragments on the stack, and we can simply `or' them together,
1226*b725ae77Skettenis      yielding the final value of the bitfield.  */
1227*b725ae77Skettenis 
1228*b725ae77Skettenis   /* The first and one-after-last bits in the field, but rounded down
1229*b725ae77Skettenis      and up to byte boundaries.  */
1230*b725ae77Skettenis   int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1231*b725ae77Skettenis   int bound_end = (((end + TARGET_CHAR_BIT - 1)
1232*b725ae77Skettenis 		    / TARGET_CHAR_BIT)
1233*b725ae77Skettenis 		   * TARGET_CHAR_BIT);
1234*b725ae77Skettenis 
1235*b725ae77Skettenis   /* current bit offset within the structure */
1236*b725ae77Skettenis   int offset;
1237*b725ae77Skettenis 
1238*b725ae77Skettenis   /* The index in ops of the opcode we're considering.  */
1239*b725ae77Skettenis   int op;
1240*b725ae77Skettenis 
1241*b725ae77Skettenis   /* The number of fragments we generated in the process.  Probably
1242*b725ae77Skettenis      equal to the number of `one' bits in bytesize, but who cares?  */
1243*b725ae77Skettenis   int fragment_count;
1244*b725ae77Skettenis 
1245*b725ae77Skettenis   /* Dereference any typedefs. */
1246*b725ae77Skettenis   type = check_typedef (type);
1247*b725ae77Skettenis 
1248*b725ae77Skettenis   /* Can we fetch the number of bits requested at all?  */
1249*b725ae77Skettenis   if ((end - start) > ((1 << num_ops) * 8))
1250*b725ae77Skettenis     internal_error (__FILE__, __LINE__,
1251*b725ae77Skettenis 		    "gen_bitfield_ref: bitfield too wide");
1252*b725ae77Skettenis 
1253*b725ae77Skettenis   /* Note that we know here that we only need to try each opcode once.
1254*b725ae77Skettenis      That may not be true on machines with weird byte sizes.  */
1255*b725ae77Skettenis   offset = bound_start;
1256*b725ae77Skettenis   fragment_count = 0;
1257*b725ae77Skettenis   for (op = num_ops - 1; op >= 0; op--)
1258*b725ae77Skettenis     {
1259*b725ae77Skettenis       /* number of bits that ops[op] would fetch */
1260*b725ae77Skettenis       int op_size = 8 << op;
1261*b725ae77Skettenis 
1262*b725ae77Skettenis       /* The stack at this point, from bottom to top, contains zero or
1263*b725ae77Skettenis          more fragments, then the address.  */
1264*b725ae77Skettenis 
1265*b725ae77Skettenis       /* Does this fetch fit within the bitfield?  */
1266*b725ae77Skettenis       if (offset + op_size <= bound_end)
1267*b725ae77Skettenis 	{
1268*b725ae77Skettenis 	  /* Is this the last fragment?  */
1269*b725ae77Skettenis 	  int last_frag = (offset + op_size == bound_end);
1270*b725ae77Skettenis 
1271*b725ae77Skettenis 	  if (!last_frag)
1272*b725ae77Skettenis 	    ax_simple (ax, aop_dup);	/* keep a copy of the address */
1273*b725ae77Skettenis 
1274*b725ae77Skettenis 	  /* Add the offset.  */
1275*b725ae77Skettenis 	  gen_offset (ax, offset / TARGET_CHAR_BIT);
1276*b725ae77Skettenis 
1277*b725ae77Skettenis 	  if (trace_kludge)
1278*b725ae77Skettenis 	    {
1279*b725ae77Skettenis 	      /* Record the area of memory we're about to fetch.  */
1280*b725ae77Skettenis 	      ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1281*b725ae77Skettenis 	    }
1282*b725ae77Skettenis 
1283*b725ae77Skettenis 	  /* Perform the fetch.  */
1284*b725ae77Skettenis 	  ax_simple (ax, ops[op]);
1285*b725ae77Skettenis 
1286*b725ae77Skettenis 	  /* Shift the bits we have to their proper position.
1287*b725ae77Skettenis 	     gen_left_shift will generate right shifts when the operand
1288*b725ae77Skettenis 	     is negative.
1289*b725ae77Skettenis 
1290*b725ae77Skettenis 	     A big-endian field diagram to ponder:
1291*b725ae77Skettenis 	     byte 0  byte 1  byte 2  byte 3  byte 4  byte 5  byte 6  byte 7
1292*b725ae77Skettenis 	     +------++------++------++------++------++------++------++------+
1293*b725ae77Skettenis 	     xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1294*b725ae77Skettenis 	     ^               ^               ^    ^
1295*b725ae77Skettenis 	     bit number      16              32              48   53
1296*b725ae77Skettenis 	     These are bit numbers as supplied by GDB.  Note that the
1297*b725ae77Skettenis 	     bit numbers run from right to left once you've fetched the
1298*b725ae77Skettenis 	     value!
1299*b725ae77Skettenis 
1300*b725ae77Skettenis 	     A little-endian field diagram to ponder:
1301*b725ae77Skettenis 	     byte 7  byte 6  byte 5  byte 4  byte 3  byte 2  byte 1  byte 0
1302*b725ae77Skettenis 	     +------++------++------++------++------++------++------++------+
1303*b725ae77Skettenis 	     xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1304*b725ae77Skettenis 	     ^               ^               ^           ^   ^
1305*b725ae77Skettenis 	     bit number     48              32              16          4   0
1306*b725ae77Skettenis 
1307*b725ae77Skettenis 	     In both cases, the most significant end is on the left
1308*b725ae77Skettenis 	     (i.e. normal numeric writing order), which means that you
1309*b725ae77Skettenis 	     don't go crazy thinking about `left' and `right' shifts.
1310*b725ae77Skettenis 
1311*b725ae77Skettenis 	     We don't have to worry about masking yet:
1312*b725ae77Skettenis 	     - If they contain garbage off the least significant end, then we
1313*b725ae77Skettenis 	     must be looking at the low end of the field, and the right
1314*b725ae77Skettenis 	     shift will wipe them out.
1315*b725ae77Skettenis 	     - If they contain garbage off the most significant end, then we
1316*b725ae77Skettenis 	     must be looking at the most significant end of the word, and
1317*b725ae77Skettenis 	     the sign/zero extension will wipe them out.
1318*b725ae77Skettenis 	     - If we're in the interior of the word, then there is no garbage
1319*b725ae77Skettenis 	     on either end, because the ref operators zero-extend.  */
1320*b725ae77Skettenis 	  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1321*b725ae77Skettenis 	    gen_left_shift (ax, end - (offset + op_size));
1322*b725ae77Skettenis 	  else
1323*b725ae77Skettenis 	    gen_left_shift (ax, offset - start);
1324*b725ae77Skettenis 
1325*b725ae77Skettenis 	  if (!last_frag)
1326*b725ae77Skettenis 	    /* Bring the copy of the address up to the top.  */
1327*b725ae77Skettenis 	    ax_simple (ax, aop_swap);
1328*b725ae77Skettenis 
1329*b725ae77Skettenis 	  offset += op_size;
1330*b725ae77Skettenis 	  fragment_count++;
1331*b725ae77Skettenis 	}
1332*b725ae77Skettenis     }
1333*b725ae77Skettenis 
1334*b725ae77Skettenis   /* Generate enough bitwise `or' operations to combine all the
1335*b725ae77Skettenis      fragments we left on the stack.  */
1336*b725ae77Skettenis   while (fragment_count-- > 1)
1337*b725ae77Skettenis     ax_simple (ax, aop_bit_or);
1338*b725ae77Skettenis 
1339*b725ae77Skettenis   /* Sign- or zero-extend the value as appropriate.  */
1340*b725ae77Skettenis   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1341*b725ae77Skettenis 
1342*b725ae77Skettenis   /* This is *not* an lvalue.  Ugh.  */
1343*b725ae77Skettenis   value->kind = axs_rvalue;
1344*b725ae77Skettenis   value->type = type;
1345*b725ae77Skettenis }
1346*b725ae77Skettenis 
1347*b725ae77Skettenis 
1348*b725ae77Skettenis /* Generate code to reference the member named FIELD of a structure or
1349*b725ae77Skettenis    union.  The top of the stack, as described by VALUE, should have
1350*b725ae77Skettenis    type (pointer to a)* struct/union.  OPERATOR_NAME is the name of
1351*b725ae77Skettenis    the operator being compiled, and OPERAND_NAME is the kind of thing
1352*b725ae77Skettenis    it operates on; we use them in error messages.  */
1353*b725ae77Skettenis static void
gen_struct_ref(struct agent_expr * ax,struct axs_value * value,char * field,char * operator_name,char * operand_name)1354*b725ae77Skettenis gen_struct_ref (struct agent_expr *ax, struct axs_value *value, char *field,
1355*b725ae77Skettenis 		char *operator_name, char *operand_name)
1356*b725ae77Skettenis {
1357*b725ae77Skettenis   struct type *type;
1358*b725ae77Skettenis   int i;
1359*b725ae77Skettenis 
1360*b725ae77Skettenis   /* Follow pointers until we reach a non-pointer.  These aren't the C
1361*b725ae77Skettenis      semantics, but they're what the normal GDB evaluator does, so we
1362*b725ae77Skettenis      should at least be consistent.  */
1363*b725ae77Skettenis   while (TYPE_CODE (value->type) == TYPE_CODE_PTR)
1364*b725ae77Skettenis     {
1365*b725ae77Skettenis       gen_usual_unary (ax, value);
1366*b725ae77Skettenis       gen_deref (ax, value);
1367*b725ae77Skettenis     }
1368*b725ae77Skettenis   type = check_typedef (value->type);
1369*b725ae77Skettenis 
1370*b725ae77Skettenis   /* This must yield a structure or a union.  */
1371*b725ae77Skettenis   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1372*b725ae77Skettenis       && TYPE_CODE (type) != TYPE_CODE_UNION)
1373*b725ae77Skettenis     error ("The left operand of `%s' is not a %s.",
1374*b725ae77Skettenis 	   operator_name, operand_name);
1375*b725ae77Skettenis 
1376*b725ae77Skettenis   /* And it must be in memory; we don't deal with structure rvalues,
1377*b725ae77Skettenis      or structures living in registers.  */
1378*b725ae77Skettenis   if (value->kind != axs_lvalue_memory)
1379*b725ae77Skettenis     error ("Structure does not live in memory.");
1380*b725ae77Skettenis 
1381*b725ae77Skettenis   i = find_field (type, field);
1382*b725ae77Skettenis 
1383*b725ae77Skettenis   /* Is this a bitfield?  */
1384*b725ae77Skettenis   if (TYPE_FIELD_PACKED (type, i))
1385*b725ae77Skettenis     gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1386*b725ae77Skettenis 		      TYPE_FIELD_BITPOS (type, i),
1387*b725ae77Skettenis 		      (TYPE_FIELD_BITPOS (type, i)
1388*b725ae77Skettenis 		       + TYPE_FIELD_BITSIZE (type, i)));
1389*b725ae77Skettenis   else
1390*b725ae77Skettenis     {
1391*b725ae77Skettenis       gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1392*b725ae77Skettenis       value->kind = axs_lvalue_memory;
1393*b725ae77Skettenis       value->type = TYPE_FIELD_TYPE (type, i);
1394*b725ae77Skettenis     }
1395*b725ae77Skettenis }
1396*b725ae77Skettenis 
1397*b725ae77Skettenis 
1398*b725ae77Skettenis /* Generate code for GDB's magical `repeat' operator.
1399*b725ae77Skettenis    LVALUE @ INT creates an array INT elements long, and whose elements
1400*b725ae77Skettenis    have the same type as LVALUE, located in memory so that LVALUE is
1401*b725ae77Skettenis    its first element.  For example, argv[0]@argc gives you the array
1402*b725ae77Skettenis    of command-line arguments.
1403*b725ae77Skettenis 
1404*b725ae77Skettenis    Unfortunately, because we have to know the types before we actually
1405*b725ae77Skettenis    have a value for the expression, we can't implement this perfectly
1406*b725ae77Skettenis    without changing the type system, having values that occupy two
1407*b725ae77Skettenis    stack slots, doing weird things with sizeof, etc.  So we require
1408*b725ae77Skettenis    the right operand to be a constant expression.  */
1409*b725ae77Skettenis static void
gen_repeat(union exp_element ** pc,struct agent_expr * ax,struct axs_value * value)1410*b725ae77Skettenis gen_repeat (union exp_element **pc, struct agent_expr *ax,
1411*b725ae77Skettenis 	    struct axs_value *value)
1412*b725ae77Skettenis {
1413*b725ae77Skettenis   struct axs_value value1;
1414*b725ae77Skettenis   /* We don't want to turn this into an rvalue, so no conversions
1415*b725ae77Skettenis      here.  */
1416*b725ae77Skettenis   gen_expr (pc, ax, &value1);
1417*b725ae77Skettenis   if (value1.kind != axs_lvalue_memory)
1418*b725ae77Skettenis     error ("Left operand of `@' must be an object in memory.");
1419*b725ae77Skettenis 
1420*b725ae77Skettenis   /* Evaluate the length; it had better be a constant.  */
1421*b725ae77Skettenis   {
1422*b725ae77Skettenis     struct value *v = const_expr (pc);
1423*b725ae77Skettenis     int length;
1424*b725ae77Skettenis 
1425*b725ae77Skettenis     if (!v)
1426*b725ae77Skettenis       error ("Right operand of `@' must be a constant, in agent expressions.");
1427*b725ae77Skettenis     if (TYPE_CODE (v->type) != TYPE_CODE_INT)
1428*b725ae77Skettenis       error ("Right operand of `@' must be an integer.");
1429*b725ae77Skettenis     length = value_as_long (v);
1430*b725ae77Skettenis     if (length <= 0)
1431*b725ae77Skettenis       error ("Right operand of `@' must be positive.");
1432*b725ae77Skettenis 
1433*b725ae77Skettenis     /* The top of the stack is already the address of the object, so
1434*b725ae77Skettenis        all we need to do is frob the type of the lvalue.  */
1435*b725ae77Skettenis     {
1436*b725ae77Skettenis       /* FIXME-type-allocation: need a way to free this type when we are
1437*b725ae77Skettenis          done with it.  */
1438*b725ae77Skettenis       struct type *range
1439*b725ae77Skettenis       = create_range_type (0, builtin_type_int, 0, length - 1);
1440*b725ae77Skettenis       struct type *array = create_array_type (0, value1.type, range);
1441*b725ae77Skettenis 
1442*b725ae77Skettenis       value->kind = axs_lvalue_memory;
1443*b725ae77Skettenis       value->type = array;
1444*b725ae77Skettenis     }
1445*b725ae77Skettenis   }
1446*b725ae77Skettenis }
1447*b725ae77Skettenis 
1448*b725ae77Skettenis 
1449*b725ae77Skettenis /* Emit code for the `sizeof' operator.
1450*b725ae77Skettenis    *PC should point at the start of the operand expression; we advance it
1451*b725ae77Skettenis    to the first instruction after the operand.  */
1452*b725ae77Skettenis static void
gen_sizeof(union exp_element ** pc,struct agent_expr * ax,struct axs_value * value)1453*b725ae77Skettenis gen_sizeof (union exp_element **pc, struct agent_expr *ax,
1454*b725ae77Skettenis 	    struct axs_value *value)
1455*b725ae77Skettenis {
1456*b725ae77Skettenis   /* We don't care about the value of the operand expression; we only
1457*b725ae77Skettenis      care about its type.  However, in the current arrangement, the
1458*b725ae77Skettenis      only way to find an expression's type is to generate code for it.
1459*b725ae77Skettenis      So we generate code for the operand, and then throw it away,
1460*b725ae77Skettenis      replacing it with code that simply pushes its size.  */
1461*b725ae77Skettenis   int start = ax->len;
1462*b725ae77Skettenis   gen_expr (pc, ax, value);
1463*b725ae77Skettenis 
1464*b725ae77Skettenis   /* Throw away the code we just generated.  */
1465*b725ae77Skettenis   ax->len = start;
1466*b725ae77Skettenis 
1467*b725ae77Skettenis   ax_const_l (ax, TYPE_LENGTH (value->type));
1468*b725ae77Skettenis   value->kind = axs_rvalue;
1469*b725ae77Skettenis   value->type = builtin_type_int;
1470*b725ae77Skettenis }
1471*b725ae77Skettenis 
1472*b725ae77Skettenis 
1473*b725ae77Skettenis /* Generating bytecode from GDB expressions: general recursive thingy  */
1474*b725ae77Skettenis 
1475*b725ae77Skettenis /* A gen_expr function written by a Gen-X'er guy.
1476*b725ae77Skettenis    Append code for the subexpression of EXPR starting at *POS_P to AX.  */
1477*b725ae77Skettenis static void
gen_expr(union exp_element ** pc,struct agent_expr * ax,struct axs_value * value)1478*b725ae77Skettenis gen_expr (union exp_element **pc, struct agent_expr *ax,
1479*b725ae77Skettenis 	  struct axs_value *value)
1480*b725ae77Skettenis {
1481*b725ae77Skettenis   /* Used to hold the descriptions of operand expressions.  */
1482*b725ae77Skettenis   struct axs_value value1, value2;
1483*b725ae77Skettenis   enum exp_opcode op = (*pc)[0].opcode;
1484*b725ae77Skettenis 
1485*b725ae77Skettenis   /* If we're looking at a constant expression, just push its value.  */
1486*b725ae77Skettenis   {
1487*b725ae77Skettenis     struct value *v = maybe_const_expr (pc);
1488*b725ae77Skettenis 
1489*b725ae77Skettenis     if (v)
1490*b725ae77Skettenis       {
1491*b725ae77Skettenis 	ax_const_l (ax, value_as_long (v));
1492*b725ae77Skettenis 	value->kind = axs_rvalue;
1493*b725ae77Skettenis 	value->type = check_typedef (VALUE_TYPE (v));
1494*b725ae77Skettenis 	return;
1495*b725ae77Skettenis       }
1496*b725ae77Skettenis   }
1497*b725ae77Skettenis 
1498*b725ae77Skettenis   /* Otherwise, go ahead and generate code for it.  */
1499*b725ae77Skettenis   switch (op)
1500*b725ae77Skettenis     {
1501*b725ae77Skettenis       /* Binary arithmetic operators.  */
1502*b725ae77Skettenis     case BINOP_ADD:
1503*b725ae77Skettenis     case BINOP_SUB:
1504*b725ae77Skettenis     case BINOP_MUL:
1505*b725ae77Skettenis     case BINOP_DIV:
1506*b725ae77Skettenis     case BINOP_REM:
1507*b725ae77Skettenis     case BINOP_SUBSCRIPT:
1508*b725ae77Skettenis     case BINOP_BITWISE_AND:
1509*b725ae77Skettenis     case BINOP_BITWISE_IOR:
1510*b725ae77Skettenis     case BINOP_BITWISE_XOR:
1511*b725ae77Skettenis       (*pc)++;
1512*b725ae77Skettenis       gen_expr (pc, ax, &value1);
1513*b725ae77Skettenis       gen_usual_unary (ax, &value1);
1514*b725ae77Skettenis       gen_expr (pc, ax, &value2);
1515*b725ae77Skettenis       gen_usual_unary (ax, &value2);
1516*b725ae77Skettenis       gen_usual_arithmetic (ax, &value1, &value2);
1517*b725ae77Skettenis       switch (op)
1518*b725ae77Skettenis 	{
1519*b725ae77Skettenis 	case BINOP_ADD:
1520*b725ae77Skettenis 	  gen_add (ax, value, &value1, &value2, "addition");
1521*b725ae77Skettenis 	  break;
1522*b725ae77Skettenis 	case BINOP_SUB:
1523*b725ae77Skettenis 	  gen_sub (ax, value, &value1, &value2);
1524*b725ae77Skettenis 	  break;
1525*b725ae77Skettenis 	case BINOP_MUL:
1526*b725ae77Skettenis 	  gen_binop (ax, value, &value1, &value2,
1527*b725ae77Skettenis 		     aop_mul, aop_mul, 1, "multiplication");
1528*b725ae77Skettenis 	  break;
1529*b725ae77Skettenis 	case BINOP_DIV:
1530*b725ae77Skettenis 	  gen_binop (ax, value, &value1, &value2,
1531*b725ae77Skettenis 		     aop_div_signed, aop_div_unsigned, 1, "division");
1532*b725ae77Skettenis 	  break;
1533*b725ae77Skettenis 	case BINOP_REM:
1534*b725ae77Skettenis 	  gen_binop (ax, value, &value1, &value2,
1535*b725ae77Skettenis 		     aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1536*b725ae77Skettenis 	  break;
1537*b725ae77Skettenis 	case BINOP_SUBSCRIPT:
1538*b725ae77Skettenis 	  gen_add (ax, value, &value1, &value2, "array subscripting");
1539*b725ae77Skettenis 	  if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1540*b725ae77Skettenis 	    error ("Illegal combination of types in array subscripting.");
1541*b725ae77Skettenis 	  gen_deref (ax, value);
1542*b725ae77Skettenis 	  break;
1543*b725ae77Skettenis 	case BINOP_BITWISE_AND:
1544*b725ae77Skettenis 	  gen_binop (ax, value, &value1, &value2,
1545*b725ae77Skettenis 		     aop_bit_and, aop_bit_and, 0, "bitwise and");
1546*b725ae77Skettenis 	  break;
1547*b725ae77Skettenis 
1548*b725ae77Skettenis 	case BINOP_BITWISE_IOR:
1549*b725ae77Skettenis 	  gen_binop (ax, value, &value1, &value2,
1550*b725ae77Skettenis 		     aop_bit_or, aop_bit_or, 0, "bitwise or");
1551*b725ae77Skettenis 	  break;
1552*b725ae77Skettenis 
1553*b725ae77Skettenis 	case BINOP_BITWISE_XOR:
1554*b725ae77Skettenis 	  gen_binop (ax, value, &value1, &value2,
1555*b725ae77Skettenis 		     aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1556*b725ae77Skettenis 	  break;
1557*b725ae77Skettenis 
1558*b725ae77Skettenis 	default:
1559*b725ae77Skettenis 	  /* We should only list operators in the outer case statement
1560*b725ae77Skettenis 	     that we actually handle in the inner case statement.  */
1561*b725ae77Skettenis 	  internal_error (__FILE__, __LINE__,
1562*b725ae77Skettenis 			  "gen_expr: op case sets don't match");
1563*b725ae77Skettenis 	}
1564*b725ae77Skettenis       break;
1565*b725ae77Skettenis 
1566*b725ae77Skettenis       /* Note that we need to be a little subtle about generating code
1567*b725ae77Skettenis          for comma.  In C, we can do some optimizations here because
1568*b725ae77Skettenis          we know the left operand is only being evaluated for effect.
1569*b725ae77Skettenis          However, if the tracing kludge is in effect, then we always
1570*b725ae77Skettenis          need to evaluate the left hand side fully, so that all the
1571*b725ae77Skettenis          variables it mentions get traced.  */
1572*b725ae77Skettenis     case BINOP_COMMA:
1573*b725ae77Skettenis       (*pc)++;
1574*b725ae77Skettenis       gen_expr (pc, ax, &value1);
1575*b725ae77Skettenis       /* Don't just dispose of the left operand.  We might be tracing,
1576*b725ae77Skettenis          in which case we want to emit code to trace it if it's an
1577*b725ae77Skettenis          lvalue.  */
1578*b725ae77Skettenis       gen_traced_pop (ax, &value1);
1579*b725ae77Skettenis       gen_expr (pc, ax, value);
1580*b725ae77Skettenis       /* It's the consumer's responsibility to trace the right operand.  */
1581*b725ae77Skettenis       break;
1582*b725ae77Skettenis 
1583*b725ae77Skettenis     case OP_LONG:		/* some integer constant */
1584*b725ae77Skettenis       {
1585*b725ae77Skettenis 	struct type *type = (*pc)[1].type;
1586*b725ae77Skettenis 	LONGEST k = (*pc)[2].longconst;
1587*b725ae77Skettenis 	(*pc) += 4;
1588*b725ae77Skettenis 	gen_int_literal (ax, value, k, type);
1589*b725ae77Skettenis       }
1590*b725ae77Skettenis       break;
1591*b725ae77Skettenis 
1592*b725ae77Skettenis     case OP_VAR_VALUE:
1593*b725ae77Skettenis       gen_var_ref (ax, value, (*pc)[2].symbol);
1594*b725ae77Skettenis       (*pc) += 4;
1595*b725ae77Skettenis       break;
1596*b725ae77Skettenis 
1597*b725ae77Skettenis     case OP_REGISTER:
1598*b725ae77Skettenis       {
1599*b725ae77Skettenis 	int reg = (int) (*pc)[1].longconst;
1600*b725ae77Skettenis 	(*pc) += 3;
1601*b725ae77Skettenis 	value->kind = axs_lvalue_register;
1602*b725ae77Skettenis 	value->u.reg = reg;
1603*b725ae77Skettenis 	value->type = register_type (current_gdbarch, reg);
1604*b725ae77Skettenis       }
1605*b725ae77Skettenis       break;
1606*b725ae77Skettenis 
1607*b725ae77Skettenis     case OP_INTERNALVAR:
1608*b725ae77Skettenis       error ("GDB agent expressions cannot use convenience variables.");
1609*b725ae77Skettenis 
1610*b725ae77Skettenis       /* Weirdo operator: see comments for gen_repeat for details.  */
1611*b725ae77Skettenis     case BINOP_REPEAT:
1612*b725ae77Skettenis       /* Note that gen_repeat handles its own argument evaluation.  */
1613*b725ae77Skettenis       (*pc)++;
1614*b725ae77Skettenis       gen_repeat (pc, ax, value);
1615*b725ae77Skettenis       break;
1616*b725ae77Skettenis 
1617*b725ae77Skettenis     case UNOP_CAST:
1618*b725ae77Skettenis       {
1619*b725ae77Skettenis 	struct type *type = (*pc)[1].type;
1620*b725ae77Skettenis 	(*pc) += 3;
1621*b725ae77Skettenis 	gen_expr (pc, ax, value);
1622*b725ae77Skettenis 	gen_cast (ax, value, type);
1623*b725ae77Skettenis       }
1624*b725ae77Skettenis       break;
1625*b725ae77Skettenis 
1626*b725ae77Skettenis     case UNOP_MEMVAL:
1627*b725ae77Skettenis       {
1628*b725ae77Skettenis 	struct type *type = check_typedef ((*pc)[1].type);
1629*b725ae77Skettenis 	(*pc) += 3;
1630*b725ae77Skettenis 	gen_expr (pc, ax, value);
1631*b725ae77Skettenis 	/* I'm not sure I understand UNOP_MEMVAL entirely.  I think
1632*b725ae77Skettenis 	   it's just a hack for dealing with minsyms; you take some
1633*b725ae77Skettenis 	   integer constant, pretend it's the address of an lvalue of
1634*b725ae77Skettenis 	   the given type, and dereference it.  */
1635*b725ae77Skettenis 	if (value->kind != axs_rvalue)
1636*b725ae77Skettenis 	  /* This would be weird.  */
1637*b725ae77Skettenis 	  internal_error (__FILE__, __LINE__,
1638*b725ae77Skettenis 			  "gen_expr: OP_MEMVAL operand isn't an rvalue???");
1639*b725ae77Skettenis 	value->type = type;
1640*b725ae77Skettenis 	value->kind = axs_lvalue_memory;
1641*b725ae77Skettenis       }
1642*b725ae77Skettenis       break;
1643*b725ae77Skettenis 
1644*b725ae77Skettenis     case UNOP_NEG:
1645*b725ae77Skettenis       (*pc)++;
1646*b725ae77Skettenis       /* -FOO is equivalent to 0 - FOO.  */
1647*b725ae77Skettenis       gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
1648*b725ae77Skettenis       gen_usual_unary (ax, &value1);	/* shouldn't do much */
1649*b725ae77Skettenis       gen_expr (pc, ax, &value2);
1650*b725ae77Skettenis       gen_usual_unary (ax, &value2);
1651*b725ae77Skettenis       gen_usual_arithmetic (ax, &value1, &value2);
1652*b725ae77Skettenis       gen_sub (ax, value, &value1, &value2);
1653*b725ae77Skettenis       break;
1654*b725ae77Skettenis 
1655*b725ae77Skettenis     case UNOP_LOGICAL_NOT:
1656*b725ae77Skettenis       (*pc)++;
1657*b725ae77Skettenis       gen_expr (pc, ax, value);
1658*b725ae77Skettenis       gen_logical_not (ax, value);
1659*b725ae77Skettenis       break;
1660*b725ae77Skettenis 
1661*b725ae77Skettenis     case UNOP_COMPLEMENT:
1662*b725ae77Skettenis       (*pc)++;
1663*b725ae77Skettenis       gen_expr (pc, ax, value);
1664*b725ae77Skettenis       gen_complement (ax, value);
1665*b725ae77Skettenis       break;
1666*b725ae77Skettenis 
1667*b725ae77Skettenis     case UNOP_IND:
1668*b725ae77Skettenis       (*pc)++;
1669*b725ae77Skettenis       gen_expr (pc, ax, value);
1670*b725ae77Skettenis       gen_usual_unary (ax, value);
1671*b725ae77Skettenis       if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1672*b725ae77Skettenis 	error ("Argument of unary `*' is not a pointer.");
1673*b725ae77Skettenis       gen_deref (ax, value);
1674*b725ae77Skettenis       break;
1675*b725ae77Skettenis 
1676*b725ae77Skettenis     case UNOP_ADDR:
1677*b725ae77Skettenis       (*pc)++;
1678*b725ae77Skettenis       gen_expr (pc, ax, value);
1679*b725ae77Skettenis       gen_address_of (ax, value);
1680*b725ae77Skettenis       break;
1681*b725ae77Skettenis 
1682*b725ae77Skettenis     case UNOP_SIZEOF:
1683*b725ae77Skettenis       (*pc)++;
1684*b725ae77Skettenis       /* Notice that gen_sizeof handles its own operand, unlike most
1685*b725ae77Skettenis          of the other unary operator functions.  This is because we
1686*b725ae77Skettenis          have to throw away the code we generate.  */
1687*b725ae77Skettenis       gen_sizeof (pc, ax, value);
1688*b725ae77Skettenis       break;
1689*b725ae77Skettenis 
1690*b725ae77Skettenis     case STRUCTOP_STRUCT:
1691*b725ae77Skettenis     case STRUCTOP_PTR:
1692*b725ae77Skettenis       {
1693*b725ae77Skettenis 	int length = (*pc)[1].longconst;
1694*b725ae77Skettenis 	char *name = &(*pc)[2].string;
1695*b725ae77Skettenis 
1696*b725ae77Skettenis 	(*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1697*b725ae77Skettenis 	gen_expr (pc, ax, value);
1698*b725ae77Skettenis 	if (op == STRUCTOP_STRUCT)
1699*b725ae77Skettenis 	  gen_struct_ref (ax, value, name, ".", "structure or union");
1700*b725ae77Skettenis 	else if (op == STRUCTOP_PTR)
1701*b725ae77Skettenis 	  gen_struct_ref (ax, value, name, "->",
1702*b725ae77Skettenis 			  "pointer to a structure or union");
1703*b725ae77Skettenis 	else
1704*b725ae77Skettenis 	  /* If this `if' chain doesn't handle it, then the case list
1705*b725ae77Skettenis 	     shouldn't mention it, and we shouldn't be here.  */
1706*b725ae77Skettenis 	  internal_error (__FILE__, __LINE__,
1707*b725ae77Skettenis 			  "gen_expr: unhandled struct case");
1708*b725ae77Skettenis       }
1709*b725ae77Skettenis       break;
1710*b725ae77Skettenis 
1711*b725ae77Skettenis     case OP_TYPE:
1712*b725ae77Skettenis       error ("Attempt to use a type name as an expression.");
1713*b725ae77Skettenis 
1714*b725ae77Skettenis     default:
1715*b725ae77Skettenis       error ("Unsupported operator in expression.");
1716*b725ae77Skettenis     }
1717*b725ae77Skettenis }
1718*b725ae77Skettenis 
1719*b725ae77Skettenis 
1720*b725ae77Skettenis 
1721*b725ae77Skettenis /* Generating bytecode from GDB expressions: driver */
1722*b725ae77Skettenis 
1723*b725ae77Skettenis /* Given a GDB expression EXPR, produce a string of agent bytecode
1724*b725ae77Skettenis    which computes its value.  Return the agent expression, and set
1725*b725ae77Skettenis    *VALUE to describe its type, and whether it's an lvalue or rvalue.  */
1726*b725ae77Skettenis struct agent_expr *
expr_to_agent(struct expression * expr,struct axs_value * value)1727*b725ae77Skettenis expr_to_agent (struct expression *expr, struct axs_value *value)
1728*b725ae77Skettenis {
1729*b725ae77Skettenis   struct cleanup *old_chain = 0;
1730*b725ae77Skettenis   struct agent_expr *ax = new_agent_expr (0);
1731*b725ae77Skettenis   union exp_element *pc;
1732*b725ae77Skettenis 
1733*b725ae77Skettenis   old_chain = make_cleanup_free_agent_expr (ax);
1734*b725ae77Skettenis 
1735*b725ae77Skettenis   pc = expr->elts;
1736*b725ae77Skettenis   trace_kludge = 0;
1737*b725ae77Skettenis   gen_expr (&pc, ax, value);
1738*b725ae77Skettenis 
1739*b725ae77Skettenis   /* We have successfully built the agent expr, so cancel the cleanup
1740*b725ae77Skettenis      request.  If we add more cleanups that we always want done, this
1741*b725ae77Skettenis      will have to get more complicated.  */
1742*b725ae77Skettenis   discard_cleanups (old_chain);
1743*b725ae77Skettenis   return ax;
1744*b725ae77Skettenis }
1745*b725ae77Skettenis 
1746*b725ae77Skettenis 
1747*b725ae77Skettenis #if 0				/* not used */
1748*b725ae77Skettenis /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1749*b725ae77Skettenis    string of agent bytecode which will leave its address and size on
1750*b725ae77Skettenis    the top of stack.  Return the agent expression.
1751*b725ae77Skettenis 
1752*b725ae77Skettenis    Not sure this function is useful at all.  */
1753*b725ae77Skettenis struct agent_expr *
1754*b725ae77Skettenis expr_to_address_and_size (struct expression *expr)
1755*b725ae77Skettenis {
1756*b725ae77Skettenis   struct axs_value value;
1757*b725ae77Skettenis   struct agent_expr *ax = expr_to_agent (expr, &value);
1758*b725ae77Skettenis 
1759*b725ae77Skettenis   /* Complain if the result is not a memory lvalue.  */
1760*b725ae77Skettenis   if (value.kind != axs_lvalue_memory)
1761*b725ae77Skettenis     {
1762*b725ae77Skettenis       free_agent_expr (ax);
1763*b725ae77Skettenis       error ("Expression does not denote an object in memory.");
1764*b725ae77Skettenis     }
1765*b725ae77Skettenis 
1766*b725ae77Skettenis   /* Push the object's size on the stack.  */
1767*b725ae77Skettenis   ax_const_l (ax, TYPE_LENGTH (value.type));
1768*b725ae77Skettenis 
1769*b725ae77Skettenis   return ax;
1770*b725ae77Skettenis }
1771*b725ae77Skettenis #endif
1772*b725ae77Skettenis 
1773*b725ae77Skettenis /* Given a GDB expression EXPR, return bytecode to trace its value.
1774*b725ae77Skettenis    The result will use the `trace' and `trace_quick' bytecodes to
1775*b725ae77Skettenis    record the value of all memory touched by the expression.  The
1776*b725ae77Skettenis    caller can then use the ax_reqs function to discover which
1777*b725ae77Skettenis    registers it relies upon.  */
1778*b725ae77Skettenis struct agent_expr *
gen_trace_for_expr(CORE_ADDR scope,struct expression * expr)1779*b725ae77Skettenis gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
1780*b725ae77Skettenis {
1781*b725ae77Skettenis   struct cleanup *old_chain = 0;
1782*b725ae77Skettenis   struct agent_expr *ax = new_agent_expr (scope);
1783*b725ae77Skettenis   union exp_element *pc;
1784*b725ae77Skettenis   struct axs_value value;
1785*b725ae77Skettenis 
1786*b725ae77Skettenis   old_chain = make_cleanup_free_agent_expr (ax);
1787*b725ae77Skettenis 
1788*b725ae77Skettenis   pc = expr->elts;
1789*b725ae77Skettenis   trace_kludge = 1;
1790*b725ae77Skettenis   gen_expr (&pc, ax, &value);
1791*b725ae77Skettenis 
1792*b725ae77Skettenis   /* Make sure we record the final object, and get rid of it.  */
1793*b725ae77Skettenis   gen_traced_pop (ax, &value);
1794*b725ae77Skettenis 
1795*b725ae77Skettenis   /* Oh, and terminate.  */
1796*b725ae77Skettenis   ax_simple (ax, aop_end);
1797*b725ae77Skettenis 
1798*b725ae77Skettenis   /* We have successfully built the agent expr, so cancel the cleanup
1799*b725ae77Skettenis      request.  If we add more cleanups that we always want done, this
1800*b725ae77Skettenis      will have to get more complicated.  */
1801*b725ae77Skettenis   discard_cleanups (old_chain);
1802*b725ae77Skettenis   return ax;
1803*b725ae77Skettenis }
1804*b725ae77Skettenis 
1805*b725ae77Skettenis static void
agent_command(char * exp,int from_tty)1806*b725ae77Skettenis agent_command (char *exp, int from_tty)
1807*b725ae77Skettenis {
1808*b725ae77Skettenis   struct cleanup *old_chain = 0;
1809*b725ae77Skettenis   struct expression *expr;
1810*b725ae77Skettenis   struct agent_expr *agent;
1811*b725ae77Skettenis   struct frame_info *fi = get_current_frame ();	/* need current scope */
1812*b725ae77Skettenis 
1813*b725ae77Skettenis   /* We don't deal with overlay debugging at the moment.  We need to
1814*b725ae77Skettenis      think more carefully about this.  If you copy this code into
1815*b725ae77Skettenis      another command, change the error message; the user shouldn't
1816*b725ae77Skettenis      have to know anything about agent expressions.  */
1817*b725ae77Skettenis   if (overlay_debugging)
1818*b725ae77Skettenis     error ("GDB can't do agent expression translation with overlays.");
1819*b725ae77Skettenis 
1820*b725ae77Skettenis   if (exp == 0)
1821*b725ae77Skettenis     error_no_arg ("expression to translate");
1822*b725ae77Skettenis 
1823*b725ae77Skettenis   expr = parse_expression (exp);
1824*b725ae77Skettenis   old_chain = make_cleanup (free_current_contents, &expr);
1825*b725ae77Skettenis   agent = gen_trace_for_expr (get_frame_pc (fi), expr);
1826*b725ae77Skettenis   make_cleanup_free_agent_expr (agent);
1827*b725ae77Skettenis   ax_print (gdb_stdout, agent);
1828*b725ae77Skettenis 
1829*b725ae77Skettenis   /* It would be nice to call ax_reqs here to gather some general info
1830*b725ae77Skettenis      about the expression, and then print out the result.  */
1831*b725ae77Skettenis 
1832*b725ae77Skettenis   do_cleanups (old_chain);
1833*b725ae77Skettenis   dont_repeat ();
1834*b725ae77Skettenis }
1835*b725ae77Skettenis 
1836*b725ae77Skettenis 
1837*b725ae77Skettenis /* Initialization code.  */
1838*b725ae77Skettenis 
1839*b725ae77Skettenis void _initialize_ax_gdb (void);
1840*b725ae77Skettenis void
_initialize_ax_gdb(void)1841*b725ae77Skettenis _initialize_ax_gdb (void)
1842*b725ae77Skettenis {
1843*b725ae77Skettenis   add_cmd ("agent", class_maintenance, agent_command,
1844*b725ae77Skettenis 	   "Translate an expression into remote agent bytecode.",
1845*b725ae77Skettenis 	   &maintenancelist);
1846*b725ae77Skettenis }
1847