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