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