ax-gdb: Remove more unused arguments
[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 {
1656   switch (TYPE_CODE (type))
1657     {
1658     case TYPE_CODE_STRUCT:
1659     case TYPE_CODE_UNION:
1660       return gen_struct_elt_for_reference (ax, value, type, field);
1661       break;
1662     case TYPE_CODE_NAMESPACE:
1663       return gen_namespace_elt (ax, value, type, field);
1664       break;
1665     default:
1666       internal_error (__FILE__, __LINE__,
1667                       _("non-aggregate type in gen_aggregate_elt_ref"));
1668     }
1669
1670   return 0;
1671 }
1672
1673 /* Generate code for GDB's magical `repeat' operator.
1674    LVALUE @ INT creates an array INT elements long, and whose elements
1675    have the same type as LVALUE, located in memory so that LVALUE is
1676    its first element.  For example, argv[0]@argc gives you the array
1677    of command-line arguments.
1678
1679    Unfortunately, because we have to know the types before we actually
1680    have a value for the expression, we can't implement this perfectly
1681    without changing the type system, having values that occupy two
1682    stack slots, doing weird things with sizeof, etc.  So we require
1683    the right operand to be a constant expression.  */
1684 static void
1685 gen_repeat (struct expression *exp, union exp_element **pc,
1686             struct agent_expr *ax, struct axs_value *value)
1687 {
1688   struct axs_value value1;
1689
1690   /* We don't want to turn this into an rvalue, so no conversions
1691      here.  */
1692   gen_expr (exp, pc, ax, &value1);
1693   if (value1.kind != axs_lvalue_memory)
1694     error (_("Left operand of `@' must be an object in memory."));
1695
1696   /* Evaluate the length; it had better be a constant.  */
1697   {
1698     struct value *v = const_expr (pc);
1699     int length;
1700
1701     if (!v)
1702       error (_("Right operand of `@' must be a "
1703                "constant, in agent expressions."));
1704     if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1705       error (_("Right operand of `@' must be an integer."));
1706     length = value_as_long (v);
1707     if (length <= 0)
1708       error (_("Right operand of `@' must be positive."));
1709
1710     /* The top of the stack is already the address of the object, so
1711        all we need to do is frob the type of the lvalue.  */
1712     {
1713       /* FIXME-type-allocation: need a way to free this type when we are
1714          done with it.  */
1715       struct type *array
1716         = lookup_array_range_type (value1.type, 0, length - 1);
1717
1718       value->kind = axs_lvalue_memory;
1719       value->type = array;
1720     }
1721   }
1722 }
1723
1724
1725 /* Emit code for the `sizeof' operator.
1726    *PC should point at the start of the operand expression; we advance it
1727    to the first instruction after the operand.  */
1728 static void
1729 gen_sizeof (struct expression *exp, union exp_element **pc,
1730             struct agent_expr *ax, struct axs_value *value,
1731             struct type *size_type)
1732 {
1733   /* We don't care about the value of the operand expression; we only
1734      care about its type.  However, in the current arrangement, the
1735      only way to find an expression's type is to generate code for it.
1736      So we generate code for the operand, and then throw it away,
1737      replacing it with code that simply pushes its size.  */
1738   int start = ax->len;
1739
1740   gen_expr (exp, pc, ax, value);
1741
1742   /* Throw away the code we just generated.  */
1743   ax->len = start;
1744
1745   ax_const_l (ax, TYPE_LENGTH (value->type));
1746   value->kind = axs_rvalue;
1747   value->type = size_type;
1748 }
1749 \f
1750
1751 /* Generating bytecode from GDB expressions: general recursive thingy  */
1752
1753 /* XXX: i18n */
1754 /* A gen_expr function written by a Gen-X'er guy.
1755    Append code for the subexpression of EXPR starting at *POS_P to AX.  */
1756 void
1757 gen_expr (struct expression *exp, union exp_element **pc,
1758           struct agent_expr *ax, struct axs_value *value)
1759 {
1760   /* Used to hold the descriptions of operand expressions.  */
1761   struct axs_value value1, value2, value3;
1762   enum exp_opcode op = (*pc)[0].opcode, op2;
1763   int if1, go1, if2, go2, end;
1764   struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
1765
1766   /* If we're looking at a constant expression, just push its value.  */
1767   {
1768     struct value *v = maybe_const_expr (pc);
1769
1770     if (v)
1771       {
1772         ax_const_l (ax, value_as_long (v));
1773         value->kind = axs_rvalue;
1774         value->type = check_typedef (value_type (v));
1775         return;
1776       }
1777   }
1778
1779   /* Otherwise, go ahead and generate code for it.  */
1780   switch (op)
1781     {
1782       /* Binary arithmetic operators.  */
1783     case BINOP_ADD:
1784     case BINOP_SUB:
1785     case BINOP_MUL:
1786     case BINOP_DIV:
1787     case BINOP_REM:
1788     case BINOP_LSH:
1789     case BINOP_RSH:
1790     case BINOP_SUBSCRIPT:
1791     case BINOP_BITWISE_AND:
1792     case BINOP_BITWISE_IOR:
1793     case BINOP_BITWISE_XOR:
1794     case BINOP_EQUAL:
1795     case BINOP_NOTEQUAL:
1796     case BINOP_LESS:
1797     case BINOP_GTR:
1798     case BINOP_LEQ:
1799     case BINOP_GEQ:
1800       (*pc)++;
1801       gen_expr (exp, pc, ax, &value1);
1802       gen_usual_unary (ax, &value1);
1803       gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1804       break;
1805
1806     case BINOP_LOGICAL_AND:
1807       (*pc)++;
1808       /* Generate the obvious sequence of tests and jumps.  */
1809       gen_expr (exp, pc, ax, &value1);
1810       gen_usual_unary (ax, &value1);
1811       if1 = ax_goto (ax, aop_if_goto);
1812       go1 = ax_goto (ax, aop_goto);
1813       ax_label (ax, if1, ax->len);
1814       gen_expr (exp, pc, ax, &value2);
1815       gen_usual_unary (ax, &value2);
1816       if2 = ax_goto (ax, aop_if_goto);
1817       go2 = ax_goto (ax, aop_goto);
1818       ax_label (ax, if2, ax->len);
1819       ax_const_l (ax, 1);
1820       end = ax_goto (ax, aop_goto);
1821       ax_label (ax, go1, ax->len);
1822       ax_label (ax, go2, ax->len);
1823       ax_const_l (ax, 0);
1824       ax_label (ax, end, ax->len);
1825       value->kind = axs_rvalue;
1826       value->type = int_type;
1827       break;
1828
1829     case BINOP_LOGICAL_OR:
1830       (*pc)++;
1831       /* Generate the obvious sequence of tests and jumps.  */
1832       gen_expr (exp, pc, ax, &value1);
1833       gen_usual_unary (ax, &value1);
1834       if1 = ax_goto (ax, aop_if_goto);
1835       gen_expr (exp, pc, ax, &value2);
1836       gen_usual_unary (ax, &value2);
1837       if2 = ax_goto (ax, aop_if_goto);
1838       ax_const_l (ax, 0);
1839       end = ax_goto (ax, aop_goto);
1840       ax_label (ax, if1, ax->len);
1841       ax_label (ax, if2, ax->len);
1842       ax_const_l (ax, 1);
1843       ax_label (ax, end, ax->len);
1844       value->kind = axs_rvalue;
1845       value->type = int_type;
1846       break;
1847
1848     case TERNOP_COND:
1849       (*pc)++;
1850       gen_expr (exp, pc, ax, &value1);
1851       gen_usual_unary (ax, &value1);
1852       /* For (A ? B : C), it's easiest to generate subexpression
1853          bytecodes in order, but if_goto jumps on true, so we invert
1854          the sense of A.  Then we can do B by dropping through, and
1855          jump to do C.  */
1856       gen_logical_not (ax, &value1, int_type);
1857       if1 = ax_goto (ax, aop_if_goto);
1858       gen_expr (exp, pc, ax, &value2);
1859       gen_usual_unary (ax, &value2);
1860       end = ax_goto (ax, aop_goto);
1861       ax_label (ax, if1, ax->len);
1862       gen_expr (exp, pc, ax, &value3);
1863       gen_usual_unary (ax, &value3);
1864       ax_label (ax, end, ax->len);
1865       /* This is arbitary - what if B and C are incompatible types? */
1866       value->type = value2.type;
1867       value->kind = value2.kind;
1868       break;
1869
1870     case BINOP_ASSIGN:
1871       (*pc)++;
1872       if ((*pc)[0].opcode == OP_INTERNALVAR)
1873         {
1874           char *name = internalvar_name ((*pc)[1].internalvar);
1875           struct trace_state_variable *tsv;
1876
1877           (*pc) += 3;
1878           gen_expr (exp, pc, ax, value);
1879           tsv = find_trace_state_variable (name);
1880           if (tsv)
1881             {
1882               ax_tsv (ax, aop_setv, tsv->number);
1883               if (ax->tracing)
1884                 ax_tsv (ax, aop_tracev, tsv->number);
1885             }
1886           else
1887             error (_("$%s is not a trace state variable, "
1888                      "may not assign to it"), name);
1889         }
1890       else
1891         error (_("May only assign to trace state variables"));
1892       break;
1893
1894     case BINOP_ASSIGN_MODIFY:
1895       (*pc)++;
1896       op2 = (*pc)[0].opcode;
1897       (*pc)++;
1898       (*pc)++;
1899       if ((*pc)[0].opcode == OP_INTERNALVAR)
1900         {
1901           char *name = internalvar_name ((*pc)[1].internalvar);
1902           struct trace_state_variable *tsv;
1903
1904           (*pc) += 3;
1905           tsv = find_trace_state_variable (name);
1906           if (tsv)
1907             {
1908               /* The tsv will be the left half of the binary operation.  */
1909               ax_tsv (ax, aop_getv, tsv->number);
1910               if (ax->tracing)
1911                 ax_tsv (ax, aop_tracev, tsv->number);
1912               /* Trace state variables are always 64-bit integers.  */
1913               value1.kind = axs_rvalue;
1914               value1.type = builtin_type (ax->gdbarch)->builtin_long_long;
1915               /* Now do right half of expression.  */
1916               gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1917               /* We have a result of the binary op, set the tsv.  */
1918               ax_tsv (ax, aop_setv, tsv->number);
1919               if (ax->tracing)
1920                 ax_tsv (ax, aop_tracev, tsv->number);
1921             }
1922           else
1923             error (_("$%s is not a trace state variable, "
1924                      "may not assign to it"), name);
1925         }
1926       else
1927         error (_("May only assign to trace state variables"));
1928       break;
1929
1930       /* Note that we need to be a little subtle about generating code
1931          for comma.  In C, we can do some optimizations here because
1932          we know the left operand is only being evaluated for effect.
1933          However, if the tracing kludge is in effect, then we always
1934          need to evaluate the left hand side fully, so that all the
1935          variables it mentions get traced.  */
1936     case BINOP_COMMA:
1937       (*pc)++;
1938       gen_expr (exp, pc, ax, &value1);
1939       /* Don't just dispose of the left operand.  We might be tracing,
1940          in which case we want to emit code to trace it if it's an
1941          lvalue.  */
1942       gen_traced_pop (ax, &value1);
1943       gen_expr (exp, pc, ax, value);
1944       /* It's the consumer's responsibility to trace the right operand.  */
1945       break;
1946
1947     case OP_LONG:               /* some integer constant */
1948       {
1949         struct type *type = (*pc)[1].type;
1950         LONGEST k = (*pc)[2].longconst;
1951
1952         (*pc) += 4;
1953         gen_int_literal (ax, value, k, type);
1954       }
1955       break;
1956
1957     case OP_VAR_VALUE:
1958       gen_var_ref (ax, value, (*pc)[2].symbol);
1959
1960       if (value->optimized_out)
1961         error (_("`%s' has been optimized out, cannot use"),
1962                SYMBOL_PRINT_NAME ((*pc)[2].symbol));
1963
1964       (*pc) += 4;
1965       break;
1966
1967     case OP_REGISTER:
1968       {
1969         const char *name = &(*pc)[2].string;
1970         int reg;
1971
1972         (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1973         reg = user_reg_map_name_to_regnum (ax->gdbarch, name, strlen (name));
1974         if (reg == -1)
1975           internal_error (__FILE__, __LINE__,
1976                           _("Register $%s not available"), name);
1977         /* No support for tracing user registers yet.  */
1978         if (reg >= gdbarch_num_regs (ax->gdbarch)
1979             + gdbarch_num_pseudo_regs (ax->gdbarch))
1980           error (_("'%s' is a user-register; "
1981                    "GDB cannot yet trace user-register contents."),
1982                  name);
1983         value->kind = axs_lvalue_register;
1984         value->u.reg = reg;
1985         value->type = register_type (ax->gdbarch, reg);
1986       }
1987       break;
1988
1989     case OP_INTERNALVAR:
1990       {
1991         struct internalvar *var = (*pc)[1].internalvar;
1992         const char *name = internalvar_name (var);
1993         struct trace_state_variable *tsv;
1994
1995         (*pc) += 3;
1996         tsv = find_trace_state_variable (name);
1997         if (tsv)
1998           {
1999             ax_tsv (ax, aop_getv, tsv->number);
2000             if (ax->tracing)
2001               ax_tsv (ax, aop_tracev, tsv->number);
2002             /* Trace state variables are always 64-bit integers.  */
2003             value->kind = axs_rvalue;
2004             value->type = builtin_type (ax->gdbarch)->builtin_long_long;
2005           }
2006         else if (! compile_internalvar_to_ax (var, ax, value))
2007           error (_("$%s is not a trace state variable; GDB agent "
2008                    "expressions cannot use convenience variables."), name);
2009       }
2010       break;
2011
2012       /* Weirdo operator: see comments for gen_repeat for details.  */
2013     case BINOP_REPEAT:
2014       /* Note that gen_repeat handles its own argument evaluation.  */
2015       (*pc)++;
2016       gen_repeat (exp, pc, ax, value);
2017       break;
2018
2019     case UNOP_CAST:
2020       {
2021         struct type *type = (*pc)[1].type;
2022
2023         (*pc) += 3;
2024         gen_expr (exp, pc, ax, value);
2025         gen_cast (ax, value, type);
2026       }
2027       break;
2028
2029     case UNOP_CAST_TYPE:
2030       {
2031         int offset;
2032         struct value *val;
2033         struct type *type;
2034
2035         ++*pc;
2036         offset = *pc - exp->elts;
2037         val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2038         type = value_type (val);
2039         *pc = &exp->elts[offset];
2040
2041         gen_expr (exp, pc, ax, value);
2042         gen_cast (ax, value, type);
2043       }
2044       break;
2045
2046     case UNOP_MEMVAL:
2047       {
2048         struct type *type = check_typedef ((*pc)[1].type);
2049
2050         (*pc) += 3;
2051         gen_expr (exp, pc, ax, value);
2052
2053         /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2054            already have the right value on the stack.  For
2055            axs_lvalue_register, we must convert.  */
2056         if (value->kind == axs_lvalue_register)
2057           require_rvalue (ax, value);
2058
2059         value->type = type;
2060         value->kind = axs_lvalue_memory;
2061       }
2062       break;
2063
2064     case UNOP_MEMVAL_TYPE:
2065       {
2066         int offset;
2067         struct value *val;
2068         struct type *type;
2069
2070         ++*pc;
2071         offset = *pc - exp->elts;
2072         val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2073         type = value_type (val);
2074         *pc = &exp->elts[offset];
2075
2076         gen_expr (exp, pc, ax, value);
2077
2078         /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2079            already have the right value on the stack.  For
2080            axs_lvalue_register, we must convert.  */
2081         if (value->kind == axs_lvalue_register)
2082           require_rvalue (ax, value);
2083
2084         value->type = type;
2085         value->kind = axs_lvalue_memory;
2086       }
2087       break;
2088
2089     case UNOP_PLUS:
2090       (*pc)++;
2091       /* + FOO is equivalent to 0 + FOO, which can be optimized.  */
2092       gen_expr (exp, pc, ax, value);
2093       gen_usual_unary (ax, value);
2094       break;
2095       
2096     case UNOP_NEG:
2097       (*pc)++;
2098       /* -FOO is equivalent to 0 - FOO.  */
2099       gen_int_literal (ax, &value1, 0,
2100                        builtin_type (ax->gdbarch)->builtin_int);
2101       gen_usual_unary (ax, &value1);    /* shouldn't do much */
2102       gen_expr (exp, pc, ax, &value2);
2103       gen_usual_unary (ax, &value2);
2104       gen_usual_arithmetic (ax, &value1, &value2);
2105       gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2106       break;
2107
2108     case UNOP_LOGICAL_NOT:
2109       (*pc)++;
2110       gen_expr (exp, pc, ax, value);
2111       gen_usual_unary (ax, value);
2112       gen_logical_not (ax, value, int_type);
2113       break;
2114
2115     case UNOP_COMPLEMENT:
2116       (*pc)++;
2117       gen_expr (exp, pc, ax, value);
2118       gen_usual_unary (ax, value);
2119       gen_integral_promotions (ax, value);
2120       gen_complement (ax, value);
2121       break;
2122
2123     case UNOP_IND:
2124       (*pc)++;
2125       gen_expr (exp, pc, ax, value);
2126       gen_usual_unary (ax, value);
2127       if (!pointer_type (value->type))
2128         error (_("Argument of unary `*' is not a pointer."));
2129       gen_deref (value);
2130       break;
2131
2132     case UNOP_ADDR:
2133       (*pc)++;
2134       gen_expr (exp, pc, ax, value);
2135       gen_address_of (value);
2136       break;
2137
2138     case UNOP_SIZEOF:
2139       (*pc)++;
2140       /* Notice that gen_sizeof handles its own operand, unlike most
2141          of the other unary operator functions.  This is because we
2142          have to throw away the code we generate.  */
2143       gen_sizeof (exp, pc, ax, value,
2144                   builtin_type (ax->gdbarch)->builtin_int);
2145       break;
2146
2147     case STRUCTOP_STRUCT:
2148     case STRUCTOP_PTR:
2149       {
2150         int length = (*pc)[1].longconst;
2151         char *name = &(*pc)[2].string;
2152
2153         (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2154         gen_expr (exp, pc, ax, value);
2155         if (op == STRUCTOP_STRUCT)
2156           gen_struct_ref (ax, value, name, ".", "structure or union");
2157         else if (op == STRUCTOP_PTR)
2158           gen_struct_ref (ax, value, name, "->",
2159                           "pointer to a structure or union");
2160         else
2161           /* If this `if' chain doesn't handle it, then the case list
2162              shouldn't mention it, and we shouldn't be here.  */
2163           internal_error (__FILE__, __LINE__,
2164                           _("gen_expr: unhandled struct case"));
2165       }
2166       break;
2167
2168     case OP_THIS:
2169       {
2170         struct symbol *sym, *func;
2171         const struct block *b;
2172         const struct language_defn *lang;
2173
2174         b = block_for_pc (ax->scope);
2175         func = block_linkage_function (b);
2176         lang = language_def (SYMBOL_LANGUAGE (func));
2177
2178         sym = lookup_language_this (lang, b).symbol;
2179         if (!sym)
2180           error (_("no `%s' found"), lang->la_name_of_this);
2181
2182         gen_var_ref (ax, value, sym);
2183
2184         if (value->optimized_out)
2185           error (_("`%s' has been optimized out, cannot use"),
2186                  SYMBOL_PRINT_NAME (sym));
2187
2188         (*pc) += 2;
2189       }
2190       break;
2191
2192     case OP_SCOPE:
2193       {
2194         struct type *type = (*pc)[1].type;
2195         int length = longest_to_int ((*pc)[2].longconst);
2196         char *name = &(*pc)[3].string;
2197         int found;
2198
2199         found = gen_aggregate_elt_ref (ax, value, type, name);
2200         if (!found)
2201           error (_("There is no field named %s"), name);
2202         (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2203       }
2204       break;
2205
2206     case OP_TYPE:
2207     case OP_TYPEOF:
2208     case OP_DECLTYPE:
2209       error (_("Attempt to use a type name as an expression."));
2210
2211     default:
2212       error (_("Unsupported operator %s (%d) in expression."),
2213              op_name (exp, op), op);
2214     }
2215 }
2216
2217 /* This handles the middle-to-right-side of code generation for binary
2218    expressions, which is shared between regular binary operations and
2219    assign-modify (+= and friends) expressions.  */
2220
2221 static void
2222 gen_expr_binop_rest (struct expression *exp,
2223                      enum exp_opcode op, union exp_element **pc,
2224                      struct agent_expr *ax, struct axs_value *value,
2225                      struct axs_value *value1, struct axs_value *value2)
2226 {
2227   struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
2228
2229   gen_expr (exp, pc, ax, value2);
2230   gen_usual_unary (ax, value2);
2231   gen_usual_arithmetic (ax, value1, value2);
2232   switch (op)
2233     {
2234     case BINOP_ADD:
2235       if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2236           && pointer_type (value2->type))
2237         {
2238           /* Swap the values and proceed normally.  */
2239           ax_simple (ax, aop_swap);
2240           gen_ptradd (ax, value, value2, value1);
2241         }
2242       else if (pointer_type (value1->type)
2243                && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2244         gen_ptradd (ax, value, value1, value2);
2245       else
2246         gen_binop (ax, value, value1, value2,
2247                    aop_add, aop_add, 1, "addition");
2248       break;
2249     case BINOP_SUB:
2250       if (pointer_type (value1->type)
2251           && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2252         gen_ptrsub (ax,value, value1, value2);
2253       else if (pointer_type (value1->type)
2254                && pointer_type (value2->type))
2255         /* FIXME --- result type should be ptrdiff_t */
2256         gen_ptrdiff (ax, value, value1, value2,
2257                      builtin_type (ax->gdbarch)->builtin_long);
2258       else
2259         gen_binop (ax, value, value1, value2,
2260                    aop_sub, aop_sub, 1, "subtraction");
2261       break;
2262     case BINOP_MUL:
2263       gen_binop (ax, value, value1, value2,
2264                  aop_mul, aop_mul, 1, "multiplication");
2265       break;
2266     case BINOP_DIV:
2267       gen_binop (ax, value, value1, value2,
2268                  aop_div_signed, aop_div_unsigned, 1, "division");
2269       break;
2270     case BINOP_REM:
2271       gen_binop (ax, value, value1, value2,
2272                  aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2273       break;
2274     case BINOP_LSH:
2275       gen_binop (ax, value, value1, value2,
2276                  aop_lsh, aop_lsh, 1, "left shift");
2277       break;
2278     case BINOP_RSH:
2279       gen_binop (ax, value, value1, value2,
2280                  aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2281       break;
2282     case BINOP_SUBSCRIPT:
2283       {
2284         struct type *type;
2285
2286         if (binop_types_user_defined_p (op, value1->type, value2->type))
2287           {
2288             error (_("cannot subscript requested type: "
2289                      "cannot call user defined functions"));
2290           }
2291         else
2292           {
2293             /* If the user attempts to subscript something that is not
2294                an array or pointer type (like a plain int variable for
2295                example), then report this as an error.  */
2296             type = check_typedef (value1->type);
2297             if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2298                 && TYPE_CODE (type) != TYPE_CODE_PTR)
2299               {
2300                 if (TYPE_NAME (type))
2301                   error (_("cannot subscript something of type `%s'"),
2302                          TYPE_NAME (type));
2303                 else
2304                   error (_("cannot subscript requested type"));
2305               }
2306           }
2307
2308         if (!is_integral_type (value2->type))
2309           error (_("Argument to arithmetic operation "
2310                    "not a number or boolean."));
2311
2312         gen_ptradd (ax, value, value1, value2);
2313         gen_deref (value);
2314         break;
2315       }
2316     case BINOP_BITWISE_AND:
2317       gen_binop (ax, value, value1, value2,
2318                  aop_bit_and, aop_bit_and, 0, "bitwise and");
2319       break;
2320
2321     case BINOP_BITWISE_IOR:
2322       gen_binop (ax, value, value1, value2,
2323                  aop_bit_or, aop_bit_or, 0, "bitwise or");
2324       break;
2325       
2326     case BINOP_BITWISE_XOR:
2327       gen_binop (ax, value, value1, value2,
2328                  aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2329       break;
2330
2331     case BINOP_EQUAL:
2332       gen_equal (ax, value, value1, value2, int_type);
2333       break;
2334
2335     case BINOP_NOTEQUAL:
2336       gen_equal (ax, value, value1, value2, int_type);
2337       gen_logical_not (ax, value, int_type);
2338       break;
2339
2340     case BINOP_LESS:
2341       gen_less (ax, value, value1, value2, int_type);
2342       break;
2343
2344     case BINOP_GTR:
2345       ax_simple (ax, aop_swap);
2346       gen_less (ax, value, value1, value2, int_type);
2347       break;
2348
2349     case BINOP_LEQ:
2350       ax_simple (ax, aop_swap);
2351       gen_less (ax, value, value1, value2, int_type);
2352       gen_logical_not (ax, value, int_type);
2353       break;
2354
2355     case BINOP_GEQ:
2356       gen_less (ax, value, value1, value2, int_type);
2357       gen_logical_not (ax, value, int_type);
2358       break;
2359
2360     default:
2361       /* We should only list operators in the outer case statement
2362          that we actually handle in the inner case statement.  */
2363       internal_error (__FILE__, __LINE__,
2364                       _("gen_expr: op case sets don't match"));
2365     }
2366 }
2367 \f
2368
2369 /* Given a single variable and a scope, generate bytecodes to trace
2370    its value.  This is for use in situations where we have only a
2371    variable's name, and no parsed expression; for instance, when the
2372    name comes from a list of local variables of a function.  */
2373
2374 agent_expr_up
2375 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2376                    struct symbol *var, int trace_string)
2377 {
2378   agent_expr_up ax (new agent_expr (gdbarch, scope));
2379   struct axs_value value;
2380
2381   ax->tracing = 1;
2382   ax->trace_string = trace_string;
2383   gen_var_ref (ax.get (), &value, var);
2384
2385   /* If there is no actual variable to trace, flag it by returning
2386      an empty agent expression.  */
2387   if (value.optimized_out)
2388     return agent_expr_up ();
2389
2390   /* Make sure we record the final object, and get rid of it.  */
2391   gen_traced_pop (ax.get (), &value);
2392
2393   /* Oh, and terminate.  */
2394   ax_simple (ax.get (), aop_end);
2395
2396   return ax;
2397 }
2398
2399 /* Generating bytecode from GDB expressions: driver */
2400
2401 /* Given a GDB expression EXPR, return bytecode to trace its value.
2402    The result will use the `trace' and `trace_quick' bytecodes to
2403    record the value of all memory touched by the expression.  The
2404    caller can then use the ax_reqs function to discover which
2405    registers it relies upon.  */
2406
2407 agent_expr_up
2408 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
2409                     int trace_string)
2410 {
2411   agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2412   union exp_element *pc;
2413   struct axs_value value;
2414
2415   pc = expr->elts;
2416   ax->tracing = 1;
2417   ax->trace_string = trace_string;
2418   value.optimized_out = 0;
2419   gen_expr (expr, &pc, ax.get (), &value);
2420
2421   /* Make sure we record the final object, and get rid of it.  */
2422   gen_traced_pop (ax.get (), &value);
2423
2424   /* Oh, and terminate.  */
2425   ax_simple (ax.get (), aop_end);
2426
2427   return ax;
2428 }
2429
2430 /* Given a GDB expression EXPR, return a bytecode sequence that will
2431    evaluate and return a result.  The bytecodes will do a direct
2432    evaluation, using the current data on the target, rather than
2433    recording blocks of memory and registers for later use, as
2434    gen_trace_for_expr does.  The generated bytecode sequence leaves
2435    the result of expression evaluation on the top of the stack.  */
2436
2437 agent_expr_up
2438 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2439 {
2440   agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2441   union exp_element *pc;
2442   struct axs_value value;
2443
2444   pc = expr->elts;
2445   ax->tracing = 0;
2446   value.optimized_out = 0;
2447   gen_expr (expr, &pc, ax.get (), &value);
2448
2449   require_rvalue (ax.get (), &value);
2450
2451   /* Oh, and terminate.  */
2452   ax_simple (ax.get (), aop_end);
2453
2454   return ax;
2455 }
2456
2457 agent_expr_up
2458 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
2459                               int trace_string)
2460 {
2461   agent_expr_up ax (new agent_expr (gdbarch, scope));
2462   struct axs_value value;
2463
2464   ax->tracing = 1;
2465   ax->trace_string = trace_string;
2466
2467   gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope);
2468
2469   /* Make sure we record the final object, and get rid of it.  */
2470   gen_traced_pop (ax.get (), &value);
2471
2472   /* Oh, and terminate.  */
2473   ax_simple (ax.get (), aop_end);
2474
2475   return ax;
2476 }
2477
2478 /* Given a collection of printf-style arguments, generate code to
2479    evaluate the arguments and pass everything to a special
2480    bytecode.  */
2481
2482 agent_expr_up
2483 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2484             CORE_ADDR function, LONGEST channel,
2485             const char *format, int fmtlen,
2486             struct format_piece *frags,
2487             int nargs, struct expression **exprs)
2488 {
2489   agent_expr_up ax (new agent_expr (gdbarch, scope));
2490   union exp_element *pc;
2491   struct axs_value value;
2492   int tem;
2493
2494   /* We're computing values, not doing side effects.  */
2495   ax->tracing = 0;
2496
2497   /* Evaluate and push the args on the stack in reverse order,
2498      for simplicity of collecting them on the target side.  */
2499   for (tem = nargs - 1; tem >= 0; --tem)
2500     {
2501       pc = exprs[tem]->elts;
2502       value.optimized_out = 0;
2503       gen_expr (exprs[tem], &pc, ax.get (), &value);
2504       require_rvalue (ax.get (), &value);
2505     }
2506
2507   /* Push function and channel.  */
2508   ax_const_l (ax.get (), channel);
2509   ax_const_l (ax.get (), function);
2510
2511   /* Issue the printf bytecode proper.  */
2512   ax_simple (ax.get (), aop_printf);
2513   ax_raw_byte (ax.get (), nargs);
2514   ax_string (ax.get (), format, fmtlen);
2515
2516   /* And terminate.  */
2517   ax_simple (ax.get (), aop_end);
2518
2519   return ax;
2520 }
2521
2522 static void
2523 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
2524 {
2525   const char *arg;
2526   int trace_string = 0;
2527
2528   if (!eval)
2529     {
2530       if (*exp == '/')
2531         exp = decode_agent_options (exp, &trace_string);
2532     }
2533
2534   agent_expr_up agent;
2535
2536   arg = exp;
2537   if (!eval && strcmp (arg, "$_ret") == 0)
2538     {
2539       agent = gen_trace_for_return_address (pc, get_current_arch (),
2540                                             trace_string);
2541     }
2542   else
2543     {
2544       expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
2545
2546       if (eval)
2547         {
2548           gdb_assert (trace_string == 0);
2549           agent = gen_eval_for_expr (pc, expr.get ());
2550         }
2551       else
2552         agent = gen_trace_for_expr (pc, expr.get (), trace_string);
2553     }
2554
2555   ax_reqs (agent.get ());
2556   ax_print (gdb_stdout, agent.get ());
2557
2558   /* It would be nice to call ax_reqs here to gather some general info
2559      about the expression, and then print out the result.  */
2560
2561   dont_repeat ();
2562 }
2563
2564 static void
2565 agent_command_1 (char *exp, int eval)
2566 {
2567   /* We don't deal with overlay debugging at the moment.  We need to
2568      think more carefully about this.  If you copy this code into
2569      another command, change the error message; the user shouldn't
2570      have to know anything about agent expressions.  */
2571   if (overlay_debugging)
2572     error (_("GDB can't do agent expression translation with overlays."));
2573
2574   if (exp == 0)
2575     error_no_arg (_("expression to translate"));
2576
2577   if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2578     {
2579       struct linespec_result canonical;
2580       int ix;
2581       struct linespec_sals *iter;
2582
2583       exp = skip_spaces (exp);
2584
2585       event_location_up location = new_linespec_location (&exp);
2586       decode_line_full (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
2587                         (struct symtab *) NULL, 0, &canonical,
2588                         NULL, NULL);
2589       exp = skip_spaces (exp);
2590       if (exp[0] == ',')
2591         {
2592           exp++;
2593           exp = skip_spaces (exp);
2594         }
2595       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
2596         {
2597           int i;
2598
2599           for (i = 0; i < iter->sals.nelts; i++)
2600             agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
2601         }
2602     }
2603   else
2604     agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
2605
2606   dont_repeat ();
2607 }
2608
2609 static void
2610 agent_command (char *exp, int from_tty)
2611 {
2612   agent_command_1 (exp, 0);
2613 }
2614
2615 /* Parse the given expression, compile it into an agent expression
2616    that does direct evaluation, and display the resulting
2617    expression.  */
2618
2619 static void
2620 agent_eval_command (char *exp, int from_tty)
2621 {
2622   agent_command_1 (exp, 1);
2623 }
2624
2625 /* Parse the given expression, compile it into an agent expression
2626    that does a printf, and display the resulting expression.  */
2627
2628 static void
2629 maint_agent_printf_command (char *exp, int from_tty)
2630 {
2631   struct cleanup *old_chain = 0;
2632   struct expression *argvec[100];
2633   struct frame_info *fi = get_current_frame (); /* need current scope */
2634   const char *cmdrest;
2635   const char *format_start, *format_end;
2636   struct format_piece *fpieces;
2637   int nargs;
2638
2639   /* We don't deal with overlay debugging at the moment.  We need to
2640      think more carefully about this.  If you copy this code into
2641      another command, change the error message; the user shouldn't
2642      have to know anything about agent expressions.  */
2643   if (overlay_debugging)
2644     error (_("GDB can't do agent expression translation with overlays."));
2645
2646   if (exp == 0)
2647     error_no_arg (_("expression to translate"));
2648
2649   cmdrest = exp;
2650
2651   cmdrest = skip_spaces_const (cmdrest);
2652
2653   if (*cmdrest++ != '"')
2654     error (_("Must start with a format string."));
2655
2656   format_start = cmdrest;
2657
2658   fpieces = parse_format_string (&cmdrest);
2659
2660   old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2661
2662   format_end = cmdrest;
2663
2664   if (*cmdrest++ != '"')
2665     error (_("Bad format string, non-terminated '\"'."));
2666   
2667   cmdrest = skip_spaces_const (cmdrest);
2668
2669   if (*cmdrest != ',' && *cmdrest != 0)
2670     error (_("Invalid argument syntax"));
2671
2672   if (*cmdrest == ',')
2673     cmdrest++;
2674   cmdrest = skip_spaces_const (cmdrest);
2675
2676   nargs = 0;
2677   while (*cmdrest != '\0')
2678     {
2679       const char *cmd1;
2680
2681       cmd1 = cmdrest;
2682       expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2683       argvec[nargs] = expr.release ();
2684       ++nargs;
2685       cmdrest = cmd1;
2686       if (*cmdrest == ',')
2687         ++cmdrest;
2688       /* else complain? */
2689     }
2690
2691
2692   agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (),
2693                                     0, 0,
2694                                     format_start, format_end - format_start,
2695                                     fpieces, nargs, argvec);
2696   ax_reqs (agent.get ());
2697   ax_print (gdb_stdout, agent.get ());
2698
2699   /* It would be nice to call ax_reqs here to gather some general info
2700      about the expression, and then print out the result.  */
2701
2702   do_cleanups (old_chain);
2703   dont_repeat ();
2704 }
2705 \f
2706
2707 /* Initialization code.  */
2708
2709 void _initialize_ax_gdb (void);
2710 void
2711 _initialize_ax_gdb (void)
2712 {
2713   add_cmd ("agent", class_maintenance, agent_command,
2714            _("\
2715 Translate an expression into remote agent bytecode for tracing.\n\
2716 Usage: maint agent [-at location,] EXPRESSION\n\
2717 If -at is given, generate remote agent bytecode for this location.\n\
2718 If not, generate remote agent bytecode for current frame pc address."),
2719            &maintenancelist);
2720
2721   add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2722            _("\
2723 Translate an expression into remote agent bytecode for evaluation.\n\
2724 Usage: maint agent-eval [-at location,] EXPRESSION\n\
2725 If -at is given, generate remote agent bytecode for this location.\n\
2726 If not, generate remote agent bytecode for current frame pc address."),
2727            &maintenancelist);
2728
2729   add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2730            _("Translate an expression into remote "
2731              "agent bytecode for evaluation and display the bytecodes."),
2732            &maintenancelist);
2733 }