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