ax-gdb: Remove unnecessary gdbarch parameters
[external/binutils.git] / gdb / ax-gdb.c
1 /* GDB-specific functions for operating on agent expressions.
2
3    Copyright (C) 1998-2017 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "symfile.h"
23 #include "gdbtypes.h"
24 #include "language.h"
25 #include "value.h"
26 #include "expression.h"
27 #include "command.h"
28 #include "gdbcmd.h"
29 #include "frame.h"
30 #include "target.h"
31 #include "ax.h"
32 #include "ax-gdb.h"
33 #include "block.h"
34 #include "regcache.h"
35 #include "user-regs.h"
36 #include "dictionary.h"
37 #include "breakpoint.h"
38 #include "tracepoint.h"
39 #include "cp-support.h"
40 #include "arch-utils.h"
41 #include "cli/cli-utils.h"
42 #include "linespec.h"
43 #include "location.h"
44 #include "objfiles.h"
45
46 #include "valprint.h"
47 #include "c-lang.h"
48
49 #include "format.h"
50
51 /* To make sense of this file, you should read doc/agentexpr.texi.
52    Then look at the types and enums in ax-gdb.h.  For the code itself,
53    look at gen_expr, towards the bottom; that's the main function that
54    looks at the GDB expressions and calls everything else to generate
55    code.
56
57    I'm beginning to wonder whether it wouldn't be nicer to internally
58    generate trees, with types, and then spit out the bytecode in
59    linear form afterwards; we could generate fewer `swap', `ext', and
60    `zero_ext' bytecodes that way; it would make good constant folding
61    easier, too.  But at the moment, I think we should be willing to
62    pay for the simplicity of this code with less-than-optimal bytecode
63    strings.
64
65    Remember, "GBD" stands for "Great Britain, Dammit!"  So be careful.  */
66 \f
67
68
69 /* Prototypes for local functions.  */
70
71 /* There's a standard order to the arguments of these functions:
72    union exp_element ** --- pointer into expression
73    struct agent_expr * --- agent expression buffer to generate code into
74    struct axs_value * --- describes value left on top of stack  */
75
76 static struct value *const_var_ref (struct symbol *var);
77 static struct value *const_expr (union exp_element **pc);
78 static struct value *maybe_const_expr (union exp_element **pc);
79
80 static void gen_traced_pop (struct agent_expr *, struct axs_value *);
81
82 static void gen_sign_extend (struct agent_expr *, struct type *);
83 static void gen_extend (struct agent_expr *, struct type *);
84 static void gen_fetch (struct agent_expr *, struct type *);
85 static void gen_left_shift (struct agent_expr *, int);
86
87
88 static void gen_frame_args_address (struct agent_expr *);
89 static void gen_frame_locals_address (struct agent_expr *);
90 static void gen_offset (struct agent_expr *ax, int offset);
91 static void gen_sym_offset (struct agent_expr *, struct symbol *);
92 static void gen_var_ref (struct agent_expr *ax, struct axs_value *value,
93                          struct symbol *var);
94
95
96 static void gen_int_literal (struct agent_expr *ax,
97                              struct axs_value *value,
98                              LONGEST k, struct type *type);
99
100 static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
101 static int type_wider_than (struct type *type1, struct type *type2);
102 static struct type *max_type (struct type *type1, struct type *type2);
103 static void gen_conversion (struct agent_expr *ax,
104                             struct type *from, struct type *to);
105 static int is_nontrivial_conversion (struct type *from, struct type *to);
106 static void gen_usual_arithmetic (struct agent_expr *ax,
107                                   struct axs_value *value1,
108                                   struct axs_value *value2);
109 static void gen_integral_promotions (struct agent_expr *ax,
110                                      struct axs_value *value);
111 static void gen_cast (struct agent_expr *ax,
112                       struct axs_value *value, struct type *type);
113 static void gen_scale (struct agent_expr *ax,
114                        enum agent_op op, struct type *type);
115 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
116                         struct axs_value *value1, struct axs_value *value2);
117 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
118                         struct axs_value *value1, struct axs_value *value2);
119 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
120                          struct axs_value *value1, struct axs_value *value2,
121                          struct type *result_type);
122 static void gen_binop (struct agent_expr *ax,
123                        struct axs_value *value,
124                        struct axs_value *value1,
125                        struct axs_value *value2,
126                        enum agent_op op,
127                        enum agent_op op_unsigned, int may_carry,
128                        const char *name);
129 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
130                              struct type *result_type);
131 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
132 static void gen_deref (struct axs_value *);
133 static void gen_address_of (struct axs_value *);
134 static void gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
135                               struct type *type, int start, int end);
136 static void gen_primitive_field (struct agent_expr *ax,
137                                  struct axs_value *value,
138                                  int offset, int fieldno, struct type *type);
139 static int gen_struct_ref_recursive (struct agent_expr *ax,
140                                      struct axs_value *value,
141                                      const char *field, int offset,
142                                      struct type *type);
143 static void gen_struct_ref (struct agent_expr *ax,
144                             struct axs_value *value,
145                             const char *field,
146                             const char *operator_name,
147                             const char *operand_name);
148 static void gen_static_field (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_binop_rest (struct expression *exp,
156                                  enum exp_opcode op, union exp_element **pc,
157                                  struct agent_expr *ax,
158                                  struct axs_value *value,
159                                  struct axs_value *value1,
160                                  struct axs_value *value2);
161
162 static void agent_command (char *exp, int from_tty);
163 \f
164
165 /* Detecting constant expressions.  */
166
167 /* If the variable reference at *PC is a constant, return its value.
168    Otherwise, return zero.
169
170    Hey, Wally!  How can a variable reference be a constant?
171
172    Well, Beav, this function really handles the OP_VAR_VALUE operator,
173    not specifically variable references.  GDB uses OP_VAR_VALUE to
174    refer to any kind of symbolic reference: function names, enum
175    elements, and goto labels are all handled through the OP_VAR_VALUE
176    operator, even though they're constants.  It makes sense given the
177    situation.
178
179    Gee, Wally, don'cha wonder sometimes if data representations that
180    subvert commonly accepted definitions of terms in favor of heavily
181    context-specific interpretations are really just a tool of the
182    programming hegemony to preserve their power and exclude the
183    proletariat?  */
184
185 static struct value *
186 const_var_ref (struct symbol *var)
187 {
188   struct type *type = SYMBOL_TYPE (var);
189
190   switch (SYMBOL_CLASS (var))
191     {
192     case LOC_CONST:
193       return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
194
195     case LOC_LABEL:
196       return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
197
198     default:
199       return 0;
200     }
201 }
202
203
204 /* If the expression starting at *PC has a constant value, return it.
205    Otherwise, return zero.  If we return a value, then *PC will be
206    advanced to the end of it.  If we return zero, *PC could be
207    anywhere.  */
208 static struct value *
209 const_expr (union exp_element **pc)
210 {
211   enum exp_opcode op = (*pc)->opcode;
212   struct value *v1;
213
214   switch (op)
215     {
216     case OP_LONG:
217       {
218         struct type *type = (*pc)[1].type;
219         LONGEST k = (*pc)[2].longconst;
220
221         (*pc) += 4;
222         return value_from_longest (type, k);
223       }
224
225     case OP_VAR_VALUE:
226       {
227         struct value *v = const_var_ref ((*pc)[2].symbol);
228
229         (*pc) += 4;
230         return v;
231       }
232
233       /* We could add more operators in here.  */
234
235     case UNOP_NEG:
236       (*pc)++;
237       v1 = const_expr (pc);
238       if (v1)
239         return value_neg (v1);
240       else
241         return 0;
242
243     default:
244       return 0;
245     }
246 }
247
248
249 /* Like const_expr, but guarantee also that *PC is undisturbed if the
250    expression is not constant.  */
251 static struct value *
252 maybe_const_expr (union exp_element **pc)
253 {
254   union exp_element *tentative_pc = *pc;
255   struct value *v = const_expr (&tentative_pc);
256
257   /* If we got a value, then update the real PC.  */
258   if (v)
259     *pc = tentative_pc;
260
261   return v;
262 }
263 \f
264
265 /* Generating bytecode from GDB expressions: general assumptions */
266
267 /* Here are a few general assumptions made throughout the code; if you
268    want to make a change that contradicts one of these, then you'd
269    better scan things pretty thoroughly.
270
271    - We assume that all values occupy one stack element.  For example,
272    sometimes we'll swap to get at the left argument to a binary
273    operator.  If we decide that void values should occupy no stack
274    elements, or that synthetic arrays (whose size is determined at
275    run time, created by the `@' operator) should occupy two stack
276    elements (address and length), then this will cause trouble.
277
278    - We assume the stack elements are infinitely wide, and that we
279    don't have to worry what happens if the user requests an
280    operation that is wider than the actual interpreter's stack.
281    That is, it's up to the interpreter to handle directly all the
282    integer widths the user has access to.  (Woe betide the language
283    with bignums!)
284
285    - We don't support side effects.  Thus, we don't have to worry about
286    GCC's generalized lvalues, function calls, etc.
287
288    - We don't support floating point.  Many places where we switch on
289    some type don't bother to include cases for floating point; there
290    may be even more subtle ways this assumption exists.  For
291    example, the arguments to % must be integers.
292
293    - We assume all subexpressions have a static, unchanging type.  If
294    we tried to support convenience variables, this would be a
295    problem.
296
297    - All values on the stack should always be fully zero- or
298    sign-extended.
299
300    (I wasn't sure whether to choose this or its opposite --- that
301    only addresses are assumed extended --- but it turns out that
302    neither convention completely eliminates spurious extend
303    operations (if everything is always extended, then you have to
304    extend after add, because it could overflow; if nothing is
305    extended, then you end up producing extends whenever you change
306    sizes), and this is simpler.)  */
307 \f
308
309 /* Scan for all static fields in the given class, including any base
310    classes, and generate tracing bytecodes for each.  */
311
312 static void
313 gen_trace_static_fields (struct agent_expr *ax,
314                          struct type *type)
315 {
316   int i, nbases = TYPE_N_BASECLASSES (type);
317   struct axs_value value;
318
319   type = check_typedef (type);
320
321   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
322     {
323       if (field_is_static (&TYPE_FIELD (type, i)))
324         {
325           gen_static_field (ax, &value, type, i);
326           if (value.optimized_out)
327             continue;
328           switch (value.kind)
329             {
330             case axs_lvalue_memory:
331               {
332                 /* Initialize the TYPE_LENGTH if it is a typedef.  */
333                 check_typedef (value.type);
334                 ax_const_l (ax, TYPE_LENGTH (value.type));
335                 ax_simple (ax, aop_trace);
336               }
337               break;
338
339             case axs_lvalue_register:
340               /* We don't actually need the register's value to be pushed,
341                  just note that we need it to be collected.  */
342               ax_reg_mask (ax, value.u.reg);
343
344             default:
345               break;
346             }
347         }
348     }
349
350   /* Now scan through base classes recursively.  */
351   for (i = 0; i < nbases; i++)
352     {
353       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
354
355       gen_trace_static_fields (ax, basetype);
356     }
357 }
358
359 /* Trace the lvalue on the stack, if it needs it.  In either case, pop
360    the value.  Useful on the left side of a comma, and at the end of
361    an expression being used for tracing.  */
362 static void
363 gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
364 {
365   int string_trace = 0;
366   if (ax->trace_string
367       && TYPE_CODE (value->type) == TYPE_CODE_PTR
368       && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
369                                  's'))
370     string_trace = 1;
371
372   if (ax->tracing)
373     switch (value->kind)
374       {
375       case axs_rvalue:
376         if (string_trace)
377           {
378             ax_const_l (ax, ax->trace_string);
379             ax_simple (ax, aop_tracenz);
380           }
381         else
382           /* We don't trace rvalues, just the lvalues necessary to
383              produce them.  So just dispose of this value.  */
384           ax_simple (ax, aop_pop);
385         break;
386
387       case axs_lvalue_memory:
388         {
389           /* Initialize the TYPE_LENGTH if it is a typedef.  */
390           check_typedef (value->type);
391
392           if (string_trace)
393             {
394               gen_fetch (ax, value->type);
395               ax_const_l (ax, ax->trace_string);
396               ax_simple (ax, aop_tracenz);
397             }
398           else
399             {
400               /* There's no point in trying to use a trace_quick bytecode
401                  here, since "trace_quick SIZE pop" is three bytes, whereas
402                  "const8 SIZE trace" is also three bytes, does the same
403                  thing, and the simplest code which generates that will also
404                  work correctly for objects with large sizes.  */
405               ax_const_l (ax, TYPE_LENGTH (value->type));
406               ax_simple (ax, aop_trace);
407             }
408         }
409         break;
410
411       case axs_lvalue_register:
412         /* We don't actually need the register's value to be on the
413            stack, and the target will get heartburn if the register is
414            larger than will fit in a stack, so just mark it for
415            collection and be done with it.  */
416         ax_reg_mask (ax, value->u.reg);
417        
418         /* But if the register points to a string, assume the value
419            will fit on the stack and push it anyway.  */
420         if (string_trace)
421           {
422             ax_reg (ax, value->u.reg);
423             ax_const_l (ax, ax->trace_string);
424             ax_simple (ax, aop_tracenz);
425           }
426         break;
427       }
428   else
429     /* If we're not tracing, just pop the value.  */
430     ax_simple (ax, aop_pop);
431
432   /* To trace C++ classes with static fields stored elsewhere.  */
433   if (ax->tracing
434       && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
435           || TYPE_CODE (value->type) == TYPE_CODE_UNION))
436     gen_trace_static_fields (ax, value->type);
437 }
438 \f
439
440
441 /* Generating bytecode from GDB expressions: helper functions */
442
443 /* Assume that the lower bits of the top of the stack is a value of
444    type TYPE, and the upper bits are zero.  Sign-extend if necessary.  */
445 static void
446 gen_sign_extend (struct agent_expr *ax, struct type *type)
447 {
448   /* Do we need to sign-extend this?  */
449   if (!TYPE_UNSIGNED (type))
450     ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
451 }
452
453
454 /* Assume the lower bits of the top of the stack hold a value of type
455    TYPE, and the upper bits are garbage.  Sign-extend or truncate as
456    needed.  */
457 static void
458 gen_extend (struct agent_expr *ax, struct type *type)
459 {
460   int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
461
462   /* I just had to.  */
463   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
464 }
465
466
467 /* Assume that the top of the stack contains a value of type "pointer
468    to TYPE"; generate code to fetch its value.  Note that TYPE is the
469    target type, not the pointer type.  */
470 static void
471 gen_fetch (struct agent_expr *ax, struct type *type)
472 {
473   if (ax->tracing)
474     {
475       /* Record the area of memory we're about to fetch.  */
476       ax_trace_quick (ax, TYPE_LENGTH (type));
477     }
478
479   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
480     type = TYPE_TARGET_TYPE (type);
481
482   switch (TYPE_CODE (type))
483     {
484     case TYPE_CODE_PTR:
485     case TYPE_CODE_REF:
486     case TYPE_CODE_RVALUE_REF:
487     case TYPE_CODE_ENUM:
488     case TYPE_CODE_INT:
489     case TYPE_CODE_CHAR:
490     case TYPE_CODE_BOOL:
491       /* It's a scalar value, so we know how to dereference it.  How
492          many bytes long is it?  */
493       switch (TYPE_LENGTH (type))
494         {
495         case 8 / TARGET_CHAR_BIT:
496           ax_simple (ax, aop_ref8);
497           break;
498         case 16 / TARGET_CHAR_BIT:
499           ax_simple (ax, aop_ref16);
500           break;
501         case 32 / TARGET_CHAR_BIT:
502           ax_simple (ax, aop_ref32);
503           break;
504         case 64 / TARGET_CHAR_BIT:
505           ax_simple (ax, aop_ref64);
506           break;
507
508           /* Either our caller shouldn't have asked us to dereference
509              that pointer (other code's fault), or we're not
510              implementing something we should be (this code's fault).
511              In any case, it's a bug the user shouldn't see.  */
512         default:
513           internal_error (__FILE__, __LINE__,
514                           _("gen_fetch: strange size"));
515         }
516
517       gen_sign_extend (ax, type);
518       break;
519
520     default:
521       /* Our caller requested us to dereference a pointer from an unsupported
522          type.  Error out and give callers a chance to handle the failure
523          gracefully.  */
524       error (_("gen_fetch: Unsupported type code `%s'."),
525              TYPE_NAME (type));
526     }
527 }
528
529
530 /* Generate code to left shift the top of the stack by DISTANCE bits, or
531    right shift it by -DISTANCE bits if DISTANCE < 0.  This generates
532    unsigned (logical) right shifts.  */
533 static void
534 gen_left_shift (struct agent_expr *ax, int distance)
535 {
536   if (distance > 0)
537     {
538       ax_const_l (ax, distance);
539       ax_simple (ax, aop_lsh);
540     }
541   else if (distance < 0)
542     {
543       ax_const_l (ax, -distance);
544       ax_simple (ax, aop_rsh_unsigned);
545     }
546 }
547 \f
548
549
550 /* Generating bytecode from GDB expressions: symbol references */
551
552 /* Generate code to push the base address of the argument portion of
553    the top stack frame.  */
554 static void
555 gen_frame_args_address (struct agent_expr *ax)
556 {
557   int frame_reg;
558   LONGEST frame_offset;
559
560   gdbarch_virtual_frame_pointer (ax->gdbarch,
561                                  ax->scope, &frame_reg, &frame_offset);
562   ax_reg (ax, frame_reg);
563   gen_offset (ax, frame_offset);
564 }
565
566
567 /* Generate code to push the base address of the locals portion of the
568    top stack frame.  */
569 static void
570 gen_frame_locals_address (struct agent_expr *ax)
571 {
572   int frame_reg;
573   LONGEST frame_offset;
574
575   gdbarch_virtual_frame_pointer (ax->gdbarch,
576                                  ax->scope, &frame_reg, &frame_offset);
577   ax_reg (ax, frame_reg);
578   gen_offset (ax, frame_offset);
579 }
580
581
582 /* Generate code to add OFFSET to the top of the stack.  Try to
583    generate short and readable code.  We use this for getting to
584    variables on the stack, and structure members.  If we were
585    programming in ML, it would be clearer why these are the same
586    thing.  */
587 static void
588 gen_offset (struct agent_expr *ax, int offset)
589 {
590   /* It would suffice to simply push the offset and add it, but this
591      makes it easier to read positive and negative offsets in the
592      bytecode.  */
593   if (offset > 0)
594     {
595       ax_const_l (ax, offset);
596       ax_simple (ax, aop_add);
597     }
598   else if (offset < 0)
599     {
600       ax_const_l (ax, -offset);
601       ax_simple (ax, aop_sub);
602     }
603 }
604
605
606 /* In many cases, a symbol's value is the offset from some other
607    address (stack frame, base register, etc.)  Generate code to add
608    VAR's value to the top of the stack.  */
609 static void
610 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
611 {
612   gen_offset (ax, SYMBOL_VALUE (var));
613 }
614
615
616 /* Generate code for a variable reference to AX.  The variable is the
617    symbol VAR.  Set VALUE to describe the result.  */
618
619 static void
620 gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
621 {
622   /* Dereference any typedefs.  */
623   value->type = check_typedef (SYMBOL_TYPE (var));
624   value->optimized_out = 0;
625
626   if (SYMBOL_COMPUTED_OPS (var) != NULL)
627     {
628       SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, ax, value);
629       return;
630     }
631
632   /* I'm imitating the code in read_var_value.  */
633   switch (SYMBOL_CLASS (var))
634     {
635     case LOC_CONST:             /* A constant, like an enum value.  */
636       ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
637       value->kind = axs_rvalue;
638       break;
639
640     case LOC_LABEL:             /* A goto label, being used as a value.  */
641       ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
642       value->kind = axs_rvalue;
643       break;
644
645     case LOC_CONST_BYTES:
646       internal_error (__FILE__, __LINE__,
647                       _("gen_var_ref: LOC_CONST_BYTES "
648                         "symbols are not supported"));
649
650       /* Variable at a fixed location in memory.  Easy.  */
651     case LOC_STATIC:
652       /* Push the address of the variable.  */
653       ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
654       value->kind = axs_lvalue_memory;
655       break;
656
657     case LOC_ARG:               /* var lives in argument area of frame */
658       gen_frame_args_address (ax);
659       gen_sym_offset (ax, var);
660       value->kind = axs_lvalue_memory;
661       break;
662
663     case LOC_REF_ARG:           /* As above, but the frame slot really
664                                    holds the address of the variable.  */
665       gen_frame_args_address (ax);
666       gen_sym_offset (ax, var);
667       /* Don't assume any particular pointer size.  */
668       gen_fetch (ax, builtin_type (ax->gdbarch)->builtin_data_ptr);
669       value->kind = axs_lvalue_memory;
670       break;
671
672     case LOC_LOCAL:             /* var lives in locals area of frame */
673       gen_frame_locals_address (ax);
674       gen_sym_offset (ax, var);
675       value->kind = axs_lvalue_memory;
676       break;
677
678     case LOC_TYPEDEF:
679       error (_("Cannot compute value of typedef `%s'."),
680              SYMBOL_PRINT_NAME (var));
681       break;
682
683     case LOC_BLOCK:
684       ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
685       value->kind = axs_rvalue;
686       break;
687
688     case LOC_REGISTER:
689       /* Don't generate any code at all; in the process of treating
690          this as an lvalue or rvalue, the caller will generate the
691          right code.  */
692       value->kind = axs_lvalue_register;
693       value->u.reg
694         = SYMBOL_REGISTER_OPS (var)->register_number (var, ax->gdbarch);
695       break;
696
697       /* A lot like LOC_REF_ARG, but the pointer lives directly in a
698          register, not on the stack.  Simpler than LOC_REGISTER
699          because it's just like any other case where the thing
700          has a real address.  */
701     case LOC_REGPARM_ADDR:
702       ax_reg (ax,
703               SYMBOL_REGISTER_OPS (var)->register_number (var, ax->gdbarch));
704       value->kind = axs_lvalue_memory;
705       break;
706
707     case LOC_UNRESOLVED:
708       {
709         struct bound_minimal_symbol msym
710           = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
711
712         if (!msym.minsym)
713           error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
714
715         /* Push the address of the variable.  */
716         ax_const_l (ax, BMSYMBOL_VALUE_ADDRESS (msym));
717         value->kind = axs_lvalue_memory;
718       }
719       break;
720
721     case LOC_COMPUTED:
722       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
723
724     case LOC_OPTIMIZED_OUT:
725       /* Flag this, but don't say anything; leave it up to callers to
726          warn the user.  */
727       value->optimized_out = 1;
728       break;
729
730     default:
731       error (_("Cannot find value of botched symbol `%s'."),
732              SYMBOL_PRINT_NAME (var));
733       break;
734     }
735 }
736 \f
737
738
739 /* Generating bytecode from GDB expressions: literals */
740
741 static void
742 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
743                  struct type *type)
744 {
745   ax_const_l (ax, k);
746   value->kind = axs_rvalue;
747   value->type = check_typedef (type);
748 }
749 \f
750
751
752 /* Generating bytecode from GDB expressions: unary conversions, casts */
753
754 /* Take what's on the top of the stack (as described by VALUE), and
755    try to make an rvalue out of it.  Signal an error if we can't do
756    that.  */
757 void
758 require_rvalue (struct agent_expr *ax, struct axs_value *value)
759 {
760   /* Only deal with scalars, structs and such may be too large
761      to fit in a stack entry.  */
762   value->type = check_typedef (value->type);
763   if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
764       || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
765       || TYPE_CODE (value->type) == TYPE_CODE_UNION
766       || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
767     error (_("Value not scalar: cannot be an rvalue."));
768
769   switch (value->kind)
770     {
771     case axs_rvalue:
772       /* It's already an rvalue.  */
773       break;
774
775     case axs_lvalue_memory:
776       /* The top of stack is the address of the object.  Dereference.  */
777       gen_fetch (ax, value->type);
778       break;
779
780     case axs_lvalue_register:
781       /* There's nothing on the stack, but value->u.reg is the
782          register number containing the value.
783
784          When we add floating-point support, this is going to have to
785          change.  What about SPARC register pairs, for example?  */
786       ax_reg (ax, value->u.reg);
787       gen_extend (ax, value->type);
788       break;
789     }
790
791   value->kind = axs_rvalue;
792 }
793
794
795 /* Assume the top of the stack is described by VALUE, and perform the
796    usual unary conversions.  This is motivated by ANSI 6.2.2, but of
797    course GDB expressions are not ANSI; they're the mishmash union of
798    a bunch of languages.  Rah.
799
800    NOTE!  This function promises to produce an rvalue only when the
801    incoming value is of an appropriate type.  In other words, the
802    consumer of the value this function produces may assume the value
803    is an rvalue only after checking its type.
804
805    The immediate issue is that if the user tries to use a structure or
806    union as an operand of, say, the `+' operator, we don't want to try
807    to convert that structure to an rvalue; require_rvalue will bomb on
808    structs and unions.  Rather, we want to simply pass the struct
809    lvalue through unchanged, and let `+' raise an error.  */
810
811 static void
812 gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
813 {
814   /* We don't have to generate any code for the usual integral
815      conversions, since values are always represented as full-width on
816      the stack.  Should we tweak the type?  */
817
818   /* Some types require special handling.  */
819   switch (TYPE_CODE (value->type))
820     {
821       /* Functions get converted to a pointer to the function.  */
822     case TYPE_CODE_FUNC:
823       value->type = lookup_pointer_type (value->type);
824       value->kind = axs_rvalue; /* Should always be true, but just in case.  */
825       break;
826
827       /* Arrays get converted to a pointer to their first element, and
828          are no longer an lvalue.  */
829     case TYPE_CODE_ARRAY:
830       {
831         struct type *elements = TYPE_TARGET_TYPE (value->type);
832
833         value->type = lookup_pointer_type (elements);
834         value->kind = axs_rvalue;
835         /* We don't need to generate any code; the address of the array
836            is also the address of its first element.  */
837       }
838       break;
839
840       /* Don't try to convert structures and unions to rvalues.  Let the
841          consumer signal an error.  */
842     case TYPE_CODE_STRUCT:
843     case TYPE_CODE_UNION:
844       return;
845     }
846
847   /* If the value is an lvalue, dereference it.  */
848   require_rvalue (ax, value);
849 }
850
851
852 /* Return non-zero iff the type TYPE1 is considered "wider" than the
853    type TYPE2, according to the rules described in gen_usual_arithmetic.  */
854 static int
855 type_wider_than (struct type *type1, struct type *type2)
856 {
857   return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
858           || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
859               && TYPE_UNSIGNED (type1)
860               && !TYPE_UNSIGNED (type2)));
861 }
862
863
864 /* Return the "wider" of the two types TYPE1 and TYPE2.  */
865 static struct type *
866 max_type (struct type *type1, struct type *type2)
867 {
868   return type_wider_than (type1, type2) ? type1 : type2;
869 }
870
871
872 /* Generate code to convert a scalar value of type FROM to type TO.  */
873 static void
874 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
875 {
876   /* Perhaps there is a more graceful way to state these rules.  */
877
878   /* If we're converting to a narrower type, then we need to clear out
879      the upper bits.  */
880   if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
881     gen_extend (ax, to);
882
883   /* If the two values have equal width, but different signednesses,
884      then we need to extend.  */
885   else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
886     {
887       if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
888         gen_extend (ax, to);
889     }
890
891   /* If we're converting to a wider type, and becoming unsigned, then
892      we need to zero out any possible sign bits.  */
893   else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
894     {
895       if (TYPE_UNSIGNED (to))
896         gen_extend (ax, to);
897     }
898 }
899
900
901 /* Return non-zero iff the type FROM will require any bytecodes to be
902    emitted to be converted to the type TO.  */
903 static int
904 is_nontrivial_conversion (struct type *from, struct type *to)
905 {
906   agent_expr_up ax (new agent_expr (NULL, 0));
907   int nontrivial;
908
909   /* Actually generate the code, and see if anything came out.  At the
910      moment, it would be trivial to replicate the code in
911      gen_conversion here, but in the future, when we're supporting
912      floating point and the like, it may not be.  Doing things this
913      way allows this function to be independent of the logic in
914      gen_conversion.  */
915   gen_conversion (ax.get (), from, to);
916   nontrivial = ax->len > 0;
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 agent_expr *ax, struct axs_value *value1,
928                       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 (ax->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 agent_expr *ax, struct axs_value *value)
965 {
966   const struct builtin_type *builtin = builtin_type (ax->gdbarch);
967
968   if (!type_wider_than (value->type, builtin->builtin_int))
969     {
970       gen_conversion (ax, value->type, builtin->builtin_int);
971       value->type = builtin->builtin_int;
972     }
973   else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
974     {
975       gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
976       value->type = builtin->builtin_unsigned_int;
977     }
978 }
979
980
981 /* Generate code for a cast to TYPE.  */
982 static void
983 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
984 {
985   /* GCC does allow casts to yield lvalues, so this should be fixed
986      before merging these changes into the trunk.  */
987   require_rvalue (ax, value);
988   /* Dereference typedefs.  */
989   type = check_typedef (type);
990
991   switch (TYPE_CODE (type))
992     {
993     case TYPE_CODE_PTR:
994     case TYPE_CODE_REF:
995     case TYPE_CODE_RVALUE_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 \f
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,
1142            enum agent_op op, enum agent_op op_unsigned,
1143            int may_carry, const char *name)
1144 {
1145   /* We only handle INT op INT.  */
1146   if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1147       || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1148     error (_("Invalid combination of types in %s."), name);
1149
1150   ax_simple (ax,
1151              TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1152   if (may_carry)
1153     gen_extend (ax, value1->type);      /* catch overflow */
1154   value->type = value1->type;
1155   value->kind = axs_rvalue;
1156 }
1157
1158
1159 static void
1160 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1161                  struct type *result_type)
1162 {
1163   if (TYPE_CODE (value->type) != TYPE_CODE_INT
1164       && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1165     error (_("Invalid type of operand to `!'."));
1166
1167   ax_simple (ax, aop_log_not);
1168   value->type = result_type;
1169 }
1170
1171
1172 static void
1173 gen_complement (struct agent_expr *ax, struct axs_value *value)
1174 {
1175   if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1176     error (_("Invalid type of operand to `~'."));
1177
1178   ax_simple (ax, aop_bit_not);
1179   gen_extend (ax, value->type);
1180 }
1181 \f
1182
1183
1184 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1185
1186 /* Dereference the value on the top of the stack.  */
1187 static void
1188 gen_deref (struct axs_value *value)
1189 {
1190   /* The caller should check the type, because several operators use
1191      this, and we don't know what error message to generate.  */
1192   if (!pointer_type (value->type))
1193     internal_error (__FILE__, __LINE__,
1194                     _("gen_deref: expected a pointer"));
1195
1196   /* We've got an rvalue now, which is a pointer.  We want to yield an
1197      lvalue, whose address is exactly that pointer.  So we don't
1198      actually emit any code; we just change the type from "Pointer to
1199      T" to "T", and mark the value as an lvalue in memory.  Leave it
1200      to the consumer to actually dereference it.  */
1201   value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1202   if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1203     error (_("Attempt to dereference a generic pointer."));
1204   value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1205                  ? axs_rvalue : axs_lvalue_memory);
1206 }
1207
1208
1209 /* Produce the address of the lvalue on the top of the stack.  */
1210 static void
1211 gen_address_of (struct axs_value *value)
1212 {
1213   /* Special case for taking the address of a function.  The ANSI
1214      standard describes this as a special case, too, so this
1215      arrangement is not without motivation.  */
1216   if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1217     /* The value's already an rvalue on the stack, so we just need to
1218        change the type.  */
1219     value->type = lookup_pointer_type (value->type);
1220   else
1221     switch (value->kind)
1222       {
1223       case axs_rvalue:
1224         error (_("Operand of `&' is an rvalue, which has no address."));
1225
1226       case axs_lvalue_register:
1227         error (_("Operand of `&' is in a register, and has no address."));
1228
1229       case axs_lvalue_memory:
1230         value->kind = axs_rvalue;
1231         value->type = lookup_pointer_type (value->type);
1232         break;
1233       }
1234 }
1235
1236 /* Generate code to push the value of a bitfield of a structure whose
1237    address is on the top of the stack.  START and END give the
1238    starting and one-past-ending *bit* numbers of the field within the
1239    structure.  */
1240 static void
1241 gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1242                   struct type *type, 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 (ax->tracing)
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 (ax->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 agent_expr *ax, struct axs_value *value,
1401                      int offset, int fieldno, struct type *type)
1402 {
1403   /* Is this a bitfield?  */
1404   if (TYPE_FIELD_PACKED (type, fieldno))
1405     gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, fieldno),
1406                       (offset * TARGET_CHAR_BIT
1407                        + TYPE_FIELD_BITPOS (type, fieldno)),
1408                       (offset * TARGET_CHAR_BIT
1409                        + TYPE_FIELD_BITPOS (type, fieldno)
1410                        + TYPE_FIELD_BITSIZE (type, fieldno)));
1411   else
1412     {
1413       gen_offset (ax, offset
1414                   + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1415       value->kind = axs_lvalue_memory;
1416       value->type = TYPE_FIELD_TYPE (type, fieldno);
1417     }
1418 }
1419
1420 /* Search for the given field in either the given type or one of its
1421    base classes.  Return 1 if found, 0 if not.  */
1422
1423 static int
1424 gen_struct_ref_recursive (struct agent_expr *ax, struct axs_value *value,
1425                           const char *field, int offset, struct type *type)
1426 {
1427   int i, rslt;
1428   int nbases = TYPE_N_BASECLASSES (type);
1429
1430   type = check_typedef (type);
1431
1432   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1433     {
1434       const char *this_name = TYPE_FIELD_NAME (type, i);
1435
1436       if (this_name)
1437         {
1438           if (strcmp (field, this_name) == 0)
1439             {
1440               /* Note that bytecodes for the struct's base (aka
1441                  "this") will have been generated already, which will
1442                  be unnecessary but not harmful if the static field is
1443                  being handled as a global.  */
1444               if (field_is_static (&TYPE_FIELD (type, i)))
1445                 {
1446                   gen_static_field (ax, value, type, i);
1447                   if (value->optimized_out)
1448                     error (_("static field `%s' has been "
1449                              "optimized out, cannot use"),
1450                            field);
1451                   return 1;
1452                 }
1453
1454               gen_primitive_field (ax, value, offset, i, type);
1455               return 1;
1456             }
1457 #if 0 /* is this right? */
1458           if (this_name[0] == '\0')
1459             internal_error (__FILE__, __LINE__,
1460                             _("find_field: anonymous unions not supported"));
1461 #endif
1462         }
1463     }
1464
1465   /* Now scan through base classes recursively.  */
1466   for (i = 0; i < nbases; i++)
1467     {
1468       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1469
1470       rslt = gen_struct_ref_recursive (ax, value, field,
1471                                        offset + TYPE_BASECLASS_BITPOS (type, i)
1472                                        / 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 agent_expr *ax, struct axs_value *value,
1489                 const char *field, const char *operator_name,
1490                 const 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 (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 (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 agent_expr *ax, struct axs_value *value,
1526                    const struct type *curtype, char *name);
1527 static int
1528 gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1529                          const struct type *curtype, char *name);
1530
1531 static void
1532 gen_static_field (struct agent_expr *ax, struct axs_value *value,
1533                   struct type *type, int fieldno)
1534 {
1535   if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1536     {
1537       ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1538       value->kind = axs_lvalue_memory;
1539       value->type = TYPE_FIELD_TYPE (type, fieldno);
1540       value->optimized_out = 0;
1541     }
1542   else
1543     {
1544       const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1545       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0).symbol;
1546
1547       if (sym)
1548         {
1549           gen_var_ref (ax, value, sym);
1550   
1551           /* Don't error if the value was optimized out, we may be
1552              scanning all static fields and just want to pass over this
1553              and continue with the rest.  */
1554         }
1555       else
1556         {
1557           /* Silently assume this was optimized out; class printing
1558              will let the user know why the data is missing.  */
1559           value->optimized_out = 1;
1560         }
1561     }
1562 }
1563
1564 static int
1565 gen_struct_elt_for_reference (struct agent_expr *ax, struct axs_value *value,
1566                               struct type *type, char *fieldname)
1567 {
1568   struct type *t = type;
1569   int i;
1570
1571   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1572       && TYPE_CODE (t) != TYPE_CODE_UNION)
1573     internal_error (__FILE__, __LINE__,
1574                     _("non-aggregate type to gen_struct_elt_for_reference"));
1575
1576   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1577     {
1578       const char *t_field_name = TYPE_FIELD_NAME (t, i);
1579
1580       if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1581         {
1582           if (field_is_static (&TYPE_FIELD (t, i)))
1583             {
1584               gen_static_field (ax, value, t, i);
1585               if (value->optimized_out)
1586                 error (_("static field `%s' has been "
1587                          "optimized out, cannot use"),
1588                        fieldname);
1589               return 1;
1590             }
1591           if (TYPE_FIELD_PACKED (t, i))
1592             error (_("pointers to bitfield members not allowed"));
1593
1594           /* FIXME we need a way to do "want_address" equivalent */       
1595
1596           error (_("Cannot reference non-static field \"%s\""), fieldname);
1597         }
1598     }
1599
1600   /* FIXME add other scoped-reference cases here */
1601
1602   /* Do a last-ditch lookup.  */
1603   return gen_maybe_namespace_elt (ax, value, type, fieldname);
1604 }
1605
1606 /* C++: Return the member NAME of the namespace given by the type
1607    CURTYPE.  */
1608
1609 static int
1610 gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1611                    const struct type *curtype, char *name)
1612 {
1613   int found = gen_maybe_namespace_elt (ax, value, curtype, name);
1614
1615   if (!found)
1616     error (_("No symbol \"%s\" in namespace \"%s\"."), 
1617            name, TYPE_TAG_NAME (curtype));
1618
1619   return found;
1620 }
1621
1622 /* A helper function used by value_namespace_elt and
1623    value_struct_elt_for_reference.  It looks up NAME inside the
1624    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1625    is a class and NAME refers to a type in CURTYPE itself (as opposed
1626    to, say, some base class of CURTYPE).  */
1627
1628 static int
1629 gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1630                          const struct type *curtype, char *name)
1631 {
1632   const char *namespace_name = TYPE_TAG_NAME (curtype);
1633   struct block_symbol sym;
1634
1635   sym = cp_lookup_symbol_namespace (namespace_name, name,
1636                                     block_for_pc (ax->scope),
1637                                     VAR_DOMAIN);
1638
1639   if (sym.symbol == NULL)
1640     return 0;
1641
1642   gen_var_ref (ax, value, sym.symbol);
1643
1644   if (value->optimized_out)
1645     error (_("`%s' has been optimized out, cannot use"),
1646            SYMBOL_PRINT_NAME (sym.symbol));
1647
1648   return 1;
1649 }
1650
1651
1652 static int
1653 gen_aggregate_elt_ref (struct agent_expr *ax, struct axs_value *value,
1654                        struct type *type, char *field,
1655                        const char *operator_name,
1656                        const char *operand_name)
1657 {
1658   switch (TYPE_CODE (type))
1659     {
1660     case TYPE_CODE_STRUCT:
1661     case TYPE_CODE_UNION:
1662       return gen_struct_elt_for_reference (ax, value, type, field);
1663       break;
1664     case TYPE_CODE_NAMESPACE:
1665       return gen_namespace_elt (ax, value, type, field);
1666       break;
1667     default:
1668       internal_error (__FILE__, __LINE__,
1669                       _("non-aggregate type in gen_aggregate_elt_ref"));
1670     }
1671
1672   return 0;
1673 }
1674
1675 /* Generate code for GDB's magical `repeat' operator.
1676    LVALUE @ INT creates an array INT elements long, and whose elements
1677    have the same type as LVALUE, located in memory so that LVALUE is
1678    its first element.  For example, argv[0]@argc gives you the array
1679    of command-line arguments.
1680
1681    Unfortunately, because we have to know the types before we actually
1682    have a value for the expression, we can't implement this perfectly
1683    without changing the type system, having values that occupy two
1684    stack slots, doing weird things with sizeof, etc.  So we require
1685    the right operand to be a constant expression.  */
1686 static void
1687 gen_repeat (struct expression *exp, union exp_element **pc,
1688             struct agent_expr *ax, struct axs_value *value)
1689 {
1690   struct axs_value value1;
1691
1692   /* We don't want to turn this into an rvalue, so no conversions
1693      here.  */
1694   gen_expr (exp, pc, ax, &value1);
1695   if (value1.kind != axs_lvalue_memory)
1696     error (_("Left operand of `@' must be an object in memory."));
1697
1698   /* Evaluate the length; it had better be a constant.  */
1699   {
1700     struct value *v = const_expr (pc);
1701     int length;
1702
1703     if (!v)
1704       error (_("Right operand of `@' must be a "
1705                "constant, in agent expressions."));
1706     if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1707       error (_("Right operand of `@' must be an integer."));
1708     length = value_as_long (v);
1709     if (length <= 0)
1710       error (_("Right operand of `@' must be positive."));
1711
1712     /* The top of the stack is already the address of the object, so
1713        all we need to do is frob the type of the lvalue.  */
1714     {
1715       /* FIXME-type-allocation: need a way to free this type when we are
1716          done with it.  */
1717       struct type *array
1718         = lookup_array_range_type (value1.type, 0, length - 1);
1719
1720       value->kind = axs_lvalue_memory;
1721       value->type = array;
1722     }
1723   }
1724 }
1725
1726
1727 /* Emit code for the `sizeof' operator.
1728    *PC should point at the start of the operand expression; we advance it
1729    to the first instruction after the operand.  */
1730 static void
1731 gen_sizeof (struct expression *exp, union exp_element **pc,
1732             struct agent_expr *ax, struct axs_value *value,
1733             struct type *size_type)
1734 {
1735   /* We don't care about the value of the operand expression; we only
1736      care about its type.  However, in the current arrangement, the
1737      only way to find an expression's type is to generate code for it.
1738      So we generate code for the operand, and then throw it away,
1739      replacing it with code that simply pushes its size.  */
1740   int start = ax->len;
1741
1742   gen_expr (exp, pc, ax, value);
1743
1744   /* Throw away the code we just generated.  */
1745   ax->len = start;
1746
1747   ax_const_l (ax, TYPE_LENGTH (value->type));
1748   value->kind = axs_rvalue;
1749   value->type = size_type;
1750 }
1751 \f
1752
1753 /* Generating bytecode from GDB expressions: general recursive thingy  */
1754
1755 /* XXX: i18n */
1756 /* A gen_expr function written by a Gen-X'er guy.
1757    Append code for the subexpression of EXPR starting at *POS_P to AX.  */
1758 void
1759 gen_expr (struct expression *exp, union exp_element **pc,
1760           struct agent_expr *ax, struct axs_value *value)
1761 {
1762   /* Used to hold the descriptions of operand expressions.  */
1763   struct axs_value value1, value2, value3;
1764   enum exp_opcode op = (*pc)[0].opcode, op2;
1765   int if1, go1, if2, go2, end;
1766   struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
1767
1768   /* If we're looking at a constant expression, just push its value.  */
1769   {
1770     struct value *v = maybe_const_expr (pc);
1771
1772     if (v)
1773       {
1774         ax_const_l (ax, value_as_long (v));
1775         value->kind = axs_rvalue;
1776         value->type = check_typedef (value_type (v));
1777         return;
1778       }
1779   }
1780
1781   /* Otherwise, go ahead and generate code for it.  */
1782   switch (op)
1783     {
1784       /* Binary arithmetic operators.  */
1785     case BINOP_ADD:
1786     case BINOP_SUB:
1787     case BINOP_MUL:
1788     case BINOP_DIV:
1789     case BINOP_REM:
1790     case BINOP_LSH:
1791     case BINOP_RSH:
1792     case BINOP_SUBSCRIPT:
1793     case BINOP_BITWISE_AND:
1794     case BINOP_BITWISE_IOR:
1795     case BINOP_BITWISE_XOR:
1796     case BINOP_EQUAL:
1797     case BINOP_NOTEQUAL:
1798     case BINOP_LESS:
1799     case BINOP_GTR:
1800     case BINOP_LEQ:
1801     case BINOP_GEQ:
1802       (*pc)++;
1803       gen_expr (exp, pc, ax, &value1);
1804       gen_usual_unary (ax, &value1);
1805       gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1806       break;
1807
1808     case BINOP_LOGICAL_AND:
1809       (*pc)++;
1810       /* Generate the obvious sequence of tests and jumps.  */
1811       gen_expr (exp, pc, ax, &value1);
1812       gen_usual_unary (ax, &value1);
1813       if1 = ax_goto (ax, aop_if_goto);
1814       go1 = ax_goto (ax, aop_goto);
1815       ax_label (ax, if1, ax->len);
1816       gen_expr (exp, pc, ax, &value2);
1817       gen_usual_unary (ax, &value2);
1818       if2 = ax_goto (ax, aop_if_goto);
1819       go2 = ax_goto (ax, aop_goto);
1820       ax_label (ax, if2, ax->len);
1821       ax_const_l (ax, 1);
1822       end = ax_goto (ax, aop_goto);
1823       ax_label (ax, go1, ax->len);
1824       ax_label (ax, go2, ax->len);
1825       ax_const_l (ax, 0);
1826       ax_label (ax, end, ax->len);
1827       value->kind = axs_rvalue;
1828       value->type = int_type;
1829       break;
1830
1831     case BINOP_LOGICAL_OR:
1832       (*pc)++;
1833       /* Generate the obvious sequence of tests and jumps.  */
1834       gen_expr (exp, pc, ax, &value1);
1835       gen_usual_unary (ax, &value1);
1836       if1 = ax_goto (ax, aop_if_goto);
1837       gen_expr (exp, pc, ax, &value2);
1838       gen_usual_unary (ax, &value2);
1839       if2 = ax_goto (ax, aop_if_goto);
1840       ax_const_l (ax, 0);
1841       end = ax_goto (ax, aop_goto);
1842       ax_label (ax, if1, ax->len);
1843       ax_label (ax, if2, ax->len);
1844       ax_const_l (ax, 1);
1845       ax_label (ax, end, ax->len);
1846       value->kind = axs_rvalue;
1847       value->type = int_type;
1848       break;
1849
1850     case TERNOP_COND:
1851       (*pc)++;
1852       gen_expr (exp, pc, ax, &value1);
1853       gen_usual_unary (ax, &value1);
1854       /* For (A ? B : C), it's easiest to generate subexpression
1855          bytecodes in order, but if_goto jumps on true, so we invert
1856          the sense of A.  Then we can do B by dropping through, and
1857          jump to do C.  */
1858       gen_logical_not (ax, &value1, int_type);
1859       if1 = ax_goto (ax, aop_if_goto);
1860       gen_expr (exp, pc, ax, &value2);
1861       gen_usual_unary (ax, &value2);
1862       end = ax_goto (ax, aop_goto);
1863       ax_label (ax, if1, ax->len);
1864       gen_expr (exp, pc, ax, &value3);
1865       gen_usual_unary (ax, &value3);
1866       ax_label (ax, end, ax->len);
1867       /* This is arbitary - what if B and C are incompatible types? */
1868       value->type = value2.type;
1869       value->kind = value2.kind;
1870       break;
1871
1872     case BINOP_ASSIGN:
1873       (*pc)++;
1874       if ((*pc)[0].opcode == OP_INTERNALVAR)
1875         {
1876           char *name = internalvar_name ((*pc)[1].internalvar);
1877           struct trace_state_variable *tsv;
1878
1879           (*pc) += 3;
1880           gen_expr (exp, pc, ax, value);
1881           tsv = find_trace_state_variable (name);
1882           if (tsv)
1883             {
1884               ax_tsv (ax, aop_setv, tsv->number);
1885               if (ax->tracing)
1886                 ax_tsv (ax, aop_tracev, tsv->number);
1887             }
1888           else
1889             error (_("$%s is not a trace state variable, "
1890                      "may not assign to it"), name);
1891         }
1892       else
1893         error (_("May only assign to trace state variables"));
1894       break;
1895
1896     case BINOP_ASSIGN_MODIFY:
1897       (*pc)++;
1898       op2 = (*pc)[0].opcode;
1899       (*pc)++;
1900       (*pc)++;
1901       if ((*pc)[0].opcode == OP_INTERNALVAR)
1902         {
1903           char *name = internalvar_name ((*pc)[1].internalvar);
1904           struct trace_state_variable *tsv;
1905
1906           (*pc) += 3;
1907           tsv = find_trace_state_variable (name);
1908           if (tsv)
1909             {
1910               /* The tsv will be the left half of the binary operation.  */
1911               ax_tsv (ax, aop_getv, tsv->number);
1912               if (ax->tracing)
1913                 ax_tsv (ax, aop_tracev, tsv->number);
1914               /* Trace state variables are always 64-bit integers.  */
1915               value1.kind = axs_rvalue;
1916               value1.type = builtin_type (ax->gdbarch)->builtin_long_long;
1917               /* Now do right half of expression.  */
1918               gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1919               /* We have a result of the binary op, set the tsv.  */
1920               ax_tsv (ax, aop_setv, tsv->number);
1921               if (ax->tracing)
1922                 ax_tsv (ax, aop_tracev, tsv->number);
1923             }
1924           else
1925             error (_("$%s is not a trace state variable, "
1926                      "may not assign to it"), name);
1927         }
1928       else
1929         error (_("May only assign to trace state variables"));
1930       break;
1931
1932       /* Note that we need to be a little subtle about generating code
1933          for comma.  In C, we can do some optimizations here because
1934          we know the left operand is only being evaluated for effect.
1935          However, if the tracing kludge is in effect, then we always
1936          need to evaluate the left hand side fully, so that all the
1937          variables it mentions get traced.  */
1938     case BINOP_COMMA:
1939       (*pc)++;
1940       gen_expr (exp, pc, ax, &value1);
1941       /* Don't just dispose of the left operand.  We might be tracing,
1942          in which case we want to emit code to trace it if it's an
1943          lvalue.  */
1944       gen_traced_pop (ax, &value1);
1945       gen_expr (exp, pc, ax, value);
1946       /* It's the consumer's responsibility to trace the right operand.  */
1947       break;
1948
1949     case OP_LONG:               /* some integer constant */
1950       {
1951         struct type *type = (*pc)[1].type;
1952         LONGEST k = (*pc)[2].longconst;
1953
1954         (*pc) += 4;
1955         gen_int_literal (ax, value, k, type);
1956       }
1957       break;
1958
1959     case OP_VAR_VALUE:
1960       gen_var_ref (ax, value, (*pc)[2].symbol);
1961
1962       if (value->optimized_out)
1963         error (_("`%s' has been optimized out, cannot use"),
1964                SYMBOL_PRINT_NAME ((*pc)[2].symbol));
1965
1966       (*pc) += 4;
1967       break;
1968
1969     case OP_REGISTER:
1970       {
1971         const char *name = &(*pc)[2].string;
1972         int reg;
1973
1974         (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1975         reg = user_reg_map_name_to_regnum (ax->gdbarch, name, strlen (name));
1976         if (reg == -1)
1977           internal_error (__FILE__, __LINE__,
1978                           _("Register $%s not available"), name);
1979         /* No support for tracing user registers yet.  */
1980         if (reg >= gdbarch_num_regs (ax->gdbarch)
1981             + gdbarch_num_pseudo_regs (ax->gdbarch))
1982           error (_("'%s' is a user-register; "
1983                    "GDB cannot yet trace user-register contents."),
1984                  name);
1985         value->kind = axs_lvalue_register;
1986         value->u.reg = reg;
1987         value->type = register_type (ax->gdbarch, reg);
1988       }
1989       break;
1990
1991     case OP_INTERNALVAR:
1992       {
1993         struct internalvar *var = (*pc)[1].internalvar;
1994         const char *name = internalvar_name (var);
1995         struct trace_state_variable *tsv;
1996
1997         (*pc) += 3;
1998         tsv = find_trace_state_variable (name);
1999         if (tsv)
2000           {
2001             ax_tsv (ax, aop_getv, tsv->number);
2002             if (ax->tracing)
2003               ax_tsv (ax, aop_tracev, tsv->number);
2004             /* Trace state variables are always 64-bit integers.  */
2005             value->kind = axs_rvalue;
2006             value->type = builtin_type (ax->gdbarch)->builtin_long_long;
2007           }
2008         else if (! compile_internalvar_to_ax (var, ax, value))
2009           error (_("$%s is not a trace state variable; GDB agent "
2010                    "expressions cannot use convenience variables."), name);
2011       }
2012       break;
2013
2014       /* Weirdo operator: see comments for gen_repeat for details.  */
2015     case BINOP_REPEAT:
2016       /* Note that gen_repeat handles its own argument evaluation.  */
2017       (*pc)++;
2018       gen_repeat (exp, pc, ax, value);
2019       break;
2020
2021     case UNOP_CAST:
2022       {
2023         struct type *type = (*pc)[1].type;
2024
2025         (*pc) += 3;
2026         gen_expr (exp, pc, ax, value);
2027         gen_cast (ax, value, type);
2028       }
2029       break;
2030
2031     case UNOP_CAST_TYPE:
2032       {
2033         int offset;
2034         struct value *val;
2035         struct type *type;
2036
2037         ++*pc;
2038         offset = *pc - exp->elts;
2039         val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2040         type = value_type (val);
2041         *pc = &exp->elts[offset];
2042
2043         gen_expr (exp, pc, ax, value);
2044         gen_cast (ax, value, type);
2045       }
2046       break;
2047
2048     case UNOP_MEMVAL:
2049       {
2050         struct type *type = check_typedef ((*pc)[1].type);
2051
2052         (*pc) += 3;
2053         gen_expr (exp, pc, ax, value);
2054
2055         /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2056            already have the right value on the stack.  For
2057            axs_lvalue_register, we must convert.  */
2058         if (value->kind == axs_lvalue_register)
2059           require_rvalue (ax, value);
2060
2061         value->type = type;
2062         value->kind = axs_lvalue_memory;
2063       }
2064       break;
2065
2066     case UNOP_MEMVAL_TYPE:
2067       {
2068         int offset;
2069         struct value *val;
2070         struct type *type;
2071
2072         ++*pc;
2073         offset = *pc - exp->elts;
2074         val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2075         type = value_type (val);
2076         *pc = &exp->elts[offset];
2077
2078         gen_expr (exp, pc, ax, value);
2079
2080         /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2081            already have the right value on the stack.  For
2082            axs_lvalue_register, we must convert.  */
2083         if (value->kind == axs_lvalue_register)
2084           require_rvalue (ax, value);
2085
2086         value->type = type;
2087         value->kind = axs_lvalue_memory;
2088       }
2089       break;
2090
2091     case UNOP_PLUS:
2092       (*pc)++;
2093       /* + FOO is equivalent to 0 + FOO, which can be optimized.  */
2094       gen_expr (exp, pc, ax, value);
2095       gen_usual_unary (ax, value);
2096       break;
2097       
2098     case UNOP_NEG:
2099       (*pc)++;
2100       /* -FOO is equivalent to 0 - FOO.  */
2101       gen_int_literal (ax, &value1, 0,
2102                        builtin_type (ax->gdbarch)->builtin_int);
2103       gen_usual_unary (ax, &value1);    /* shouldn't do much */
2104       gen_expr (exp, pc, ax, &value2);
2105       gen_usual_unary (ax, &value2);
2106       gen_usual_arithmetic (ax, &value1, &value2);
2107       gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2108       break;
2109
2110     case UNOP_LOGICAL_NOT:
2111       (*pc)++;
2112       gen_expr (exp, pc, ax, value);
2113       gen_usual_unary (ax, value);
2114       gen_logical_not (ax, value, int_type);
2115       break;
2116
2117     case UNOP_COMPLEMENT:
2118       (*pc)++;
2119       gen_expr (exp, pc, ax, value);
2120       gen_usual_unary (ax, value);
2121       gen_integral_promotions (ax, value);
2122       gen_complement (ax, value);
2123       break;
2124
2125     case UNOP_IND:
2126       (*pc)++;
2127       gen_expr (exp, pc, ax, value);
2128       gen_usual_unary (ax, value);
2129       if (!pointer_type (value->type))
2130         error (_("Argument of unary `*' is not a pointer."));
2131       gen_deref (value);
2132       break;
2133
2134     case UNOP_ADDR:
2135       (*pc)++;
2136       gen_expr (exp, pc, ax, value);
2137       gen_address_of (value);
2138       break;
2139
2140     case UNOP_SIZEOF:
2141       (*pc)++;
2142       /* Notice that gen_sizeof handles its own operand, unlike most
2143          of the other unary operator functions.  This is because we
2144          have to throw away the code we generate.  */
2145       gen_sizeof (exp, pc, ax, value,
2146                   builtin_type (ax->gdbarch)->builtin_int);
2147       break;
2148
2149     case STRUCTOP_STRUCT:
2150     case STRUCTOP_PTR:
2151       {
2152         int length = (*pc)[1].longconst;
2153         char *name = &(*pc)[2].string;
2154
2155         (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2156         gen_expr (exp, pc, ax, value);
2157         if (op == STRUCTOP_STRUCT)
2158           gen_struct_ref (ax, value, name, ".", "structure or union");
2159         else if (op == STRUCTOP_PTR)
2160           gen_struct_ref (ax, value, name, "->",
2161                           "pointer to a structure or union");
2162         else
2163           /* If this `if' chain doesn't handle it, then the case list
2164              shouldn't mention it, and we shouldn't be here.  */
2165           internal_error (__FILE__, __LINE__,
2166                           _("gen_expr: unhandled struct case"));
2167       }
2168       break;
2169
2170     case OP_THIS:
2171       {
2172         struct symbol *sym, *func;
2173         const struct block *b;
2174         const struct language_defn *lang;
2175
2176         b = block_for_pc (ax->scope);
2177         func = block_linkage_function (b);
2178         lang = language_def (SYMBOL_LANGUAGE (func));
2179
2180         sym = lookup_language_this (lang, b).symbol;
2181         if (!sym)
2182           error (_("no `%s' found"), lang->la_name_of_this);
2183
2184         gen_var_ref (ax, value, sym);
2185
2186         if (value->optimized_out)
2187           error (_("`%s' has been optimized out, cannot use"),
2188                  SYMBOL_PRINT_NAME (sym));
2189
2190         (*pc) += 2;
2191       }
2192       break;
2193
2194     case OP_SCOPE:
2195       {
2196         struct type *type = (*pc)[1].type;
2197         int length = longest_to_int ((*pc)[2].longconst);
2198         char *name = &(*pc)[3].string;
2199         int found;
2200
2201         found = gen_aggregate_elt_ref (ax, value, type, name, "?", "??");
2202         if (!found)
2203           error (_("There is no field named %s"), name);
2204         (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2205       }
2206       break;
2207
2208     case OP_TYPE:
2209     case OP_TYPEOF:
2210     case OP_DECLTYPE:
2211       error (_("Attempt to use a type name as an expression."));
2212
2213     default:
2214       error (_("Unsupported operator %s (%d) in expression."),
2215              op_name (exp, op), op);
2216     }
2217 }
2218
2219 /* This handles the middle-to-right-side of code generation for binary
2220    expressions, which is shared between regular binary operations and
2221    assign-modify (+= and friends) expressions.  */
2222
2223 static void
2224 gen_expr_binop_rest (struct expression *exp,
2225                      enum exp_opcode op, union exp_element **pc,
2226                      struct agent_expr *ax, struct axs_value *value,
2227                      struct axs_value *value1, struct axs_value *value2)
2228 {
2229   struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
2230
2231   gen_expr (exp, pc, ax, value2);
2232   gen_usual_unary (ax, value2);
2233   gen_usual_arithmetic (ax, value1, value2);
2234   switch (op)
2235     {
2236     case BINOP_ADD:
2237       if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2238           && pointer_type (value2->type))
2239         {
2240           /* Swap the values and proceed normally.  */
2241           ax_simple (ax, aop_swap);
2242           gen_ptradd (ax, value, value2, value1);
2243         }
2244       else if (pointer_type (value1->type)
2245                && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2246         gen_ptradd (ax, value, value1, value2);
2247       else
2248         gen_binop (ax, value, value1, value2,
2249                    aop_add, aop_add, 1, "addition");
2250       break;
2251     case BINOP_SUB:
2252       if (pointer_type (value1->type)
2253           && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2254         gen_ptrsub (ax,value, value1, value2);
2255       else if (pointer_type (value1->type)
2256                && pointer_type (value2->type))
2257         /* FIXME --- result type should be ptrdiff_t */
2258         gen_ptrdiff (ax, value, value1, value2,
2259                      builtin_type (ax->gdbarch)->builtin_long);
2260       else
2261         gen_binop (ax, value, value1, value2,
2262                    aop_sub, aop_sub, 1, "subtraction");
2263       break;
2264     case BINOP_MUL:
2265       gen_binop (ax, value, value1, value2,
2266                  aop_mul, aop_mul, 1, "multiplication");
2267       break;
2268     case BINOP_DIV:
2269       gen_binop (ax, value, value1, value2,
2270                  aop_div_signed, aop_div_unsigned, 1, "division");
2271       break;
2272     case BINOP_REM:
2273       gen_binop (ax, value, value1, value2,
2274                  aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2275       break;
2276     case BINOP_LSH:
2277       gen_binop (ax, value, value1, value2,
2278                  aop_lsh, aop_lsh, 1, "left shift");
2279       break;
2280     case BINOP_RSH:
2281       gen_binop (ax, value, value1, value2,
2282                  aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2283       break;
2284     case BINOP_SUBSCRIPT:
2285       {
2286         struct type *type;
2287
2288         if (binop_types_user_defined_p (op, value1->type, value2->type))
2289           {
2290             error (_("cannot subscript requested type: "
2291                      "cannot call user defined functions"));
2292           }
2293         else
2294           {
2295             /* If the user attempts to subscript something that is not
2296                an array or pointer type (like a plain int variable for
2297                example), then report this as an error.  */
2298             type = check_typedef (value1->type);
2299             if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2300                 && TYPE_CODE (type) != TYPE_CODE_PTR)
2301               {
2302                 if (TYPE_NAME (type))
2303                   error (_("cannot subscript something of type `%s'"),
2304                          TYPE_NAME (type));
2305                 else
2306                   error (_("cannot subscript requested type"));
2307               }
2308           }
2309
2310         if (!is_integral_type (value2->type))
2311           error (_("Argument to arithmetic operation "
2312                    "not a number or boolean."));
2313
2314         gen_ptradd (ax, value, value1, value2);
2315         gen_deref (value);
2316         break;
2317       }
2318     case BINOP_BITWISE_AND:
2319       gen_binop (ax, value, value1, value2,
2320                  aop_bit_and, aop_bit_and, 0, "bitwise and");
2321       break;
2322
2323     case BINOP_BITWISE_IOR:
2324       gen_binop (ax, value, value1, value2,
2325                  aop_bit_or, aop_bit_or, 0, "bitwise or");
2326       break;
2327       
2328     case BINOP_BITWISE_XOR:
2329       gen_binop (ax, value, value1, value2,
2330                  aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2331       break;
2332
2333     case BINOP_EQUAL:
2334       gen_equal (ax, value, value1, value2, int_type);
2335       break;
2336
2337     case BINOP_NOTEQUAL:
2338       gen_equal (ax, value, value1, value2, int_type);
2339       gen_logical_not (ax, value, int_type);
2340       break;
2341
2342     case BINOP_LESS:
2343       gen_less (ax, value, value1, value2, int_type);
2344       break;
2345
2346     case BINOP_GTR:
2347       ax_simple (ax, aop_swap);
2348       gen_less (ax, value, value1, value2, int_type);
2349       break;
2350
2351     case BINOP_LEQ:
2352       ax_simple (ax, aop_swap);
2353       gen_less (ax, value, value1, value2, int_type);
2354       gen_logical_not (ax, value, int_type);
2355       break;
2356
2357     case BINOP_GEQ:
2358       gen_less (ax, value, value1, value2, int_type);
2359       gen_logical_not (ax, value, int_type);
2360       break;
2361
2362     default:
2363       /* We should only list operators in the outer case statement
2364          that we actually handle in the inner case statement.  */
2365       internal_error (__FILE__, __LINE__,
2366                       _("gen_expr: op case sets don't match"));
2367     }
2368 }
2369 \f
2370
2371 /* Given a single variable and a scope, generate bytecodes to trace
2372    its value.  This is for use in situations where we have only a
2373    variable's name, and no parsed expression; for instance, when the
2374    name comes from a list of local variables of a function.  */
2375
2376 agent_expr_up
2377 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2378                    struct symbol *var, int trace_string)
2379 {
2380   agent_expr_up ax (new agent_expr (gdbarch, scope));
2381   struct axs_value value;
2382
2383   ax->tracing = 1;
2384   ax->trace_string = trace_string;
2385   gen_var_ref (ax.get (), &value, var);
2386
2387   /* If there is no actual variable to trace, flag it by returning
2388      an empty agent expression.  */
2389   if (value.optimized_out)
2390     return agent_expr_up ();
2391
2392   /* Make sure we record the final object, and get rid of it.  */
2393   gen_traced_pop (ax.get (), &value);
2394
2395   /* Oh, and terminate.  */
2396   ax_simple (ax.get (), aop_end);
2397
2398   return ax;
2399 }
2400
2401 /* Generating bytecode from GDB expressions: driver */
2402
2403 /* Given a GDB expression EXPR, return bytecode to trace its value.
2404    The result will use the `trace' and `trace_quick' bytecodes to
2405    record the value of all memory touched by the expression.  The
2406    caller can then use the ax_reqs function to discover which
2407    registers it relies upon.  */
2408
2409 agent_expr_up
2410 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
2411                     int trace_string)
2412 {
2413   agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2414   union exp_element *pc;
2415   struct axs_value value;
2416
2417   pc = expr->elts;
2418   ax->tracing = 1;
2419   ax->trace_string = trace_string;
2420   value.optimized_out = 0;
2421   gen_expr (expr, &pc, ax.get (), &value);
2422
2423   /* Make sure we record the final object, and get rid of it.  */
2424   gen_traced_pop (ax.get (), &value);
2425
2426   /* Oh, and terminate.  */
2427   ax_simple (ax.get (), aop_end);
2428
2429   return ax;
2430 }
2431
2432 /* Given a GDB expression EXPR, return a bytecode sequence that will
2433    evaluate and return a result.  The bytecodes will do a direct
2434    evaluation, using the current data on the target, rather than
2435    recording blocks of memory and registers for later use, as
2436    gen_trace_for_expr does.  The generated bytecode sequence leaves
2437    the result of expression evaluation on the top of the stack.  */
2438
2439 agent_expr_up
2440 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2441 {
2442   agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2443   union exp_element *pc;
2444   struct axs_value value;
2445
2446   pc = expr->elts;
2447   ax->tracing = 0;
2448   value.optimized_out = 0;
2449   gen_expr (expr, &pc, ax.get (), &value);
2450
2451   require_rvalue (ax.get (), &value);
2452
2453   /* Oh, and terminate.  */
2454   ax_simple (ax.get (), aop_end);
2455
2456   return ax;
2457 }
2458
2459 agent_expr_up
2460 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
2461                               int trace_string)
2462 {
2463   agent_expr_up ax (new agent_expr (gdbarch, scope));
2464   struct axs_value value;
2465
2466   ax->tracing = 1;
2467   ax->trace_string = trace_string;
2468
2469   gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope);
2470
2471   /* Make sure we record the final object, and get rid of it.  */
2472   gen_traced_pop (ax.get (), &value);
2473
2474   /* Oh, and terminate.  */
2475   ax_simple (ax.get (), aop_end);
2476
2477   return ax;
2478 }
2479
2480 /* Given a collection of printf-style arguments, generate code to
2481    evaluate the arguments and pass everything to a special
2482    bytecode.  */
2483
2484 agent_expr_up
2485 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2486             CORE_ADDR function, LONGEST channel,
2487             const char *format, int fmtlen,
2488             struct format_piece *frags,
2489             int nargs, struct expression **exprs)
2490 {
2491   agent_expr_up ax (new agent_expr (gdbarch, scope));
2492   union exp_element *pc;
2493   struct axs_value value;
2494   int tem;
2495
2496   /* We're computing values, not doing side effects.  */
2497   ax->tracing = 0;
2498
2499   /* Evaluate and push the args on the stack in reverse order,
2500      for simplicity of collecting them on the target side.  */
2501   for (tem = nargs - 1; tem >= 0; --tem)
2502     {
2503       pc = exprs[tem]->elts;
2504       value.optimized_out = 0;
2505       gen_expr (exprs[tem], &pc, ax.get (), &value);
2506       require_rvalue (ax.get (), &value);
2507     }
2508
2509   /* Push function and channel.  */
2510   ax_const_l (ax.get (), channel);
2511   ax_const_l (ax.get (), function);
2512
2513   /* Issue the printf bytecode proper.  */
2514   ax_simple (ax.get (), aop_printf);
2515   ax_raw_byte (ax.get (), nargs);
2516   ax_string (ax.get (), format, fmtlen);
2517
2518   /* And terminate.  */
2519   ax_simple (ax.get (), aop_end);
2520
2521   return ax;
2522 }
2523
2524 static void
2525 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
2526 {
2527   const char *arg;
2528   int trace_string = 0;
2529
2530   if (!eval)
2531     {
2532       if (*exp == '/')
2533         exp = decode_agent_options (exp, &trace_string);
2534     }
2535
2536   agent_expr_up agent;
2537
2538   arg = exp;
2539   if (!eval && strcmp (arg, "$_ret") == 0)
2540     {
2541       agent = gen_trace_for_return_address (pc, get_current_arch (),
2542                                             trace_string);
2543     }
2544   else
2545     {
2546       expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
2547
2548       if (eval)
2549         {
2550           gdb_assert (trace_string == 0);
2551           agent = gen_eval_for_expr (pc, expr.get ());
2552         }
2553       else
2554         agent = gen_trace_for_expr (pc, expr.get (), trace_string);
2555     }
2556
2557   ax_reqs (agent.get ());
2558   ax_print (gdb_stdout, agent.get ());
2559
2560   /* It would be nice to call ax_reqs here to gather some general info
2561      about the expression, and then print out the result.  */
2562
2563   dont_repeat ();
2564 }
2565
2566 static void
2567 agent_command_1 (char *exp, int eval)
2568 {
2569   /* We don't deal with overlay debugging at the moment.  We need to
2570      think more carefully about this.  If you copy this code into
2571      another command, change the error message; the user shouldn't
2572      have to know anything about agent expressions.  */
2573   if (overlay_debugging)
2574     error (_("GDB can't do agent expression translation with overlays."));
2575
2576   if (exp == 0)
2577     error_no_arg (_("expression to translate"));
2578
2579   if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2580     {
2581       struct linespec_result canonical;
2582       int ix;
2583       struct linespec_sals *iter;
2584
2585       exp = skip_spaces (exp);
2586
2587       event_location_up location = new_linespec_location (&exp);
2588       decode_line_full (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
2589                         (struct symtab *) NULL, 0, &canonical,
2590                         NULL, NULL);
2591       exp = skip_spaces (exp);
2592       if (exp[0] == ',')
2593         {
2594           exp++;
2595           exp = skip_spaces (exp);
2596         }
2597       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
2598         {
2599           int i;
2600
2601           for (i = 0; i < iter->sals.nelts; i++)
2602             agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
2603         }
2604     }
2605   else
2606     agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
2607
2608   dont_repeat ();
2609 }
2610
2611 static void
2612 agent_command (char *exp, int from_tty)
2613 {
2614   agent_command_1 (exp, 0);
2615 }
2616
2617 /* Parse the given expression, compile it into an agent expression
2618    that does direct evaluation, and display the resulting
2619    expression.  */
2620
2621 static void
2622 agent_eval_command (char *exp, int from_tty)
2623 {
2624   agent_command_1 (exp, 1);
2625 }
2626
2627 /* Parse the given expression, compile it into an agent expression
2628    that does a printf, and display the resulting expression.  */
2629
2630 static void
2631 maint_agent_printf_command (char *exp, int from_tty)
2632 {
2633   struct cleanup *old_chain = 0;
2634   struct expression *argvec[100];
2635   struct frame_info *fi = get_current_frame (); /* need current scope */
2636   const char *cmdrest;
2637   const char *format_start, *format_end;
2638   struct format_piece *fpieces;
2639   int nargs;
2640
2641   /* We don't deal with overlay debugging at the moment.  We need to
2642      think more carefully about this.  If you copy this code into
2643      another command, change the error message; the user shouldn't
2644      have to know anything about agent expressions.  */
2645   if (overlay_debugging)
2646     error (_("GDB can't do agent expression translation with overlays."));
2647
2648   if (exp == 0)
2649     error_no_arg (_("expression to translate"));
2650
2651   cmdrest = exp;
2652
2653   cmdrest = skip_spaces_const (cmdrest);
2654
2655   if (*cmdrest++ != '"')
2656     error (_("Must start with a format string."));
2657
2658   format_start = cmdrest;
2659
2660   fpieces = parse_format_string (&cmdrest);
2661
2662   old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2663
2664   format_end = cmdrest;
2665
2666   if (*cmdrest++ != '"')
2667     error (_("Bad format string, non-terminated '\"'."));
2668   
2669   cmdrest = skip_spaces_const (cmdrest);
2670
2671   if (*cmdrest != ',' && *cmdrest != 0)
2672     error (_("Invalid argument syntax"));
2673
2674   if (*cmdrest == ',')
2675     cmdrest++;
2676   cmdrest = skip_spaces_const (cmdrest);
2677
2678   nargs = 0;
2679   while (*cmdrest != '\0')
2680     {
2681       const char *cmd1;
2682
2683       cmd1 = cmdrest;
2684       expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2685       argvec[nargs] = expr.release ();
2686       ++nargs;
2687       cmdrest = cmd1;
2688       if (*cmdrest == ',')
2689         ++cmdrest;
2690       /* else complain? */
2691     }
2692
2693
2694   agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (),
2695                                     0, 0,
2696                                     format_start, format_end - format_start,
2697                                     fpieces, nargs, argvec);
2698   ax_reqs (agent.get ());
2699   ax_print (gdb_stdout, agent.get ());
2700
2701   /* It would be nice to call ax_reqs here to gather some general info
2702      about the expression, and then print out the result.  */
2703
2704   do_cleanups (old_chain);
2705   dont_repeat ();
2706 }
2707 \f
2708
2709 /* Initialization code.  */
2710
2711 void _initialize_ax_gdb (void);
2712 void
2713 _initialize_ax_gdb (void)
2714 {
2715   add_cmd ("agent", class_maintenance, agent_command,
2716            _("\
2717 Translate an expression into remote agent bytecode for tracing.\n\
2718 Usage: maint agent [-at location,] EXPRESSION\n\
2719 If -at is given, generate remote agent bytecode for this location.\n\
2720 If not, generate remote agent bytecode for current frame pc address."),
2721            &maintenancelist);
2722
2723   add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2724            _("\
2725 Translate an expression into remote agent bytecode for evaluation.\n\
2726 Usage: maint agent-eval [-at location,] EXPRESSION\n\
2727 If -at is given, generate remote agent bytecode for this location.\n\
2728 If not, generate remote agent bytecode for current frame pc address."),
2729            &maintenancelist);
2730
2731   add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2732            _("Translate an expression into remote "
2733              "agent bytecode for evaluation and display the bytecodes."),
2734            &maintenancelist);
2735 }