xref: /dragonfly/contrib/gdb-7/gdb/ax-gdb.c (revision ad9f8794)
1 /* GDB-specific functions for operating on agent expressions.
2 
3    Copyright (C) 1998, 1999, 2000, 2001, 2003, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5 
6    This file is part of GDB.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20 
21 #include "defs.h"
22 #include "symtab.h"
23 #include "symfile.h"
24 #include "gdbtypes.h"
25 #include "language.h"
26 #include "value.h"
27 #include "expression.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "frame.h"
31 #include "target.h"
32 #include "ax.h"
33 #include "ax-gdb.h"
34 #include "gdb_string.h"
35 #include "block.h"
36 #include "regcache.h"
37 #include "user-regs.h"
38 #include "language.h"
39 #include "dictionary.h"
40 #include "breakpoint.h"
41 #include "tracepoint.h"
42 #include "cp-support.h"
43 
44 /* To make sense of this file, you should read doc/agentexpr.texi.
45    Then look at the types and enums in ax-gdb.h.  For the code itself,
46    look at gen_expr, towards the bottom; that's the main function that
47    looks at the GDB expressions and calls everything else to generate
48    code.
49 
50    I'm beginning to wonder whether it wouldn't be nicer to internally
51    generate trees, with types, and then spit out the bytecode in
52    linear form afterwards; we could generate fewer `swap', `ext', and
53    `zero_ext' bytecodes that way; it would make good constant folding
54    easier, too.  But at the moment, I think we should be willing to
55    pay for the simplicity of this code with less-than-optimal bytecode
56    strings.
57 
58    Remember, "GBD" stands for "Great Britain, Dammit!"  So be careful.  */
59 
60 
61 
62 /* Prototypes for local functions. */
63 
64 /* There's a standard order to the arguments of these functions:
65    union exp_element ** --- pointer into expression
66    struct agent_expr * --- agent expression buffer to generate code into
67    struct axs_value * --- describes value left on top of stack  */
68 
69 static struct value *const_var_ref (struct symbol *var);
70 static struct value *const_expr (union exp_element **pc);
71 static struct value *maybe_const_expr (union exp_element **pc);
72 
73 static void gen_traced_pop (struct gdbarch *, struct agent_expr *, struct axs_value *);
74 
75 static void gen_sign_extend (struct agent_expr *, struct type *);
76 static void gen_extend (struct agent_expr *, struct type *);
77 static void gen_fetch (struct agent_expr *, struct type *);
78 static void gen_left_shift (struct agent_expr *, int);
79 
80 
81 static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
82 static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
83 static void gen_offset (struct agent_expr *ax, int offset);
84 static void gen_sym_offset (struct agent_expr *, struct symbol *);
85 static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
86 			 struct axs_value *value, struct symbol *var);
87 
88 
89 static void gen_int_literal (struct agent_expr *ax,
90 			     struct axs_value *value,
91 			     LONGEST k, struct type *type);
92 
93 
94 static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
95 static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
96 			     struct axs_value *value);
97 static int type_wider_than (struct type *type1, struct type *type2);
98 static struct type *max_type (struct type *type1, struct type *type2);
99 static void gen_conversion (struct agent_expr *ax,
100 			    struct type *from, struct type *to);
101 static int is_nontrivial_conversion (struct type *from, struct type *to);
102 static void gen_usual_arithmetic (struct expression *exp,
103 				  struct agent_expr *ax,
104 				  struct axs_value *value1,
105 				  struct axs_value *value2);
106 static void gen_integral_promotions (struct expression *exp,
107 				     struct agent_expr *ax,
108 				     struct axs_value *value);
109 static void gen_cast (struct agent_expr *ax,
110 		      struct axs_value *value, struct type *type);
111 static void gen_scale (struct agent_expr *ax,
112 		       enum agent_op op, struct type *type);
113 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
114 			struct axs_value *value1, struct axs_value *value2);
115 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
116 			struct axs_value *value1, struct axs_value *value2);
117 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
118 			 struct axs_value *value1, struct axs_value *value2,
119 			 struct type *result_type);
120 static void gen_binop (struct agent_expr *ax,
121 		       struct axs_value *value,
122 		       struct axs_value *value1,
123 		       struct axs_value *value2,
124 		       enum agent_op op,
125 		       enum agent_op op_unsigned, int may_carry, char *name);
126 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
127 			     struct type *result_type);
128 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
129 static void gen_deref (struct agent_expr *, struct axs_value *);
130 static void gen_address_of (struct agent_expr *, struct axs_value *);
131 static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
132 			      struct axs_value *value,
133 			      struct type *type, int start, int end);
134 static void gen_primitive_field (struct expression *exp,
135 				 struct agent_expr *ax,
136 				 struct axs_value *value,
137 				 int offset, int fieldno, struct type *type);
138 static int gen_struct_ref_recursive (struct expression *exp,
139 				     struct agent_expr *ax,
140 				     struct axs_value *value,
141 				     char *field, int offset,
142 				     struct type *type);
143 static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
144 			    struct axs_value *value,
145 			    char *field,
146 			    char *operator_name, char *operand_name);
147 static void gen_static_field (struct gdbarch *gdbarch,
148 			      struct agent_expr *ax, struct axs_value *value,
149 			      struct type *type, int fieldno);
150 static void gen_repeat (struct expression *exp, union exp_element **pc,
151 			struct agent_expr *ax, struct axs_value *value);
152 static void gen_sizeof (struct expression *exp, union exp_element **pc,
153 			struct agent_expr *ax, struct axs_value *value,
154 			struct type *size_type);
155 static void gen_expr (struct expression *exp, union exp_element **pc,
156 		      struct agent_expr *ax, struct axs_value *value);
157 static void gen_expr_binop_rest (struct expression *exp,
158 				 enum exp_opcode op, union exp_element **pc,
159 				 struct agent_expr *ax,
160 				 struct axs_value *value,
161 				 struct axs_value *value1,
162 				 struct axs_value *value2);
163 
164 static void agent_command (char *exp, int from_tty);
165 
166 
167 /* Detecting constant expressions.  */
168 
169 /* If the variable reference at *PC is a constant, return its value.
170    Otherwise, return zero.
171 
172    Hey, Wally!  How can a variable reference be a constant?
173 
174    Well, Beav, this function really handles the OP_VAR_VALUE operator,
175    not specifically variable references.  GDB uses OP_VAR_VALUE to
176    refer to any kind of symbolic reference: function names, enum
177    elements, and goto labels are all handled through the OP_VAR_VALUE
178    operator, even though they're constants.  It makes sense given the
179    situation.
180 
181    Gee, Wally, don'cha wonder sometimes if data representations that
182    subvert commonly accepted definitions of terms in favor of heavily
183    context-specific interpretations are really just a tool of the
184    programming hegemony to preserve their power and exclude the
185    proletariat?  */
186 
187 static struct value *
188 const_var_ref (struct symbol *var)
189 {
190   struct type *type = SYMBOL_TYPE (var);
191 
192   switch (SYMBOL_CLASS (var))
193     {
194     case LOC_CONST:
195       return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
196 
197     case LOC_LABEL:
198       return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
199 
200     default:
201       return 0;
202     }
203 }
204 
205 
206 /* If the expression starting at *PC has a constant value, return it.
207    Otherwise, return zero.  If we return a value, then *PC will be
208    advanced to the end of it.  If we return zero, *PC could be
209    anywhere.  */
210 static struct value *
211 const_expr (union exp_element **pc)
212 {
213   enum exp_opcode op = (*pc)->opcode;
214   struct value *v1;
215 
216   switch (op)
217     {
218     case OP_LONG:
219       {
220 	struct type *type = (*pc)[1].type;
221 	LONGEST k = (*pc)[2].longconst;
222 
223 	(*pc) += 4;
224 	return value_from_longest (type, k);
225       }
226 
227     case OP_VAR_VALUE:
228       {
229 	struct value *v = const_var_ref ((*pc)[2].symbol);
230 
231 	(*pc) += 4;
232 	return v;
233       }
234 
235       /* We could add more operators in here.  */
236 
237     case UNOP_NEG:
238       (*pc)++;
239       v1 = const_expr (pc);
240       if (v1)
241 	return value_neg (v1);
242       else
243 	return 0;
244 
245     default:
246       return 0;
247     }
248 }
249 
250 
251 /* Like const_expr, but guarantee also that *PC is undisturbed if the
252    expression is not constant.  */
253 static struct value *
254 maybe_const_expr (union exp_element **pc)
255 {
256   union exp_element *tentative_pc = *pc;
257   struct value *v = const_expr (&tentative_pc);
258 
259   /* If we got a value, then update the real PC.  */
260   if (v)
261     *pc = tentative_pc;
262 
263   return v;
264 }
265 
266 
267 /* Generating bytecode from GDB expressions: general assumptions */
268 
269 /* Here are a few general assumptions made throughout the code; if you
270    want to make a change that contradicts one of these, then you'd
271    better scan things pretty thoroughly.
272 
273    - We assume that all values occupy one stack element.  For example,
274    sometimes we'll swap to get at the left argument to a binary
275    operator.  If we decide that void values should occupy no stack
276    elements, or that synthetic arrays (whose size is determined at
277    run time, created by the `@' operator) should occupy two stack
278    elements (address and length), then this will cause trouble.
279 
280    - We assume the stack elements are infinitely wide, and that we
281    don't have to worry what happens if the user requests an
282    operation that is wider than the actual interpreter's stack.
283    That is, it's up to the interpreter to handle directly all the
284    integer widths the user has access to.  (Woe betide the language
285    with bignums!)
286 
287    - We don't support side effects.  Thus, we don't have to worry about
288    GCC's generalized lvalues, function calls, etc.
289 
290    - We don't support floating point.  Many places where we switch on
291    some type don't bother to include cases for floating point; there
292    may be even more subtle ways this assumption exists.  For
293    example, the arguments to % must be integers.
294 
295    - We assume all subexpressions have a static, unchanging type.  If
296    we tried to support convenience variables, this would be a
297    problem.
298 
299    - All values on the stack should always be fully zero- or
300    sign-extended.
301 
302    (I wasn't sure whether to choose this or its opposite --- that
303    only addresses are assumed extended --- but it turns out that
304    neither convention completely eliminates spurious extend
305    operations (if everything is always extended, then you have to
306    extend after add, because it could overflow; if nothing is
307    extended, then you end up producing extends whenever you change
308    sizes), and this is simpler.)  */
309 
310 
311 /* Generating bytecode from GDB expressions: the `trace' kludge  */
312 
313 /* The compiler in this file is a general-purpose mechanism for
314    translating GDB expressions into bytecode.  One ought to be able to
315    find a million and one uses for it.
316 
317    However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
318    of expediency.  Let he who is without sin cast the first stone.
319 
320    For the data tracing facility, we need to insert `trace' bytecodes
321    before each data fetch; this records all the memory that the
322    expression touches in the course of evaluation, so that memory will
323    be available when the user later tries to evaluate the expression
324    in GDB.
325 
326    This should be done (I think) in a post-processing pass, that walks
327    an arbitrary agent expression and inserts `trace' operations at the
328    appropriate points.  But it's much faster to just hack them
329    directly into the code.  And since we're in a crunch, that's what
330    I've done.
331 
332    Setting the flag trace_kludge to non-zero enables the code that
333    emits the trace bytecodes at the appropriate points.  */
334 int trace_kludge;
335 
336 /* Scan for all static fields in the given class, including any base
337    classes, and generate tracing bytecodes for each.  */
338 
339 static void
340 gen_trace_static_fields (struct gdbarch *gdbarch,
341 			 struct agent_expr *ax,
342 			 struct type *type)
343 {
344   int i, nbases = TYPE_N_BASECLASSES (type);
345   struct axs_value value;
346 
347   CHECK_TYPEDEF (type);
348 
349   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
350     {
351       if (field_is_static (&TYPE_FIELD (type, i)))
352 	{
353 	  gen_static_field (gdbarch, ax, &value, type, i);
354 	  if (value.optimized_out)
355 	    continue;
356 	  switch (value.kind)
357 	    {
358 	    case axs_lvalue_memory:
359 	      {
360 		int length = TYPE_LENGTH (check_typedef (value.type));
361 
362 		ax_const_l (ax, length);
363 		ax_simple (ax, aop_trace);
364 	      }
365 	      break;
366 
367 	    case axs_lvalue_register:
368 	      /* We don't actually need the register's value to be pushed,
369 		 just note that we need it to be collected.  */
370 	      ax_reg_mask (ax, value.u.reg);
371 
372 	    default:
373 	      break;
374 	    }
375 	}
376     }
377 
378   /* Now scan through base classes recursively.  */
379   for (i = 0; i < nbases; i++)
380     {
381       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
382 
383       gen_trace_static_fields (gdbarch, ax, basetype);
384     }
385 }
386 
387 /* Trace the lvalue on the stack, if it needs it.  In either case, pop
388    the value.  Useful on the left side of a comma, and at the end of
389    an expression being used for tracing.  */
390 static void
391 gen_traced_pop (struct gdbarch *gdbarch,
392 		struct agent_expr *ax, struct axs_value *value)
393 {
394   if (trace_kludge)
395     switch (value->kind)
396       {
397       case axs_rvalue:
398 	/* We don't trace rvalues, just the lvalues necessary to
399 	   produce them.  So just dispose of this value.  */
400 	ax_simple (ax, aop_pop);
401 	break;
402 
403       case axs_lvalue_memory:
404 	{
405 	  int length = TYPE_LENGTH (check_typedef (value->type));
406 
407 	  /* There's no point in trying to use a trace_quick bytecode
408 	     here, since "trace_quick SIZE pop" is three bytes, whereas
409 	     "const8 SIZE trace" is also three bytes, does the same
410 	     thing, and the simplest code which generates that will also
411 	     work correctly for objects with large sizes.  */
412 	  ax_const_l (ax, length);
413 	  ax_simple (ax, aop_trace);
414 	}
415 	break;
416 
417       case axs_lvalue_register:
418 	/* We don't actually need the register's value to be on the
419 	   stack, and the target will get heartburn if the register is
420 	   larger than will fit in a stack, so just mark it for
421 	   collection and be done with it.  */
422 	ax_reg_mask (ax, value->u.reg);
423 	break;
424       }
425   else
426     /* If we're not tracing, just pop the value.  */
427     ax_simple (ax, aop_pop);
428 
429   /* To trace C++ classes with static fields stored elsewhere.  */
430   if (trace_kludge
431       && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
432 	  || TYPE_CODE (value->type) == TYPE_CODE_UNION))
433     gen_trace_static_fields (gdbarch, ax, value->type);
434 }
435 
436 
437 
438 /* Generating bytecode from GDB expressions: helper functions */
439 
440 /* Assume that the lower bits of the top of the stack is a value of
441    type TYPE, and the upper bits are zero.  Sign-extend if necessary.  */
442 static void
443 gen_sign_extend (struct agent_expr *ax, struct type *type)
444 {
445   /* Do we need to sign-extend this?  */
446   if (!TYPE_UNSIGNED (type))
447     ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
448 }
449 
450 
451 /* Assume the lower bits of the top of the stack hold a value of type
452    TYPE, and the upper bits are garbage.  Sign-extend or truncate as
453    needed.  */
454 static void
455 gen_extend (struct agent_expr *ax, struct type *type)
456 {
457   int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
458 
459   /* I just had to.  */
460   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
461 }
462 
463 
464 /* Assume that the top of the stack contains a value of type "pointer
465    to TYPE"; generate code to fetch its value.  Note that TYPE is the
466    target type, not the pointer type.  */
467 static void
468 gen_fetch (struct agent_expr *ax, struct type *type)
469 {
470   if (trace_kludge)
471     {
472       /* Record the area of memory we're about to fetch.  */
473       ax_trace_quick (ax, TYPE_LENGTH (type));
474     }
475 
476   switch (TYPE_CODE (type))
477     {
478     case TYPE_CODE_PTR:
479     case TYPE_CODE_REF:
480     case TYPE_CODE_ENUM:
481     case TYPE_CODE_INT:
482     case TYPE_CODE_CHAR:
483     case TYPE_CODE_BOOL:
484       /* It's a scalar value, so we know how to dereference it.  How
485          many bytes long is it?  */
486       switch (TYPE_LENGTH (type))
487 	{
488 	case 8 / TARGET_CHAR_BIT:
489 	  ax_simple (ax, aop_ref8);
490 	  break;
491 	case 16 / TARGET_CHAR_BIT:
492 	  ax_simple (ax, aop_ref16);
493 	  break;
494 	case 32 / TARGET_CHAR_BIT:
495 	  ax_simple (ax, aop_ref32);
496 	  break;
497 	case 64 / TARGET_CHAR_BIT:
498 	  ax_simple (ax, aop_ref64);
499 	  break;
500 
501 	  /* Either our caller shouldn't have asked us to dereference
502 	     that pointer (other code's fault), or we're not
503 	     implementing something we should be (this code's fault).
504 	     In any case, it's a bug the user shouldn't see.  */
505 	default:
506 	  internal_error (__FILE__, __LINE__,
507 			  _("gen_fetch: strange size"));
508 	}
509 
510       gen_sign_extend (ax, type);
511       break;
512 
513     default:
514       /* Either our caller shouldn't have asked us to dereference that
515          pointer (other code's fault), or we're not implementing
516          something we should be (this code's fault).  In any case,
517          it's a bug the user shouldn't see.  */
518       internal_error (__FILE__, __LINE__,
519 		      _("gen_fetch: bad type code"));
520     }
521 }
522 
523 
524 /* Generate code to left shift the top of the stack by DISTANCE bits, or
525    right shift it by -DISTANCE bits if DISTANCE < 0.  This generates
526    unsigned (logical) right shifts.  */
527 static void
528 gen_left_shift (struct agent_expr *ax, int distance)
529 {
530   if (distance > 0)
531     {
532       ax_const_l (ax, distance);
533       ax_simple (ax, aop_lsh);
534     }
535   else if (distance < 0)
536     {
537       ax_const_l (ax, -distance);
538       ax_simple (ax, aop_rsh_unsigned);
539     }
540 }
541 
542 
543 
544 /* Generating bytecode from GDB expressions: symbol references */
545 
546 /* Generate code to push the base address of the argument portion of
547    the top stack frame.  */
548 static void
549 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
550 {
551   int frame_reg;
552   LONGEST frame_offset;
553 
554   gdbarch_virtual_frame_pointer (gdbarch,
555 				 ax->scope, &frame_reg, &frame_offset);
556   ax_reg (ax, frame_reg);
557   gen_offset (ax, frame_offset);
558 }
559 
560 
561 /* Generate code to push the base address of the locals portion of the
562    top stack frame.  */
563 static void
564 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
565 {
566   int frame_reg;
567   LONGEST frame_offset;
568 
569   gdbarch_virtual_frame_pointer (gdbarch,
570 				 ax->scope, &frame_reg, &frame_offset);
571   ax_reg (ax, frame_reg);
572   gen_offset (ax, frame_offset);
573 }
574 
575 
576 /* Generate code to add OFFSET to the top of the stack.  Try to
577    generate short and readable code.  We use this for getting to
578    variables on the stack, and structure members.  If we were
579    programming in ML, it would be clearer why these are the same
580    thing.  */
581 static void
582 gen_offset (struct agent_expr *ax, int offset)
583 {
584   /* It would suffice to simply push the offset and add it, but this
585      makes it easier to read positive and negative offsets in the
586      bytecode.  */
587   if (offset > 0)
588     {
589       ax_const_l (ax, offset);
590       ax_simple (ax, aop_add);
591     }
592   else if (offset < 0)
593     {
594       ax_const_l (ax, -offset);
595       ax_simple (ax, aop_sub);
596     }
597 }
598 
599 
600 /* In many cases, a symbol's value is the offset from some other
601    address (stack frame, base register, etc.)  Generate code to add
602    VAR's value to the top of the stack.  */
603 static void
604 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
605 {
606   gen_offset (ax, SYMBOL_VALUE (var));
607 }
608 
609 
610 /* Generate code for a variable reference to AX.  The variable is the
611    symbol VAR.  Set VALUE to describe the result.  */
612 
613 static void
614 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
615 	     struct axs_value *value, struct symbol *var)
616 {
617   /* Dereference any typedefs. */
618   value->type = check_typedef (SYMBOL_TYPE (var));
619   value->optimized_out = 0;
620 
621   /* I'm imitating the code in read_var_value.  */
622   switch (SYMBOL_CLASS (var))
623     {
624     case LOC_CONST:		/* A constant, like an enum value.  */
625       ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
626       value->kind = axs_rvalue;
627       break;
628 
629     case LOC_LABEL:		/* A goto label, being used as a value.  */
630       ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
631       value->kind = axs_rvalue;
632       break;
633 
634     case LOC_CONST_BYTES:
635       internal_error (__FILE__, __LINE__,
636 		      _("gen_var_ref: LOC_CONST_BYTES symbols are not supported"));
637 
638       /* Variable at a fixed location in memory.  Easy.  */
639     case LOC_STATIC:
640       /* Push the address of the variable.  */
641       ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
642       value->kind = axs_lvalue_memory;
643       break;
644 
645     case LOC_ARG:		/* var lives in argument area of frame */
646       gen_frame_args_address (gdbarch, ax);
647       gen_sym_offset (ax, var);
648       value->kind = axs_lvalue_memory;
649       break;
650 
651     case LOC_REF_ARG:		/* As above, but the frame slot really
652 				   holds the address of the variable.  */
653       gen_frame_args_address (gdbarch, ax);
654       gen_sym_offset (ax, var);
655       /* Don't assume any particular pointer size.  */
656       gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
657       value->kind = axs_lvalue_memory;
658       break;
659 
660     case LOC_LOCAL:		/* var lives in locals area of frame */
661       gen_frame_locals_address (gdbarch, ax);
662       gen_sym_offset (ax, var);
663       value->kind = axs_lvalue_memory;
664       break;
665 
666     case LOC_TYPEDEF:
667       error (_("Cannot compute value of typedef `%s'."),
668 	     SYMBOL_PRINT_NAME (var));
669       break;
670 
671     case LOC_BLOCK:
672       ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
673       value->kind = axs_rvalue;
674       break;
675 
676     case LOC_REGISTER:
677       /* Don't generate any code at all; in the process of treating
678          this as an lvalue or rvalue, the caller will generate the
679          right code.  */
680       value->kind = axs_lvalue_register;
681       value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
682       break;
683 
684       /* A lot like LOC_REF_ARG, but the pointer lives directly in a
685          register, not on the stack.  Simpler than LOC_REGISTER
686          because it's just like any other case where the thing
687 	 has a real address.  */
688     case LOC_REGPARM_ADDR:
689       ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
690       value->kind = axs_lvalue_memory;
691       break;
692 
693     case LOC_UNRESOLVED:
694       {
695 	struct minimal_symbol *msym
696 	  = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
697 
698 	if (!msym)
699 	  error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
700 
701 	/* Push the address of the variable.  */
702 	ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
703 	value->kind = axs_lvalue_memory;
704       }
705       break;
706 
707     case LOC_COMPUTED:
708       /* FIXME: cagney/2004-01-26: It should be possible to
709 	 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
710 	 Unfortunately DWARF 2 stores the frame-base (instead of the
711 	 function) location in a function's symbol.  Oops!  For the
712 	 moment enable this when/where applicable.  */
713       SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
714       break;
715 
716     case LOC_OPTIMIZED_OUT:
717       /* Flag this, but don't say anything; leave it up to callers to
718 	 warn the user.  */
719       value->optimized_out = 1;
720       break;
721 
722     default:
723       error (_("Cannot find value of botched symbol `%s'."),
724 	     SYMBOL_PRINT_NAME (var));
725       break;
726     }
727 }
728 
729 
730 
731 /* Generating bytecode from GDB expressions: literals */
732 
733 static void
734 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
735 		 struct type *type)
736 {
737   ax_const_l (ax, k);
738   value->kind = axs_rvalue;
739   value->type = check_typedef (type);
740 }
741 
742 
743 
744 /* Generating bytecode from GDB expressions: unary conversions, casts */
745 
746 /* Take what's on the top of the stack (as described by VALUE), and
747    try to make an rvalue out of it.  Signal an error if we can't do
748    that.  */
749 static void
750 require_rvalue (struct agent_expr *ax, struct axs_value *value)
751 {
752   /* Only deal with scalars, structs and such may be too large
753      to fit in a stack entry.  */
754   value->type = check_typedef (value->type);
755   if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
756       || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
757       || TYPE_CODE (value->type) == TYPE_CODE_UNION
758       || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
759     error (_("Value not scalar: cannot be an rvalue."));
760 
761   switch (value->kind)
762     {
763     case axs_rvalue:
764       /* It's already an rvalue.  */
765       break;
766 
767     case axs_lvalue_memory:
768       /* The top of stack is the address of the object.  Dereference.  */
769       gen_fetch (ax, value->type);
770       break;
771 
772     case axs_lvalue_register:
773       /* There's nothing on the stack, but value->u.reg is the
774          register number containing the value.
775 
776          When we add floating-point support, this is going to have to
777          change.  What about SPARC register pairs, for example?  */
778       ax_reg (ax, value->u.reg);
779       gen_extend (ax, value->type);
780       break;
781     }
782 
783   value->kind = axs_rvalue;
784 }
785 
786 
787 /* Assume the top of the stack is described by VALUE, and perform the
788    usual unary conversions.  This is motivated by ANSI 6.2.2, but of
789    course GDB expressions are not ANSI; they're the mishmash union of
790    a bunch of languages.  Rah.
791 
792    NOTE!  This function promises to produce an rvalue only when the
793    incoming value is of an appropriate type.  In other words, the
794    consumer of the value this function produces may assume the value
795    is an rvalue only after checking its type.
796 
797    The immediate issue is that if the user tries to use a structure or
798    union as an operand of, say, the `+' operator, we don't want to try
799    to convert that structure to an rvalue; require_rvalue will bomb on
800    structs and unions.  Rather, we want to simply pass the struct
801    lvalue through unchanged, and let `+' raise an error.  */
802 
803 static void
804 gen_usual_unary (struct expression *exp, struct agent_expr *ax,
805 		 struct axs_value *value)
806 {
807   /* We don't have to generate any code for the usual integral
808      conversions, since values are always represented as full-width on
809      the stack.  Should we tweak the type?  */
810 
811   /* Some types require special handling.  */
812   switch (TYPE_CODE (value->type))
813     {
814       /* Functions get converted to a pointer to the function.  */
815     case TYPE_CODE_FUNC:
816       value->type = lookup_pointer_type (value->type);
817       value->kind = axs_rvalue;	/* Should always be true, but just in case.  */
818       break;
819 
820       /* Arrays get converted to a pointer to their first element, and
821          are no longer an lvalue.  */
822     case TYPE_CODE_ARRAY:
823       {
824 	struct type *elements = TYPE_TARGET_TYPE (value->type);
825 
826 	value->type = lookup_pointer_type (elements);
827 	value->kind = axs_rvalue;
828 	/* We don't need to generate any code; the address of the array
829 	   is also the address of its first element.  */
830       }
831       break;
832 
833       /* Don't try to convert structures and unions to rvalues.  Let the
834          consumer signal an error.  */
835     case TYPE_CODE_STRUCT:
836     case TYPE_CODE_UNION:
837       return;
838 
839       /* If the value is an enum or a bool, call it an integer.  */
840     case TYPE_CODE_ENUM:
841     case TYPE_CODE_BOOL:
842       value->type = builtin_type (exp->gdbarch)->builtin_int;
843       break;
844     }
845 
846   /* If the value is an lvalue, dereference it.  */
847   require_rvalue (ax, value);
848 }
849 
850 
851 /* Return non-zero iff the type TYPE1 is considered "wider" than the
852    type TYPE2, according to the rules described in gen_usual_arithmetic.  */
853 static int
854 type_wider_than (struct type *type1, struct type *type2)
855 {
856   return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
857 	  || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
858 	      && TYPE_UNSIGNED (type1)
859 	      && !TYPE_UNSIGNED (type2)));
860 }
861 
862 
863 /* Return the "wider" of the two types TYPE1 and TYPE2.  */
864 static struct type *
865 max_type (struct type *type1, struct type *type2)
866 {
867   return type_wider_than (type1, type2) ? type1 : type2;
868 }
869 
870 
871 /* Generate code to convert a scalar value of type FROM to type TO.  */
872 static void
873 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
874 {
875   /* Perhaps there is a more graceful way to state these rules.  */
876 
877   /* If we're converting to a narrower type, then we need to clear out
878      the upper bits.  */
879   if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
880     gen_extend (ax, from);
881 
882   /* If the two values have equal width, but different signednesses,
883      then we need to extend.  */
884   else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
885     {
886       if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
887 	gen_extend (ax, to);
888     }
889 
890   /* If we're converting to a wider type, and becoming unsigned, then
891      we need to zero out any possible sign bits.  */
892   else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
893     {
894       if (TYPE_UNSIGNED (to))
895 	gen_extend (ax, to);
896     }
897 }
898 
899 
900 /* Return non-zero iff the type FROM will require any bytecodes to be
901    emitted to be converted to the type TO.  */
902 static int
903 is_nontrivial_conversion (struct type *from, struct type *to)
904 {
905   struct agent_expr *ax = new_agent_expr (NULL, 0);
906   int nontrivial;
907 
908   /* Actually generate the code, and see if anything came out.  At the
909      moment, it would be trivial to replicate the code in
910      gen_conversion here, but in the future, when we're supporting
911      floating point and the like, it may not be.  Doing things this
912      way allows this function to be independent of the logic in
913      gen_conversion.  */
914   gen_conversion (ax, from, to);
915   nontrivial = ax->len > 0;
916   free_agent_expr (ax);
917   return nontrivial;
918 }
919 
920 
921 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
922    6.2.1.5) for the two operands of an arithmetic operator.  This
923    effectively finds a "least upper bound" type for the two arguments,
924    and promotes each argument to that type.  *VALUE1 and *VALUE2
925    describe the values as they are passed in, and as they are left.  */
926 static void
927 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
928 		      struct axs_value *value1, struct axs_value *value2)
929 {
930   /* Do the usual binary conversions.  */
931   if (TYPE_CODE (value1->type) == TYPE_CODE_INT
932       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
933     {
934       /* The ANSI integral promotions seem to work this way: Order the
935          integer types by size, and then by signedness: an n-bit
936          unsigned type is considered "wider" than an n-bit signed
937          type.  Promote to the "wider" of the two types, and always
938          promote at least to int.  */
939       struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
940 				      max_type (value1->type, value2->type));
941 
942       /* Deal with value2, on the top of the stack.  */
943       gen_conversion (ax, value2->type, target);
944 
945       /* Deal with value1, not on the top of the stack.  Don't
946          generate the `swap' instructions if we're not actually going
947          to do anything.  */
948       if (is_nontrivial_conversion (value1->type, target))
949 	{
950 	  ax_simple (ax, aop_swap);
951 	  gen_conversion (ax, value1->type, target);
952 	  ax_simple (ax, aop_swap);
953 	}
954 
955       value1->type = value2->type = check_typedef (target);
956     }
957 }
958 
959 
960 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
961    the value on the top of the stack, as described by VALUE.  Assume
962    the value has integral type.  */
963 static void
964 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
965 			 struct axs_value *value)
966 {
967   const struct builtin_type *builtin = builtin_type (exp->gdbarch);
968 
969   if (!type_wider_than (value->type, builtin->builtin_int))
970     {
971       gen_conversion (ax, value->type, builtin->builtin_int);
972       value->type = builtin->builtin_int;
973     }
974   else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
975     {
976       gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
977       value->type = builtin->builtin_unsigned_int;
978     }
979 }
980 
981 
982 /* Generate code for a cast to TYPE.  */
983 static void
984 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
985 {
986   /* GCC does allow casts to yield lvalues, so this should be fixed
987      before merging these changes into the trunk.  */
988   require_rvalue (ax, value);
989   /* Dereference typedefs. */
990   type = check_typedef (type);
991 
992   switch (TYPE_CODE (type))
993     {
994     case TYPE_CODE_PTR:
995     case TYPE_CODE_REF:
996       /* It's implementation-defined, and I'll bet this is what GCC
997          does.  */
998       break;
999 
1000     case TYPE_CODE_ARRAY:
1001     case TYPE_CODE_STRUCT:
1002     case TYPE_CODE_UNION:
1003     case TYPE_CODE_FUNC:
1004       error (_("Invalid type cast: intended type must be scalar."));
1005 
1006     case TYPE_CODE_ENUM:
1007     case TYPE_CODE_BOOL:
1008       /* We don't have to worry about the size of the value, because
1009          all our integral values are fully sign-extended, and when
1010          casting pointers we can do anything we like.  Is there any
1011          way for us to know what GCC actually does with a cast like
1012          this?  */
1013       break;
1014 
1015     case TYPE_CODE_INT:
1016       gen_conversion (ax, value->type, type);
1017       break;
1018 
1019     case TYPE_CODE_VOID:
1020       /* We could pop the value, and rely on everyone else to check
1021          the type and notice that this value doesn't occupy a stack
1022          slot.  But for now, leave the value on the stack, and
1023          preserve the "value == stack element" assumption.  */
1024       break;
1025 
1026     default:
1027       error (_("Casts to requested type are not yet implemented."));
1028     }
1029 
1030   value->type = type;
1031 }
1032 
1033 
1034 
1035 /* Generating bytecode from GDB expressions: arithmetic */
1036 
1037 /* Scale the integer on the top of the stack by the size of the target
1038    of the pointer type TYPE.  */
1039 static void
1040 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1041 {
1042   struct type *element = TYPE_TARGET_TYPE (type);
1043 
1044   if (TYPE_LENGTH (element) != 1)
1045     {
1046       ax_const_l (ax, TYPE_LENGTH (element));
1047       ax_simple (ax, op);
1048     }
1049 }
1050 
1051 
1052 /* Generate code for pointer arithmetic PTR + INT.  */
1053 static void
1054 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1055 	    struct axs_value *value1, struct axs_value *value2)
1056 {
1057   gdb_assert (pointer_type (value1->type));
1058   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1059 
1060   gen_scale (ax, aop_mul, value1->type);
1061   ax_simple (ax, aop_add);
1062   gen_extend (ax, value1->type);	/* Catch overflow.  */
1063   value->type = value1->type;
1064   value->kind = axs_rvalue;
1065 }
1066 
1067 
1068 /* Generate code for pointer arithmetic PTR - INT.  */
1069 static void
1070 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1071 	    struct axs_value *value1, struct axs_value *value2)
1072 {
1073   gdb_assert (pointer_type (value1->type));
1074   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1075 
1076   gen_scale (ax, aop_mul, value1->type);
1077   ax_simple (ax, aop_sub);
1078   gen_extend (ax, value1->type);	/* Catch overflow.  */
1079   value->type = value1->type;
1080   value->kind = axs_rvalue;
1081 }
1082 
1083 
1084 /* Generate code for pointer arithmetic PTR - PTR.  */
1085 static void
1086 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1087 	     struct axs_value *value1, struct axs_value *value2,
1088 	     struct type *result_type)
1089 {
1090   gdb_assert (pointer_type (value1->type));
1091   gdb_assert (pointer_type (value2->type));
1092 
1093   if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1094       != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1095     error (_("\
1096 First argument of `-' is a pointer, but second argument is neither\n\
1097 an integer nor a pointer of the same type."));
1098 
1099   ax_simple (ax, aop_sub);
1100   gen_scale (ax, aop_div_unsigned, value1->type);
1101   value->type = result_type;
1102   value->kind = axs_rvalue;
1103 }
1104 
1105 static void
1106 gen_equal (struct agent_expr *ax, struct axs_value *value,
1107 	   struct axs_value *value1, struct axs_value *value2,
1108 	   struct type *result_type)
1109 {
1110   if (pointer_type (value1->type) || pointer_type (value2->type))
1111     ax_simple (ax, aop_equal);
1112   else
1113     gen_binop (ax, value, value1, value2,
1114 	       aop_equal, aop_equal, 0, "equal");
1115   value->type = result_type;
1116   value->kind = axs_rvalue;
1117 }
1118 
1119 static void
1120 gen_less (struct agent_expr *ax, struct axs_value *value,
1121 	  struct axs_value *value1, struct axs_value *value2,
1122 	  struct type *result_type)
1123 {
1124   if (pointer_type (value1->type) || pointer_type (value2->type))
1125     ax_simple (ax, aop_less_unsigned);
1126   else
1127     gen_binop (ax, value, value1, value2,
1128 	       aop_less_signed, aop_less_unsigned, 0, "less than");
1129   value->type = result_type;
1130   value->kind = axs_rvalue;
1131 }
1132 
1133 /* Generate code for a binary operator that doesn't do pointer magic.
1134    We set VALUE to describe the result value; we assume VALUE1 and
1135    VALUE2 describe the two operands, and that they've undergone the
1136    usual binary conversions.  MAY_CARRY should be non-zero iff the
1137    result needs to be extended.  NAME is the English name of the
1138    operator, used in error messages */
1139 static void
1140 gen_binop (struct agent_expr *ax, struct axs_value *value,
1141 	   struct axs_value *value1, struct axs_value *value2, enum agent_op op,
1142 	   enum agent_op op_unsigned, int may_carry, char *name)
1143 {
1144   /* We only handle INT op INT.  */
1145   if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1146       || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1147     error (_("Invalid combination of types in %s."), name);
1148 
1149   ax_simple (ax,
1150 	     TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1151   if (may_carry)
1152     gen_extend (ax, value1->type);	/* catch overflow */
1153   value->type = value1->type;
1154   value->kind = axs_rvalue;
1155 }
1156 
1157 
1158 static void
1159 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1160 		 struct type *result_type)
1161 {
1162   if (TYPE_CODE (value->type) != TYPE_CODE_INT
1163       && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1164     error (_("Invalid type of operand to `!'."));
1165 
1166   ax_simple (ax, aop_log_not);
1167   value->type = result_type;
1168 }
1169 
1170 
1171 static void
1172 gen_complement (struct agent_expr *ax, struct axs_value *value)
1173 {
1174   if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1175     error (_("Invalid type of operand to `~'."));
1176 
1177   ax_simple (ax, aop_bit_not);
1178   gen_extend (ax, value->type);
1179 }
1180 
1181 
1182 
1183 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1184 
1185 /* Dereference the value on the top of the stack.  */
1186 static void
1187 gen_deref (struct agent_expr *ax, struct axs_value *value)
1188 {
1189   /* The caller should check the type, because several operators use
1190      this, and we don't know what error message to generate.  */
1191   if (!pointer_type (value->type))
1192     internal_error (__FILE__, __LINE__,
1193 		    _("gen_deref: expected a pointer"));
1194 
1195   /* We've got an rvalue now, which is a pointer.  We want to yield an
1196      lvalue, whose address is exactly that pointer.  So we don't
1197      actually emit any code; we just change the type from "Pointer to
1198      T" to "T", and mark the value as an lvalue in memory.  Leave it
1199      to the consumer to actually dereference it.  */
1200   value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1201   if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1202     error (_("Attempt to dereference a generic pointer."));
1203   value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1204 		 ? axs_rvalue : axs_lvalue_memory);
1205 }
1206 
1207 
1208 /* Produce the address of the lvalue on the top of the stack.  */
1209 static void
1210 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1211 {
1212   /* Special case for taking the address of a function.  The ANSI
1213      standard describes this as a special case, too, so this
1214      arrangement is not without motivation.  */
1215   if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1216     /* The value's already an rvalue on the stack, so we just need to
1217        change the type.  */
1218     value->type = lookup_pointer_type (value->type);
1219   else
1220     switch (value->kind)
1221       {
1222       case axs_rvalue:
1223 	error (_("Operand of `&' is an rvalue, which has no address."));
1224 
1225       case axs_lvalue_register:
1226 	error (_("Operand of `&' is in a register, and has no address."));
1227 
1228       case axs_lvalue_memory:
1229 	value->kind = axs_rvalue;
1230 	value->type = lookup_pointer_type (value->type);
1231 	break;
1232       }
1233 }
1234 
1235 /* Generate code to push the value of a bitfield of a structure whose
1236    address is on the top of the stack.  START and END give the
1237    starting and one-past-ending *bit* numbers of the field within the
1238    structure.  */
1239 static void
1240 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1241 		  struct axs_value *value, struct type *type,
1242 		  int start, int end)
1243 {
1244   /* Note that ops[i] fetches 8 << i bits.  */
1245   static enum agent_op ops[]
1246     = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1247   static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1248 
1249   /* We don't want to touch any byte that the bitfield doesn't
1250      actually occupy; we shouldn't make any accesses we're not
1251      explicitly permitted to.  We rely here on the fact that the
1252      bytecode `ref' operators work on unaligned addresses.
1253 
1254      It takes some fancy footwork to get the stack to work the way
1255      we'd like.  Say we're retrieving a bitfield that requires three
1256      fetches.  Initially, the stack just contains the address:
1257      addr
1258      For the first fetch, we duplicate the address
1259      addr addr
1260      then add the byte offset, do the fetch, and shift and mask as
1261      needed, yielding a fragment of the value, properly aligned for
1262      the final bitwise or:
1263      addr frag1
1264      then we swap, and repeat the process:
1265      frag1 addr                    --- address on top
1266      frag1 addr addr               --- duplicate it
1267      frag1 addr frag2              --- get second fragment
1268      frag1 frag2 addr              --- swap again
1269      frag1 frag2 frag3             --- get third fragment
1270      Notice that, since the third fragment is the last one, we don't
1271      bother duplicating the address this time.  Now we have all the
1272      fragments on the stack, and we can simply `or' them together,
1273      yielding the final value of the bitfield.  */
1274 
1275   /* The first and one-after-last bits in the field, but rounded down
1276      and up to byte boundaries.  */
1277   int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1278   int bound_end = (((end + TARGET_CHAR_BIT - 1)
1279 		    / TARGET_CHAR_BIT)
1280 		   * TARGET_CHAR_BIT);
1281 
1282   /* current bit offset within the structure */
1283   int offset;
1284 
1285   /* The index in ops of the opcode we're considering.  */
1286   int op;
1287 
1288   /* The number of fragments we generated in the process.  Probably
1289      equal to the number of `one' bits in bytesize, but who cares?  */
1290   int fragment_count;
1291 
1292   /* Dereference any typedefs. */
1293   type = check_typedef (type);
1294 
1295   /* Can we fetch the number of bits requested at all?  */
1296   if ((end - start) > ((1 << num_ops) * 8))
1297     internal_error (__FILE__, __LINE__,
1298 		    _("gen_bitfield_ref: bitfield too wide"));
1299 
1300   /* Note that we know here that we only need to try each opcode once.
1301      That may not be true on machines with weird byte sizes.  */
1302   offset = bound_start;
1303   fragment_count = 0;
1304   for (op = num_ops - 1; op >= 0; op--)
1305     {
1306       /* number of bits that ops[op] would fetch */
1307       int op_size = 8 << op;
1308 
1309       /* The stack at this point, from bottom to top, contains zero or
1310          more fragments, then the address.  */
1311 
1312       /* Does this fetch fit within the bitfield?  */
1313       if (offset + op_size <= bound_end)
1314 	{
1315 	  /* Is this the last fragment?  */
1316 	  int last_frag = (offset + op_size == bound_end);
1317 
1318 	  if (!last_frag)
1319 	    ax_simple (ax, aop_dup);	/* keep a copy of the address */
1320 
1321 	  /* Add the offset.  */
1322 	  gen_offset (ax, offset / TARGET_CHAR_BIT);
1323 
1324 	  if (trace_kludge)
1325 	    {
1326 	      /* Record the area of memory we're about to fetch.  */
1327 	      ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1328 	    }
1329 
1330 	  /* Perform the fetch.  */
1331 	  ax_simple (ax, ops[op]);
1332 
1333 	  /* Shift the bits we have to their proper position.
1334 	     gen_left_shift will generate right shifts when the operand
1335 	     is negative.
1336 
1337 	     A big-endian field diagram to ponder:
1338 	     byte 0  byte 1  byte 2  byte 3  byte 4  byte 5  byte 6  byte 7
1339 	     +------++------++------++------++------++------++------++------+
1340 	     xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1341 	     ^               ^               ^    ^
1342 	     bit number      16              32              48   53
1343 	     These are bit numbers as supplied by GDB.  Note that the
1344 	     bit numbers run from right to left once you've fetched the
1345 	     value!
1346 
1347 	     A little-endian field diagram to ponder:
1348 	     byte 7  byte 6  byte 5  byte 4  byte 3  byte 2  byte 1  byte 0
1349 	     +------++------++------++------++------++------++------++------+
1350 	     xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1351 	     ^               ^               ^           ^   ^
1352 	     bit number     48              32              16          4   0
1353 
1354 	     In both cases, the most significant end is on the left
1355 	     (i.e. normal numeric writing order), which means that you
1356 	     don't go crazy thinking about `left' and `right' shifts.
1357 
1358 	     We don't have to worry about masking yet:
1359 	     - If they contain garbage off the least significant end, then we
1360 	     must be looking at the low end of the field, and the right
1361 	     shift will wipe them out.
1362 	     - If they contain garbage off the most significant end, then we
1363 	     must be looking at the most significant end of the word, and
1364 	     the sign/zero extension will wipe them out.
1365 	     - If we're in the interior of the word, then there is no garbage
1366 	     on either end, because the ref operators zero-extend.  */
1367 	  if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1368 	    gen_left_shift (ax, end - (offset + op_size));
1369 	  else
1370 	    gen_left_shift (ax, offset - start);
1371 
1372 	  if (!last_frag)
1373 	    /* Bring the copy of the address up to the top.  */
1374 	    ax_simple (ax, aop_swap);
1375 
1376 	  offset += op_size;
1377 	  fragment_count++;
1378 	}
1379     }
1380 
1381   /* Generate enough bitwise `or' operations to combine all the
1382      fragments we left on the stack.  */
1383   while (fragment_count-- > 1)
1384     ax_simple (ax, aop_bit_or);
1385 
1386   /* Sign- or zero-extend the value as appropriate.  */
1387   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1388 
1389   /* This is *not* an lvalue.  Ugh.  */
1390   value->kind = axs_rvalue;
1391   value->type = type;
1392 }
1393 
1394 /* Generate bytecodes for field number FIELDNO of type TYPE.  OFFSET
1395    is an accumulated offset (in bytes), will be nonzero for objects
1396    embedded in other objects, like C++ base classes.  Behavior should
1397    generally follow value_primitive_field.  */
1398 
1399 static void
1400 gen_primitive_field (struct expression *exp,
1401 		     struct agent_expr *ax, struct axs_value *value,
1402 		     int offset, int fieldno, struct type *type)
1403 {
1404   /* Is this a bitfield?  */
1405   if (TYPE_FIELD_PACKED (type, fieldno))
1406     gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1407 		      (offset * TARGET_CHAR_BIT
1408 		       + TYPE_FIELD_BITPOS (type, fieldno)),
1409 		      (offset * TARGET_CHAR_BIT
1410 		       + TYPE_FIELD_BITPOS (type, fieldno)
1411 		       + TYPE_FIELD_BITSIZE (type, fieldno)));
1412   else
1413     {
1414       gen_offset (ax, offset
1415 		  + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1416       value->kind = axs_lvalue_memory;
1417       value->type = TYPE_FIELD_TYPE (type, fieldno);
1418     }
1419 }
1420 
1421 /* Search for the given field in either the given type or one of its
1422    base classes.  Return 1 if found, 0 if not.  */
1423 
1424 static int
1425 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1426 			  struct axs_value *value,
1427 			  char *field, int offset, struct type *type)
1428 {
1429   int i, rslt;
1430   int nbases = TYPE_N_BASECLASSES (type);
1431 
1432   CHECK_TYPEDEF (type);
1433 
1434   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1435     {
1436       char *this_name = TYPE_FIELD_NAME (type, i);
1437 
1438       if (this_name)
1439 	{
1440 	  if (strcmp (field, this_name) == 0)
1441 	    {
1442 	      /* Note that bytecodes for the struct's base (aka
1443 		 "this") will have been generated already, which will
1444 		 be unnecessary but not harmful if the static field is
1445 		 being handled as a global.  */
1446 	      if (field_is_static (&TYPE_FIELD (type, i)))
1447 		{
1448 		  gen_static_field (exp->gdbarch, ax, value, type, i);
1449 		  if (value->optimized_out)
1450 		    error (_("static field `%s' has been optimized out, cannot use"),
1451 			   field);
1452 		  return 1;
1453 		}
1454 
1455 	      gen_primitive_field (exp, ax, value, offset, i, type);
1456 	      return 1;
1457 	    }
1458 #if 0 /* is this right? */
1459 	  if (this_name[0] == '\0')
1460 	    internal_error (__FILE__, __LINE__,
1461 			    _("find_field: anonymous unions not supported"));
1462 #endif
1463 	}
1464     }
1465 
1466   /* Now scan through base classes recursively.  */
1467   for (i = 0; i < nbases; i++)
1468     {
1469       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1470 
1471       rslt = gen_struct_ref_recursive (exp, ax, value, field,
1472 				       offset + TYPE_BASECLASS_BITPOS (type, i) / TARGET_CHAR_BIT,
1473 				       basetype);
1474       if (rslt)
1475 	return 1;
1476     }
1477 
1478   /* Not found anywhere, flag so caller can complain.  */
1479   return 0;
1480 }
1481 
1482 /* Generate code to reference the member named FIELD of a structure or
1483    union.  The top of the stack, as described by VALUE, should have
1484    type (pointer to a)* struct/union.  OPERATOR_NAME is the name of
1485    the operator being compiled, and OPERAND_NAME is the kind of thing
1486    it operates on; we use them in error messages.  */
1487 static void
1488 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1489 		struct axs_value *value, char *field,
1490 		char *operator_name, char *operand_name)
1491 {
1492   struct type *type;
1493   int found;
1494 
1495   /* Follow pointers until we reach a non-pointer.  These aren't the C
1496      semantics, but they're what the normal GDB evaluator does, so we
1497      should at least be consistent.  */
1498   while (pointer_type (value->type))
1499     {
1500       require_rvalue (ax, value);
1501       gen_deref (ax, value);
1502     }
1503   type = check_typedef (value->type);
1504 
1505   /* This must yield a structure or a union.  */
1506   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1507       && TYPE_CODE (type) != TYPE_CODE_UNION)
1508     error (_("The left operand of `%s' is not a %s."),
1509 	   operator_name, operand_name);
1510 
1511   /* And it must be in memory; we don't deal with structure rvalues,
1512      or structures living in registers.  */
1513   if (value->kind != axs_lvalue_memory)
1514     error (_("Structure does not live in memory."));
1515 
1516   /* Search through fields and base classes recursively.  */
1517   found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1518 
1519   if (!found)
1520     error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1521 	   field, TYPE_TAG_NAME (type));
1522 }
1523 
1524 static int
1525 gen_namespace_elt (struct expression *exp,
1526 		   struct agent_expr *ax, struct axs_value *value,
1527 		   const struct type *curtype, char *name);
1528 static int
1529 gen_maybe_namespace_elt (struct expression *exp,
1530 			 struct agent_expr *ax, struct axs_value *value,
1531 			 const struct type *curtype, char *name);
1532 
1533 static void
1534 gen_static_field (struct gdbarch *gdbarch,
1535 		  struct agent_expr *ax, struct axs_value *value,
1536 		  struct type *type, int fieldno)
1537 {
1538   if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1539     {
1540       ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1541       value->kind = axs_lvalue_memory;
1542       value->type = TYPE_FIELD_TYPE (type, fieldno);
1543       value->optimized_out = 0;
1544     }
1545   else
1546     {
1547       char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1548       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
1549 
1550       if (sym)
1551 	{
1552 	  gen_var_ref (gdbarch, ax, value, sym);
1553 
1554 	  /* Don't error if the value was optimized out, we may be
1555 	     scanning all static fields and just want to pass over this
1556 	     and continue with the rest.  */
1557 	}
1558       else
1559 	{
1560 	  /* Silently assume this was optimized out; class printing
1561 	     will let the user know why the data is missing.  */
1562 	  value->optimized_out = 1;
1563 	}
1564     }
1565 }
1566 
1567 static int
1568 gen_struct_elt_for_reference (struct expression *exp,
1569 			      struct agent_expr *ax, struct axs_value *value,
1570 			      struct type *type, char *fieldname)
1571 {
1572   struct type *t = type;
1573   int i;
1574 
1575   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1576       && TYPE_CODE (t) != TYPE_CODE_UNION)
1577     internal_error (__FILE__, __LINE__,
1578 		    _("non-aggregate type to gen_struct_elt_for_reference"));
1579 
1580   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1581     {
1582       char *t_field_name = TYPE_FIELD_NAME (t, i);
1583 
1584       if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1585 	{
1586 	  if (field_is_static (&TYPE_FIELD (t, i)))
1587 	    {
1588 	      gen_static_field (exp->gdbarch, ax, value, t, i);
1589 	      if (value->optimized_out)
1590 		error (_("static field `%s' has been optimized out, cannot use"),
1591 		       fieldname);
1592 	      return 1;
1593 	    }
1594 	  if (TYPE_FIELD_PACKED (t, i))
1595 	    error (_("pointers to bitfield members not allowed"));
1596 
1597 	  /* FIXME we need a way to do "want_address" equivalent */
1598 
1599 	  error (_("Cannot reference non-static field \"%s\""), fieldname);
1600 	}
1601     }
1602 
1603   /* FIXME add other scoped-reference cases here */
1604 
1605   /* Do a last-ditch lookup.  */
1606   return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1607 }
1608 
1609 /* C++: Return the member NAME of the namespace given by the type
1610    CURTYPE.  */
1611 
1612 static int
1613 gen_namespace_elt (struct expression *exp,
1614 		   struct agent_expr *ax, struct axs_value *value,
1615 		   const struct type *curtype, char *name)
1616 {
1617   int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1618 
1619   if (!found)
1620     error (_("No symbol \"%s\" in namespace \"%s\"."),
1621 	   name, TYPE_TAG_NAME (curtype));
1622 
1623   return found;
1624 }
1625 
1626 /* A helper function used by value_namespace_elt and
1627    value_struct_elt_for_reference.  It looks up NAME inside the
1628    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1629    is a class and NAME refers to a type in CURTYPE itself (as opposed
1630    to, say, some base class of CURTYPE).  */
1631 
1632 static int
1633 gen_maybe_namespace_elt (struct expression *exp,
1634 			 struct agent_expr *ax, struct axs_value *value,
1635 			 const struct type *curtype, char *name)
1636 {
1637   const char *namespace_name = TYPE_TAG_NAME (curtype);
1638   struct symbol *sym;
1639 
1640   sym = cp_lookup_symbol_namespace (namespace_name, name,
1641 				    block_for_pc (ax->scope),
1642 				    VAR_DOMAIN);
1643 
1644   if (sym == NULL)
1645     return 0;
1646 
1647   gen_var_ref (exp->gdbarch, ax, value, sym);
1648 
1649   if (value->optimized_out)
1650     error (_("`%s' has been optimized out, cannot use"),
1651 	   SYMBOL_PRINT_NAME (sym));
1652 
1653   return 1;
1654 }
1655 
1656 
1657 static int
1658 gen_aggregate_elt_ref (struct expression *exp,
1659 		       struct agent_expr *ax, struct axs_value *value,
1660 		       struct type *type, char *field,
1661 		       char *operator_name, char *operand_name)
1662 {
1663   switch (TYPE_CODE (type))
1664     {
1665     case TYPE_CODE_STRUCT:
1666     case TYPE_CODE_UNION:
1667       return gen_struct_elt_for_reference (exp, ax, value, type, field);
1668       break;
1669     case TYPE_CODE_NAMESPACE:
1670       return gen_namespace_elt (exp, ax, value, type, field);
1671       break;
1672     default:
1673       internal_error (__FILE__, __LINE__,
1674 		      _("non-aggregate type in gen_aggregate_elt_ref"));
1675     }
1676 
1677   return 0;
1678 }
1679 
1680 /* Generate code for GDB's magical `repeat' operator.
1681    LVALUE @ INT creates an array INT elements long, and whose elements
1682    have the same type as LVALUE, located in memory so that LVALUE is
1683    its first element.  For example, argv[0]@argc gives you the array
1684    of command-line arguments.
1685 
1686    Unfortunately, because we have to know the types before we actually
1687    have a value for the expression, we can't implement this perfectly
1688    without changing the type system, having values that occupy two
1689    stack slots, doing weird things with sizeof, etc.  So we require
1690    the right operand to be a constant expression.  */
1691 static void
1692 gen_repeat (struct expression *exp, union exp_element **pc,
1693 	    struct agent_expr *ax, struct axs_value *value)
1694 {
1695   struct axs_value value1;
1696 
1697   /* We don't want to turn this into an rvalue, so no conversions
1698      here.  */
1699   gen_expr (exp, pc, ax, &value1);
1700   if (value1.kind != axs_lvalue_memory)
1701     error (_("Left operand of `@' must be an object in memory."));
1702 
1703   /* Evaluate the length; it had better be a constant.  */
1704   {
1705     struct value *v = const_expr (pc);
1706     int length;
1707 
1708     if (!v)
1709       error (_("Right operand of `@' must be a constant, in agent expressions."));
1710     if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1711       error (_("Right operand of `@' must be an integer."));
1712     length = value_as_long (v);
1713     if (length <= 0)
1714       error (_("Right operand of `@' must be positive."));
1715 
1716     /* The top of the stack is already the address of the object, so
1717        all we need to do is frob the type of the lvalue.  */
1718     {
1719       /* FIXME-type-allocation: need a way to free this type when we are
1720          done with it.  */
1721       struct type *array
1722 	= lookup_array_range_type (value1.type, 0, length - 1);
1723 
1724       value->kind = axs_lvalue_memory;
1725       value->type = array;
1726     }
1727   }
1728 }
1729 
1730 
1731 /* Emit code for the `sizeof' operator.
1732    *PC should point at the start of the operand expression; we advance it
1733    to the first instruction after the operand.  */
1734 static void
1735 gen_sizeof (struct expression *exp, union exp_element **pc,
1736 	    struct agent_expr *ax, struct axs_value *value,
1737 	    struct type *size_type)
1738 {
1739   /* We don't care about the value of the operand expression; we only
1740      care about its type.  However, in the current arrangement, the
1741      only way to find an expression's type is to generate code for it.
1742      So we generate code for the operand, and then throw it away,
1743      replacing it with code that simply pushes its size.  */
1744   int start = ax->len;
1745 
1746   gen_expr (exp, pc, ax, value);
1747 
1748   /* Throw away the code we just generated.  */
1749   ax->len = start;
1750 
1751   ax_const_l (ax, TYPE_LENGTH (value->type));
1752   value->kind = axs_rvalue;
1753   value->type = size_type;
1754 }
1755 
1756 
1757 /* Generating bytecode from GDB expressions: general recursive thingy  */
1758 
1759 /* XXX: i18n */
1760 /* A gen_expr function written by a Gen-X'er guy.
1761    Append code for the subexpression of EXPR starting at *POS_P to AX.  */
1762 static void
1763 gen_expr (struct expression *exp, union exp_element **pc,
1764 	  struct agent_expr *ax, struct axs_value *value)
1765 {
1766   /* Used to hold the descriptions of operand expressions.  */
1767   struct axs_value value1, value2, value3;
1768   enum exp_opcode op = (*pc)[0].opcode, op2;
1769   int if1, go1, if2, go2, end;
1770   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
1771 
1772   /* If we're looking at a constant expression, just push its value.  */
1773   {
1774     struct value *v = maybe_const_expr (pc);
1775 
1776     if (v)
1777       {
1778 	ax_const_l (ax, value_as_long (v));
1779 	value->kind = axs_rvalue;
1780 	value->type = check_typedef (value_type (v));
1781 	return;
1782       }
1783   }
1784 
1785   /* Otherwise, go ahead and generate code for it.  */
1786   switch (op)
1787     {
1788       /* Binary arithmetic operators.  */
1789     case BINOP_ADD:
1790     case BINOP_SUB:
1791     case BINOP_MUL:
1792     case BINOP_DIV:
1793     case BINOP_REM:
1794     case BINOP_LSH:
1795     case BINOP_RSH:
1796     case BINOP_SUBSCRIPT:
1797     case BINOP_BITWISE_AND:
1798     case BINOP_BITWISE_IOR:
1799     case BINOP_BITWISE_XOR:
1800     case BINOP_EQUAL:
1801     case BINOP_NOTEQUAL:
1802     case BINOP_LESS:
1803     case BINOP_GTR:
1804     case BINOP_LEQ:
1805     case BINOP_GEQ:
1806       (*pc)++;
1807       gen_expr (exp, pc, ax, &value1);
1808       gen_usual_unary (exp, ax, &value1);
1809       gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1810       break;
1811 
1812     case BINOP_LOGICAL_AND:
1813       (*pc)++;
1814       /* Generate the obvious sequence of tests and jumps.  */
1815       gen_expr (exp, pc, ax, &value1);
1816       gen_usual_unary (exp, ax, &value1);
1817       if1 = ax_goto (ax, aop_if_goto);
1818       go1 = ax_goto (ax, aop_goto);
1819       ax_label (ax, if1, ax->len);
1820       gen_expr (exp, pc, ax, &value2);
1821       gen_usual_unary (exp, ax, &value2);
1822       if2 = ax_goto (ax, aop_if_goto);
1823       go2 = ax_goto (ax, aop_goto);
1824       ax_label (ax, if2, ax->len);
1825       ax_const_l (ax, 1);
1826       end = ax_goto (ax, aop_goto);
1827       ax_label (ax, go1, ax->len);
1828       ax_label (ax, go2, ax->len);
1829       ax_const_l (ax, 0);
1830       ax_label (ax, end, ax->len);
1831       value->kind = axs_rvalue;
1832       value->type = int_type;
1833       break;
1834 
1835     case BINOP_LOGICAL_OR:
1836       (*pc)++;
1837       /* Generate the obvious sequence of tests and jumps.  */
1838       gen_expr (exp, pc, ax, &value1);
1839       gen_usual_unary (exp, ax, &value1);
1840       if1 = ax_goto (ax, aop_if_goto);
1841       gen_expr (exp, pc, ax, &value2);
1842       gen_usual_unary (exp, ax, &value2);
1843       if2 = ax_goto (ax, aop_if_goto);
1844       ax_const_l (ax, 0);
1845       end = ax_goto (ax, aop_goto);
1846       ax_label (ax, if1, ax->len);
1847       ax_label (ax, if2, ax->len);
1848       ax_const_l (ax, 1);
1849       ax_label (ax, end, ax->len);
1850       value->kind = axs_rvalue;
1851       value->type = int_type;
1852       break;
1853 
1854     case TERNOP_COND:
1855       (*pc)++;
1856       gen_expr (exp, pc, ax, &value1);
1857       gen_usual_unary (exp, ax, &value1);
1858       /* For (A ? B : C), it's easiest to generate subexpression
1859 	 bytecodes in order, but if_goto jumps on true, so we invert
1860 	 the sense of A.  Then we can do B by dropping through, and
1861 	 jump to do C.  */
1862       gen_logical_not (ax, &value1, int_type);
1863       if1 = ax_goto (ax, aop_if_goto);
1864       gen_expr (exp, pc, ax, &value2);
1865       gen_usual_unary (exp, ax, &value2);
1866       end = ax_goto (ax, aop_goto);
1867       ax_label (ax, if1, ax->len);
1868       gen_expr (exp, pc, ax, &value3);
1869       gen_usual_unary (exp, ax, &value3);
1870       ax_label (ax, end, ax->len);
1871       /* This is arbitary - what if B and C are incompatible types? */
1872       value->type = value2.type;
1873       value->kind = value2.kind;
1874       break;
1875 
1876     case BINOP_ASSIGN:
1877       (*pc)++;
1878       if ((*pc)[0].opcode == OP_INTERNALVAR)
1879 	{
1880 	  char *name = internalvar_name ((*pc)[1].internalvar);
1881 	  struct trace_state_variable *tsv;
1882 
1883 	  (*pc) += 3;
1884 	  gen_expr (exp, pc, ax, value);
1885 	  tsv = find_trace_state_variable (name);
1886 	  if (tsv)
1887 	    {
1888 	      ax_tsv (ax, aop_setv, tsv->number);
1889 	      if (trace_kludge)
1890 		ax_tsv (ax, aop_tracev, tsv->number);
1891 	    }
1892 	  else
1893 	    error (_("$%s is not a trace state variable, may not assign to it"), name);
1894 	}
1895       else
1896 	error (_("May only assign to trace state variables"));
1897       break;
1898 
1899     case BINOP_ASSIGN_MODIFY:
1900       (*pc)++;
1901       op2 = (*pc)[0].opcode;
1902       (*pc)++;
1903       (*pc)++;
1904       if ((*pc)[0].opcode == OP_INTERNALVAR)
1905 	{
1906 	  char *name = internalvar_name ((*pc)[1].internalvar);
1907 	  struct trace_state_variable *tsv;
1908 
1909 	  (*pc) += 3;
1910 	  tsv = find_trace_state_variable (name);
1911 	  if (tsv)
1912 	    {
1913 	      /* The tsv will be the left half of the binary operation.  */
1914 	      ax_tsv (ax, aop_getv, tsv->number);
1915 	      if (trace_kludge)
1916 		ax_tsv (ax, aop_tracev, tsv->number);
1917 	      /* Trace state variables are always 64-bit integers.  */
1918 	      value1.kind = axs_rvalue;
1919 	      value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1920 	      /* Now do right half of expression.  */
1921 	      gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1922 	      /* We have a result of the binary op, set the tsv.  */
1923 	      ax_tsv (ax, aop_setv, tsv->number);
1924 	      if (trace_kludge)
1925 		ax_tsv (ax, aop_tracev, tsv->number);
1926 	    }
1927 	  else
1928 	    error (_("$%s is not a trace state variable, may not assign to it"), name);
1929 	}
1930       else
1931 	error (_("May only assign to trace state variables"));
1932       break;
1933 
1934       /* Note that we need to be a little subtle about generating code
1935          for comma.  In C, we can do some optimizations here because
1936          we know the left operand is only being evaluated for effect.
1937          However, if the tracing kludge is in effect, then we always
1938          need to evaluate the left hand side fully, so that all the
1939          variables it mentions get traced.  */
1940     case BINOP_COMMA:
1941       (*pc)++;
1942       gen_expr (exp, pc, ax, &value1);
1943       /* Don't just dispose of the left operand.  We might be tracing,
1944          in which case we want to emit code to trace it if it's an
1945          lvalue.  */
1946       gen_traced_pop (exp->gdbarch, ax, &value1);
1947       gen_expr (exp, pc, ax, value);
1948       /* It's the consumer's responsibility to trace the right operand.  */
1949       break;
1950 
1951     case OP_LONG:		/* some integer constant */
1952       {
1953 	struct type *type = (*pc)[1].type;
1954 	LONGEST k = (*pc)[2].longconst;
1955 
1956 	(*pc) += 4;
1957 	gen_int_literal (ax, value, k, type);
1958       }
1959       break;
1960 
1961     case OP_VAR_VALUE:
1962       gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
1963 
1964       if (value->optimized_out)
1965 	error (_("`%s' has been optimized out, cannot use"),
1966 	       SYMBOL_PRINT_NAME ((*pc)[2].symbol));
1967 
1968       (*pc) += 4;
1969       break;
1970 
1971     case OP_REGISTER:
1972       {
1973 	const char *name = &(*pc)[2].string;
1974 	int reg;
1975 
1976 	(*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1977 	reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
1978 	if (reg == -1)
1979 	  internal_error (__FILE__, __LINE__,
1980 			  _("Register $%s not available"), name);
1981 	if (reg >= gdbarch_num_regs (exp->gdbarch))
1982 	  error (_("'%s' is a pseudo-register; "
1983 		   "GDB cannot yet trace pseudoregister contents."),
1984 		 name);
1985 	value->kind = axs_lvalue_register;
1986 	value->u.reg = reg;
1987 	value->type = register_type (exp->gdbarch, reg);
1988       }
1989       break;
1990 
1991     case OP_INTERNALVAR:
1992       {
1993 	const char *name = internalvar_name ((*pc)[1].internalvar);
1994 	struct trace_state_variable *tsv;
1995 
1996 	(*pc) += 3;
1997 	tsv = find_trace_state_variable (name);
1998 	if (tsv)
1999 	  {
2000 	    ax_tsv (ax, aop_getv, tsv->number);
2001 	    if (trace_kludge)
2002 	      ax_tsv (ax, aop_tracev, tsv->number);
2003 	    /* Trace state variables are always 64-bit integers.  */
2004 	    value->kind = axs_rvalue;
2005 	    value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2006 	  }
2007 	else
2008 	  error (_("$%s is not a trace state variable; GDB agent expressions cannot use convenience variables."), name);
2009       }
2010       break;
2011 
2012       /* Weirdo operator: see comments for gen_repeat for details.  */
2013     case BINOP_REPEAT:
2014       /* Note that gen_repeat handles its own argument evaluation.  */
2015       (*pc)++;
2016       gen_repeat (exp, pc, ax, value);
2017       break;
2018 
2019     case UNOP_CAST:
2020       {
2021 	struct type *type = (*pc)[1].type;
2022 
2023 	(*pc) += 3;
2024 	gen_expr (exp, pc, ax, value);
2025 	gen_cast (ax, value, type);
2026       }
2027       break;
2028 
2029     case UNOP_MEMVAL:
2030       {
2031 	struct type *type = check_typedef ((*pc)[1].type);
2032 
2033 	(*pc) += 3;
2034 	gen_expr (exp, pc, ax, value);
2035 	/* I'm not sure I understand UNOP_MEMVAL entirely.  I think
2036 	   it's just a hack for dealing with minsyms; you take some
2037 	   integer constant, pretend it's the address of an lvalue of
2038 	   the given type, and dereference it.  */
2039 	if (value->kind != axs_rvalue)
2040 	  /* This would be weird.  */
2041 	  internal_error (__FILE__, __LINE__,
2042 			  _("gen_expr: OP_MEMVAL operand isn't an rvalue???"));
2043 	value->type = type;
2044 	value->kind = axs_lvalue_memory;
2045       }
2046       break;
2047 
2048     case UNOP_PLUS:
2049       (*pc)++;
2050       /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2051       gen_expr (exp, pc, ax, value);
2052       gen_usual_unary (exp, ax, value);
2053       break;
2054 
2055     case UNOP_NEG:
2056       (*pc)++;
2057       /* -FOO is equivalent to 0 - FOO.  */
2058       gen_int_literal (ax, &value1, 0,
2059 		       builtin_type (exp->gdbarch)->builtin_int);
2060       gen_usual_unary (exp, ax, &value1);	/* shouldn't do much */
2061       gen_expr (exp, pc, ax, &value2);
2062       gen_usual_unary (exp, ax, &value2);
2063       gen_usual_arithmetic (exp, ax, &value1, &value2);
2064       gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2065       break;
2066 
2067     case UNOP_LOGICAL_NOT:
2068       (*pc)++;
2069       gen_expr (exp, pc, ax, value);
2070       gen_usual_unary (exp, ax, value);
2071       gen_logical_not (ax, value, int_type);
2072       break;
2073 
2074     case UNOP_COMPLEMENT:
2075       (*pc)++;
2076       gen_expr (exp, pc, ax, value);
2077       gen_usual_unary (exp, ax, value);
2078       gen_integral_promotions (exp, ax, value);
2079       gen_complement (ax, value);
2080       break;
2081 
2082     case UNOP_IND:
2083       (*pc)++;
2084       gen_expr (exp, pc, ax, value);
2085       gen_usual_unary (exp, ax, value);
2086       if (!pointer_type (value->type))
2087 	error (_("Argument of unary `*' is not a pointer."));
2088       gen_deref (ax, value);
2089       break;
2090 
2091     case UNOP_ADDR:
2092       (*pc)++;
2093       gen_expr (exp, pc, ax, value);
2094       gen_address_of (ax, value);
2095       break;
2096 
2097     case UNOP_SIZEOF:
2098       (*pc)++;
2099       /* Notice that gen_sizeof handles its own operand, unlike most
2100          of the other unary operator functions.  This is because we
2101          have to throw away the code we generate.  */
2102       gen_sizeof (exp, pc, ax, value,
2103 		  builtin_type (exp->gdbarch)->builtin_int);
2104       break;
2105 
2106     case STRUCTOP_STRUCT:
2107     case STRUCTOP_PTR:
2108       {
2109 	int length = (*pc)[1].longconst;
2110 	char *name = &(*pc)[2].string;
2111 
2112 	(*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2113 	gen_expr (exp, pc, ax, value);
2114 	if (op == STRUCTOP_STRUCT)
2115 	  gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2116 	else if (op == STRUCTOP_PTR)
2117 	  gen_struct_ref (exp, ax, value, name, "->",
2118 			  "pointer to a structure or union");
2119 	else
2120 	  /* If this `if' chain doesn't handle it, then the case list
2121 	     shouldn't mention it, and we shouldn't be here.  */
2122 	  internal_error (__FILE__, __LINE__,
2123 			  _("gen_expr: unhandled struct case"));
2124       }
2125       break;
2126 
2127     case OP_THIS:
2128       {
2129 	char *this_name;
2130 	struct symbol *func, *sym;
2131 	struct block *b;
2132 
2133 	func = block_linkage_function (block_for_pc (ax->scope));
2134 	this_name = language_def (SYMBOL_LANGUAGE (func))->la_name_of_this;
2135 	b = SYMBOL_BLOCK_VALUE (func);
2136 
2137 	/* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2138 	   symbol instead of the LOC_ARG one (if both exist).  */
2139 	sym = lookup_block_symbol (b, this_name, VAR_DOMAIN);
2140 	if (!sym)
2141 	  error (_("no `%s' found"), this_name);
2142 
2143 	gen_var_ref (exp->gdbarch, ax, value, sym);
2144 
2145 	if (value->optimized_out)
2146 	  error (_("`%s' has been optimized out, cannot use"),
2147 		 SYMBOL_PRINT_NAME (sym));
2148 
2149 	(*pc) += 2;
2150       }
2151       break;
2152 
2153     case OP_SCOPE:
2154       {
2155 	struct type *type = (*pc)[1].type;
2156 	int length = longest_to_int ((*pc)[2].longconst);
2157 	char *name = &(*pc)[3].string;
2158 	int found;
2159 
2160 	found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2161 				       "?", "??");
2162 	if (!found)
2163 	  error (_("There is no field named %s"), name);
2164 	(*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2165       }
2166       break;
2167 
2168     case OP_TYPE:
2169       error (_("Attempt to use a type name as an expression."));
2170 
2171     default:
2172       error (_("Unsupported operator %s (%d) in expression."),
2173 	     op_string (op), op);
2174     }
2175 }
2176 
2177 /* This handles the middle-to-right-side of code generation for binary
2178    expressions, which is shared between regular binary operations and
2179    assign-modify (+= and friends) expressions.  */
2180 
2181 static void
2182 gen_expr_binop_rest (struct expression *exp,
2183 		     enum exp_opcode op, union exp_element **pc,
2184 		     struct agent_expr *ax, struct axs_value *value,
2185 		     struct axs_value *value1, struct axs_value *value2)
2186 {
2187   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2188 
2189   gen_expr (exp, pc, ax, value2);
2190   gen_usual_unary (exp, ax, value2);
2191   gen_usual_arithmetic (exp, ax, value1, value2);
2192   switch (op)
2193     {
2194     case BINOP_ADD:
2195       if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2196 	  && pointer_type (value2->type))
2197 	{
2198 	  /* Swap the values and proceed normally.  */
2199 	  ax_simple (ax, aop_swap);
2200 	  gen_ptradd (ax, value, value2, value1);
2201 	}
2202       else if (pointer_type (value1->type)
2203 	       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2204 	gen_ptradd (ax, value, value1, value2);
2205       else
2206 	gen_binop (ax, value, value1, value2,
2207 		   aop_add, aop_add, 1, "addition");
2208       break;
2209     case BINOP_SUB:
2210       if (pointer_type (value1->type)
2211 	  && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2212 	gen_ptrsub (ax,value, value1, value2);
2213       else if (pointer_type (value1->type)
2214 	       && pointer_type (value2->type))
2215 	/* FIXME --- result type should be ptrdiff_t */
2216 	gen_ptrdiff (ax, value, value1, value2,
2217 		     builtin_type (exp->gdbarch)->builtin_long);
2218       else
2219 	gen_binop (ax, value, value1, value2,
2220 		   aop_sub, aop_sub, 1, "subtraction");
2221       break;
2222     case BINOP_MUL:
2223       gen_binop (ax, value, value1, value2,
2224 		 aop_mul, aop_mul, 1, "multiplication");
2225       break;
2226     case BINOP_DIV:
2227       gen_binop (ax, value, value1, value2,
2228 		 aop_div_signed, aop_div_unsigned, 1, "division");
2229       break;
2230     case BINOP_REM:
2231       gen_binop (ax, value, value1, value2,
2232 		 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2233       break;
2234     case BINOP_LSH:
2235       gen_binop (ax, value, value1, value2,
2236 		 aop_lsh, aop_lsh, 1, "left shift");
2237       break;
2238     case BINOP_RSH:
2239       gen_binop (ax, value, value1, value2,
2240 		 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2241       break;
2242     case BINOP_SUBSCRIPT:
2243       {
2244 	struct type *type;
2245 
2246 	if (binop_types_user_defined_p (op, value1->type, value2->type))
2247 	  {
2248 	    error (_("\
2249 cannot subscript requested type: cannot call user defined functions"));
2250 	  }
2251 	else
2252 	  {
2253 	    /* If the user attempts to subscript something that is not
2254 	       an array or pointer type (like a plain int variable for
2255 	       example), then report this as an error.  */
2256 	    type = check_typedef (value1->type);
2257 	    if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2258 		&& TYPE_CODE (type) != TYPE_CODE_PTR)
2259 	      {
2260 		if (TYPE_NAME (type))
2261 		  error (_("cannot subscript something of type `%s'"),
2262 			 TYPE_NAME (type));
2263 		else
2264 		  error (_("cannot subscript requested type"));
2265 	      }
2266 	  }
2267 
2268 	if (!is_integral_type (value2->type))
2269 	  error (_("Argument to arithmetic operation not a number or boolean."));
2270 
2271 	gen_ptradd (ax, value, value1, value2);
2272 	gen_deref (ax, value);
2273 	break;
2274       }
2275     case BINOP_BITWISE_AND:
2276       gen_binop (ax, value, value1, value2,
2277 		 aop_bit_and, aop_bit_and, 0, "bitwise and");
2278       break;
2279 
2280     case BINOP_BITWISE_IOR:
2281       gen_binop (ax, value, value1, value2,
2282 		 aop_bit_or, aop_bit_or, 0, "bitwise or");
2283       break;
2284 
2285     case BINOP_BITWISE_XOR:
2286       gen_binop (ax, value, value1, value2,
2287 		 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2288       break;
2289 
2290     case BINOP_EQUAL:
2291       gen_equal (ax, value, value1, value2, int_type);
2292       break;
2293 
2294     case BINOP_NOTEQUAL:
2295       gen_equal (ax, value, value1, value2, int_type);
2296       gen_logical_not (ax, value, int_type);
2297       break;
2298 
2299     case BINOP_LESS:
2300       gen_less (ax, value, value1, value2, int_type);
2301       break;
2302 
2303     case BINOP_GTR:
2304       ax_simple (ax, aop_swap);
2305       gen_less (ax, value, value1, value2, int_type);
2306       break;
2307 
2308     case BINOP_LEQ:
2309       ax_simple (ax, aop_swap);
2310       gen_less (ax, value, value1, value2, int_type);
2311       gen_logical_not (ax, value, int_type);
2312       break;
2313 
2314     case BINOP_GEQ:
2315       gen_less (ax, value, value1, value2, int_type);
2316       gen_logical_not (ax, value, int_type);
2317       break;
2318 
2319     default:
2320       /* We should only list operators in the outer case statement
2321 	 that we actually handle in the inner case statement.  */
2322       internal_error (__FILE__, __LINE__,
2323 		      _("gen_expr: op case sets don't match"));
2324     }
2325 }
2326 
2327 
2328 /* Given a single variable and a scope, generate bytecodes to trace
2329    its value.  This is for use in situations where we have only a
2330    variable's name, and no parsed expression; for instance, when the
2331    name comes from a list of local variables of a function.  */
2332 
2333 struct agent_expr *
2334 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2335 		   struct symbol *var)
2336 {
2337   struct cleanup *old_chain = 0;
2338   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2339   struct axs_value value;
2340 
2341   old_chain = make_cleanup_free_agent_expr (ax);
2342 
2343   trace_kludge = 1;
2344   gen_var_ref (gdbarch, ax, &value, var);
2345 
2346   /* If there is no actual variable to trace, flag it by returning
2347      an empty agent expression.  */
2348   if (value.optimized_out)
2349     {
2350       do_cleanups (old_chain);
2351       return NULL;
2352     }
2353 
2354   /* Make sure we record the final object, and get rid of it.  */
2355   gen_traced_pop (gdbarch, ax, &value);
2356 
2357   /* Oh, and terminate.  */
2358   ax_simple (ax, aop_end);
2359 
2360   /* We have successfully built the agent expr, so cancel the cleanup
2361      request.  If we add more cleanups that we always want done, this
2362      will have to get more complicated.  */
2363   discard_cleanups (old_chain);
2364   return ax;
2365 }
2366 
2367 /* Generating bytecode from GDB expressions: driver */
2368 
2369 /* Given a GDB expression EXPR, return bytecode to trace its value.
2370    The result will use the `trace' and `trace_quick' bytecodes to
2371    record the value of all memory touched by the expression.  The
2372    caller can then use the ax_reqs function to discover which
2373    registers it relies upon.  */
2374 struct agent_expr *
2375 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
2376 {
2377   struct cleanup *old_chain = 0;
2378   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2379   union exp_element *pc;
2380   struct axs_value value;
2381 
2382   old_chain = make_cleanup_free_agent_expr (ax);
2383 
2384   pc = expr->elts;
2385   trace_kludge = 1;
2386   value.optimized_out = 0;
2387   gen_expr (expr, &pc, ax, &value);
2388 
2389   /* Make sure we record the final object, and get rid of it.  */
2390   gen_traced_pop (expr->gdbarch, ax, &value);
2391 
2392   /* Oh, and terminate.  */
2393   ax_simple (ax, aop_end);
2394 
2395   /* We have successfully built the agent expr, so cancel the cleanup
2396      request.  If we add more cleanups that we always want done, this
2397      will have to get more complicated.  */
2398   discard_cleanups (old_chain);
2399   return ax;
2400 }
2401 
2402 /* Given a GDB expression EXPR, return a bytecode sequence that will
2403    evaluate and return a result.  The bytecodes will do a direct
2404    evaluation, using the current data on the target, rather than
2405    recording blocks of memory and registers for later use, as
2406    gen_trace_for_expr does.  The generated bytecode sequence leaves
2407    the result of expression evaluation on the top of the stack.  */
2408 
2409 struct agent_expr *
2410 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2411 {
2412   struct cleanup *old_chain = 0;
2413   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2414   union exp_element *pc;
2415   struct axs_value value;
2416 
2417   old_chain = make_cleanup_free_agent_expr (ax);
2418 
2419   pc = expr->elts;
2420   trace_kludge = 0;
2421   value.optimized_out = 0;
2422   gen_expr (expr, &pc, ax, &value);
2423 
2424   require_rvalue (ax, &value);
2425 
2426   /* Oh, and terminate.  */
2427   ax_simple (ax, aop_end);
2428 
2429   /* We have successfully built the agent expr, so cancel the cleanup
2430      request.  If we add more cleanups that we always want done, this
2431      will have to get more complicated.  */
2432   discard_cleanups (old_chain);
2433   return ax;
2434 }
2435 
2436 static void
2437 agent_command (char *exp, int from_tty)
2438 {
2439   struct cleanup *old_chain = 0;
2440   struct expression *expr;
2441   struct agent_expr *agent;
2442   struct frame_info *fi = get_current_frame ();	/* need current scope */
2443 
2444   /* We don't deal with overlay debugging at the moment.  We need to
2445      think more carefully about this.  If you copy this code into
2446      another command, change the error message; the user shouldn't
2447      have to know anything about agent expressions.  */
2448   if (overlay_debugging)
2449     error (_("GDB can't do agent expression translation with overlays."));
2450 
2451   if (exp == 0)
2452     error_no_arg (_("expression to translate"));
2453 
2454   expr = parse_expression (exp);
2455   old_chain = make_cleanup (free_current_contents, &expr);
2456   agent = gen_trace_for_expr (get_frame_pc (fi), expr);
2457   make_cleanup_free_agent_expr (agent);
2458   ax_reqs (agent);
2459   ax_print (gdb_stdout, agent);
2460 
2461   /* It would be nice to call ax_reqs here to gather some general info
2462      about the expression, and then print out the result.  */
2463 
2464   do_cleanups (old_chain);
2465   dont_repeat ();
2466 }
2467 
2468 /* Parse the given expression, compile it into an agent expression
2469    that does direct evaluation, and display the resulting
2470    expression.  */
2471 
2472 static void
2473 agent_eval_command (char *exp, int from_tty)
2474 {
2475   struct cleanup *old_chain = 0;
2476   struct expression *expr;
2477   struct agent_expr *agent;
2478   struct frame_info *fi = get_current_frame ();	/* need current scope */
2479 
2480   /* We don't deal with overlay debugging at the moment.  We need to
2481      think more carefully about this.  If you copy this code into
2482      another command, change the error message; the user shouldn't
2483      have to know anything about agent expressions.  */
2484   if (overlay_debugging)
2485     error (_("GDB can't do agent expression translation with overlays."));
2486 
2487   if (exp == 0)
2488     error_no_arg (_("expression to translate"));
2489 
2490   expr = parse_expression (exp);
2491   old_chain = make_cleanup (free_current_contents, &expr);
2492   agent = gen_eval_for_expr (get_frame_pc (fi), expr);
2493   make_cleanup_free_agent_expr (agent);
2494   ax_reqs (agent);
2495   ax_print (gdb_stdout, agent);
2496 
2497   /* It would be nice to call ax_reqs here to gather some general info
2498      about the expression, and then print out the result.  */
2499 
2500   do_cleanups (old_chain);
2501   dont_repeat ();
2502 }
2503 
2504 
2505 /* Initialization code.  */
2506 
2507 void _initialize_ax_gdb (void);
2508 void
2509 _initialize_ax_gdb (void)
2510 {
2511   add_cmd ("agent", class_maintenance, agent_command,
2512 	   _("Translate an expression into remote agent bytecode for tracing."),
2513 	   &maintenancelist);
2514 
2515   add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2516 	   _("Translate an expression into remote agent bytecode for evaluation."),
2517 	   &maintenancelist);
2518 }
2519