GDB (API)
/home/stan/gdb/src/gdb/ax-gdb.c
Go to the documentation of this file.
00001 /* GDB-specific functions for operating on agent expressions.
00002 
00003    Copyright (C) 1998-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "symtab.h"
00022 #include "symfile.h"
00023 #include "gdbtypes.h"
00024 #include "language.h"
00025 #include "value.h"
00026 #include "expression.h"
00027 #include "command.h"
00028 #include "gdbcmd.h"
00029 #include "frame.h"
00030 #include "target.h"
00031 #include "ax.h"
00032 #include "ax-gdb.h"
00033 #include "gdb_string.h"
00034 #include "block.h"
00035 #include "regcache.h"
00036 #include "user-regs.h"
00037 #include "language.h"
00038 #include "dictionary.h"
00039 #include "breakpoint.h"
00040 #include "tracepoint.h"
00041 #include "cp-support.h"
00042 #include "arch-utils.h"
00043 #include "cli/cli-utils.h"
00044 #include "linespec.h"
00045 
00046 #include "valprint.h"
00047 #include "c-lang.h"
00048 
00049 #include "format.h"
00050 
00051 /* To make sense of this file, you should read doc/agentexpr.texi.
00052    Then look at the types and enums in ax-gdb.h.  For the code itself,
00053    look at gen_expr, towards the bottom; that's the main function that
00054    looks at the GDB expressions and calls everything else to generate
00055    code.
00056 
00057    I'm beginning to wonder whether it wouldn't be nicer to internally
00058    generate trees, with types, and then spit out the bytecode in
00059    linear form afterwards; we could generate fewer `swap', `ext', and
00060    `zero_ext' bytecodes that way; it would make good constant folding
00061    easier, too.  But at the moment, I think we should be willing to
00062    pay for the simplicity of this code with less-than-optimal bytecode
00063    strings.
00064 
00065    Remember, "GBD" stands for "Great Britain, Dammit!"  So be careful.  */
00066 
00067 
00068 
00069 /* Prototypes for local functions.  */
00070 
00071 /* There's a standard order to the arguments of these functions:
00072    union exp_element ** --- pointer into expression
00073    struct agent_expr * --- agent expression buffer to generate code into
00074    struct axs_value * --- describes value left on top of stack  */
00075 
00076 static struct value *const_var_ref (struct symbol *var);
00077 static struct value *const_expr (union exp_element **pc);
00078 static struct value *maybe_const_expr (union exp_element **pc);
00079 
00080 static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
00081                             struct axs_value *);
00082 
00083 static void gen_sign_extend (struct agent_expr *, struct type *);
00084 static void gen_extend (struct agent_expr *, struct type *);
00085 static void gen_fetch (struct agent_expr *, struct type *);
00086 static void gen_left_shift (struct agent_expr *, int);
00087 
00088 
00089 static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
00090 static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
00091 static void gen_offset (struct agent_expr *ax, int offset);
00092 static void gen_sym_offset (struct agent_expr *, struct symbol *);
00093 static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
00094                          struct axs_value *value, struct symbol *var);
00095 
00096 
00097 static void gen_int_literal (struct agent_expr *ax,
00098                              struct axs_value *value,
00099                              LONGEST k, struct type *type);
00100 
00101 static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
00102                              struct axs_value *value);
00103 static int type_wider_than (struct type *type1, struct type *type2);
00104 static struct type *max_type (struct type *type1, struct type *type2);
00105 static void gen_conversion (struct agent_expr *ax,
00106                             struct type *from, struct type *to);
00107 static int is_nontrivial_conversion (struct type *from, struct type *to);
00108 static void gen_usual_arithmetic (struct expression *exp,
00109                                   struct agent_expr *ax,
00110                                   struct axs_value *value1,
00111                                   struct axs_value *value2);
00112 static void gen_integral_promotions (struct expression *exp,
00113                                      struct agent_expr *ax,
00114                                      struct axs_value *value);
00115 static void gen_cast (struct agent_expr *ax,
00116                       struct axs_value *value, struct type *type);
00117 static void gen_scale (struct agent_expr *ax,
00118                        enum agent_op op, struct type *type);
00119 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
00120                         struct axs_value *value1, struct axs_value *value2);
00121 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
00122                         struct axs_value *value1, struct axs_value *value2);
00123 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
00124                          struct axs_value *value1, struct axs_value *value2,
00125                          struct type *result_type);
00126 static void gen_binop (struct agent_expr *ax,
00127                        struct axs_value *value,
00128                        struct axs_value *value1,
00129                        struct axs_value *value2,
00130                        enum agent_op op,
00131                        enum agent_op op_unsigned, int may_carry, char *name);
00132 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
00133                              struct type *result_type);
00134 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
00135 static void gen_deref (struct agent_expr *, struct axs_value *);
00136 static void gen_address_of (struct agent_expr *, struct axs_value *);
00137 static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
00138                               struct axs_value *value,
00139                               struct type *type, int start, int end);
00140 static void gen_primitive_field (struct expression *exp,
00141                                  struct agent_expr *ax,
00142                                  struct axs_value *value,
00143                                  int offset, int fieldno, struct type *type);
00144 static int gen_struct_ref_recursive (struct expression *exp,
00145                                      struct agent_expr *ax,
00146                                      struct axs_value *value,
00147                                      char *field, int offset,
00148                                      struct type *type);
00149 static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
00150                             struct axs_value *value,
00151                             char *field,
00152                             char *operator_name, char *operand_name);
00153 static void gen_static_field (struct gdbarch *gdbarch,
00154                               struct agent_expr *ax, struct axs_value *value,
00155                               struct type *type, int fieldno);
00156 static void gen_repeat (struct expression *exp, union exp_element **pc,
00157                         struct agent_expr *ax, struct axs_value *value);
00158 static void gen_sizeof (struct expression *exp, union exp_element **pc,
00159                         struct agent_expr *ax, struct axs_value *value,
00160                         struct type *size_type);
00161 static void gen_expr_binop_rest (struct expression *exp,
00162                                  enum exp_opcode op, union exp_element **pc,
00163                                  struct agent_expr *ax,
00164                                  struct axs_value *value,
00165                                  struct axs_value *value1,
00166                                  struct axs_value *value2);
00167 
00168 static void agent_command (char *exp, int from_tty);
00169 
00170 
00171 /* Detecting constant expressions.  */
00172 
00173 /* If the variable reference at *PC is a constant, return its value.
00174    Otherwise, return zero.
00175 
00176    Hey, Wally!  How can a variable reference be a constant?
00177 
00178    Well, Beav, this function really handles the OP_VAR_VALUE operator,
00179    not specifically variable references.  GDB uses OP_VAR_VALUE to
00180    refer to any kind of symbolic reference: function names, enum
00181    elements, and goto labels are all handled through the OP_VAR_VALUE
00182    operator, even though they're constants.  It makes sense given the
00183    situation.
00184 
00185    Gee, Wally, don'cha wonder sometimes if data representations that
00186    subvert commonly accepted definitions of terms in favor of heavily
00187    context-specific interpretations are really just a tool of the
00188    programming hegemony to preserve their power and exclude the
00189    proletariat?  */
00190 
00191 static struct value *
00192 const_var_ref (struct symbol *var)
00193 {
00194   struct type *type = SYMBOL_TYPE (var);
00195 
00196   switch (SYMBOL_CLASS (var))
00197     {
00198     case LOC_CONST:
00199       return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
00200 
00201     case LOC_LABEL:
00202       return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
00203 
00204     default:
00205       return 0;
00206     }
00207 }
00208 
00209 
00210 /* If the expression starting at *PC has a constant value, return it.
00211    Otherwise, return zero.  If we return a value, then *PC will be
00212    advanced to the end of it.  If we return zero, *PC could be
00213    anywhere.  */
00214 static struct value *
00215 const_expr (union exp_element **pc)
00216 {
00217   enum exp_opcode op = (*pc)->opcode;
00218   struct value *v1;
00219 
00220   switch (op)
00221     {
00222     case OP_LONG:
00223       {
00224         struct type *type = (*pc)[1].type;
00225         LONGEST k = (*pc)[2].longconst;
00226 
00227         (*pc) += 4;
00228         return value_from_longest (type, k);
00229       }
00230 
00231     case OP_VAR_VALUE:
00232       {
00233         struct value *v = const_var_ref ((*pc)[2].symbol);
00234 
00235         (*pc) += 4;
00236         return v;
00237       }
00238 
00239       /* We could add more operators in here.  */
00240 
00241     case UNOP_NEG:
00242       (*pc)++;
00243       v1 = const_expr (pc);
00244       if (v1)
00245         return value_neg (v1);
00246       else
00247         return 0;
00248 
00249     default:
00250       return 0;
00251     }
00252 }
00253 
00254 
00255 /* Like const_expr, but guarantee also that *PC is undisturbed if the
00256    expression is not constant.  */
00257 static struct value *
00258 maybe_const_expr (union exp_element **pc)
00259 {
00260   union exp_element *tentative_pc = *pc;
00261   struct value *v = const_expr (&tentative_pc);
00262 
00263   /* If we got a value, then update the real PC.  */
00264   if (v)
00265     *pc = tentative_pc;
00266 
00267   return v;
00268 }
00269 
00270 
00271 /* Generating bytecode from GDB expressions: general assumptions */
00272 
00273 /* Here are a few general assumptions made throughout the code; if you
00274    want to make a change that contradicts one of these, then you'd
00275    better scan things pretty thoroughly.
00276 
00277    - We assume that all values occupy one stack element.  For example,
00278    sometimes we'll swap to get at the left argument to a binary
00279    operator.  If we decide that void values should occupy no stack
00280    elements, or that synthetic arrays (whose size is determined at
00281    run time, created by the `@' operator) should occupy two stack
00282    elements (address and length), then this will cause trouble.
00283 
00284    - We assume the stack elements are infinitely wide, and that we
00285    don't have to worry what happens if the user requests an
00286    operation that is wider than the actual interpreter's stack.
00287    That is, it's up to the interpreter to handle directly all the
00288    integer widths the user has access to.  (Woe betide the language
00289    with bignums!)
00290 
00291    - We don't support side effects.  Thus, we don't have to worry about
00292    GCC's generalized lvalues, function calls, etc.
00293 
00294    - We don't support floating point.  Many places where we switch on
00295    some type don't bother to include cases for floating point; there
00296    may be even more subtle ways this assumption exists.  For
00297    example, the arguments to % must be integers.
00298 
00299    - We assume all subexpressions have a static, unchanging type.  If
00300    we tried to support convenience variables, this would be a
00301    problem.
00302 
00303    - All values on the stack should always be fully zero- or
00304    sign-extended.
00305 
00306    (I wasn't sure whether to choose this or its opposite --- that
00307    only addresses are assumed extended --- but it turns out that
00308    neither convention completely eliminates spurious extend
00309    operations (if everything is always extended, then you have to
00310    extend after add, because it could overflow; if nothing is
00311    extended, then you end up producing extends whenever you change
00312    sizes), and this is simpler.)  */
00313 
00314 
00315 /* Scan for all static fields in the given class, including any base
00316    classes, and generate tracing bytecodes for each.  */
00317 
00318 static void
00319 gen_trace_static_fields (struct gdbarch *gdbarch,
00320                          struct agent_expr *ax,
00321                          struct type *type)
00322 {
00323   int i, nbases = TYPE_N_BASECLASSES (type);
00324   struct axs_value value;
00325 
00326   CHECK_TYPEDEF (type);
00327 
00328   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
00329     {
00330       if (field_is_static (&TYPE_FIELD (type, i)))
00331         {
00332           gen_static_field (gdbarch, ax, &value, type, i);
00333           if (value.optimized_out)
00334             continue;
00335           switch (value.kind)
00336             {
00337             case axs_lvalue_memory:
00338               {
00339                 /* Initialize the TYPE_LENGTH if it is a typedef.  */
00340                 check_typedef (value.type);
00341                 ax_const_l (ax, TYPE_LENGTH (value.type));
00342                 ax_simple (ax, aop_trace);
00343               }
00344               break;
00345 
00346             case axs_lvalue_register:
00347               /* We don't actually need the register's value to be pushed,
00348                  just note that we need it to be collected.  */
00349               ax_reg_mask (ax, value.u.reg);
00350 
00351             default:
00352               break;
00353             }
00354         }
00355     }
00356 
00357   /* Now scan through base classes recursively.  */
00358   for (i = 0; i < nbases; i++)
00359     {
00360       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
00361 
00362       gen_trace_static_fields (gdbarch, ax, basetype);
00363     }
00364 }
00365 
00366 /* Trace the lvalue on the stack, if it needs it.  In either case, pop
00367    the value.  Useful on the left side of a comma, and at the end of
00368    an expression being used for tracing.  */
00369 static void
00370 gen_traced_pop (struct gdbarch *gdbarch,
00371                 struct agent_expr *ax, struct axs_value *value)
00372 {
00373   int string_trace = 0;
00374   if (ax->trace_string
00375       && TYPE_CODE (value->type) == TYPE_CODE_PTR
00376       && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
00377                                  's'))
00378     string_trace = 1;
00379 
00380   if (ax->tracing)
00381     switch (value->kind)
00382       {
00383       case axs_rvalue:
00384         if (string_trace)
00385           {
00386             ax_const_l (ax, ax->trace_string);
00387             ax_simple (ax, aop_tracenz);
00388           }
00389         else
00390           /* We don't trace rvalues, just the lvalues necessary to
00391              produce them.  So just dispose of this value.  */
00392           ax_simple (ax, aop_pop);
00393         break;
00394 
00395       case axs_lvalue_memory:
00396         {
00397           if (string_trace)
00398             ax_simple (ax, aop_dup);
00399 
00400           /* Initialize the TYPE_LENGTH if it is a typedef.  */
00401           check_typedef (value->type);
00402 
00403           /* There's no point in trying to use a trace_quick bytecode
00404              here, since "trace_quick SIZE pop" is three bytes, whereas
00405              "const8 SIZE trace" is also three bytes, does the same
00406              thing, and the simplest code which generates that will also
00407              work correctly for objects with large sizes.  */
00408           ax_const_l (ax, TYPE_LENGTH (value->type));
00409           ax_simple (ax, aop_trace);
00410 
00411           if (string_trace)
00412             {
00413               ax_simple (ax, aop_ref32);
00414               ax_const_l (ax, ax->trace_string);
00415               ax_simple (ax, aop_tracenz);
00416             }
00417         }
00418         break;
00419 
00420       case axs_lvalue_register:
00421         /* We don't actually need the register's value to be on the
00422            stack, and the target will get heartburn if the register is
00423            larger than will fit in a stack, so just mark it for
00424            collection and be done with it.  */
00425         ax_reg_mask (ax, value->u.reg);
00426        
00427         /* But if the register points to a string, assume the value
00428            will fit on the stack and push it anyway.  */
00429         if (string_trace)
00430           {
00431             ax_reg (ax, value->u.reg);
00432             ax_const_l (ax, ax->trace_string);
00433             ax_simple (ax, aop_tracenz);
00434           }
00435         break;
00436       }
00437   else
00438     /* If we're not tracing, just pop the value.  */
00439     ax_simple (ax, aop_pop);
00440 
00441   /* To trace C++ classes with static fields stored elsewhere.  */
00442   if (ax->tracing
00443       && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
00444           || TYPE_CODE (value->type) == TYPE_CODE_UNION))
00445     gen_trace_static_fields (gdbarch, ax, value->type);
00446 }
00447 
00448 
00449 
00450 /* Generating bytecode from GDB expressions: helper functions */
00451 
00452 /* Assume that the lower bits of the top of the stack is a value of
00453    type TYPE, and the upper bits are zero.  Sign-extend if necessary.  */
00454 static void
00455 gen_sign_extend (struct agent_expr *ax, struct type *type)
00456 {
00457   /* Do we need to sign-extend this?  */
00458   if (!TYPE_UNSIGNED (type))
00459     ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
00460 }
00461 
00462 
00463 /* Assume the lower bits of the top of the stack hold a value of type
00464    TYPE, and the upper bits are garbage.  Sign-extend or truncate as
00465    needed.  */
00466 static void
00467 gen_extend (struct agent_expr *ax, struct type *type)
00468 {
00469   int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
00470 
00471   /* I just had to.  */
00472   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
00473 }
00474 
00475 
00476 /* Assume that the top of the stack contains a value of type "pointer
00477    to TYPE"; generate code to fetch its value.  Note that TYPE is the
00478    target type, not the pointer type.  */
00479 static void
00480 gen_fetch (struct agent_expr *ax, struct type *type)
00481 {
00482   if (ax->tracing)
00483     {
00484       /* Record the area of memory we're about to fetch.  */
00485       ax_trace_quick (ax, TYPE_LENGTH (type));
00486     }
00487 
00488   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
00489     type = TYPE_TARGET_TYPE (type);
00490 
00491   switch (TYPE_CODE (type))
00492     {
00493     case TYPE_CODE_PTR:
00494     case TYPE_CODE_REF:
00495     case TYPE_CODE_ENUM:
00496     case TYPE_CODE_INT:
00497     case TYPE_CODE_CHAR:
00498     case TYPE_CODE_BOOL:
00499       /* It's a scalar value, so we know how to dereference it.  How
00500          many bytes long is it?  */
00501       switch (TYPE_LENGTH (type))
00502         {
00503         case 8 / TARGET_CHAR_BIT:
00504           ax_simple (ax, aop_ref8);
00505           break;
00506         case 16 / TARGET_CHAR_BIT:
00507           ax_simple (ax, aop_ref16);
00508           break;
00509         case 32 / TARGET_CHAR_BIT:
00510           ax_simple (ax, aop_ref32);
00511           break;
00512         case 64 / TARGET_CHAR_BIT:
00513           ax_simple (ax, aop_ref64);
00514           break;
00515 
00516           /* Either our caller shouldn't have asked us to dereference
00517              that pointer (other code's fault), or we're not
00518              implementing something we should be (this code's fault).
00519              In any case, it's a bug the user shouldn't see.  */
00520         default:
00521           internal_error (__FILE__, __LINE__,
00522                           _("gen_fetch: strange size"));
00523         }
00524 
00525       gen_sign_extend (ax, type);
00526       break;
00527 
00528     default:
00529       /* Our caller requested us to dereference a pointer from an unsupported
00530          type.  Error out and give callers a chance to handle the failure
00531          gracefully.  */
00532       error (_("gen_fetch: Unsupported type code `%s'."),
00533              TYPE_NAME (type));
00534     }
00535 }
00536 
00537 
00538 /* Generate code to left shift the top of the stack by DISTANCE bits, or
00539    right shift it by -DISTANCE bits if DISTANCE < 0.  This generates
00540    unsigned (logical) right shifts.  */
00541 static void
00542 gen_left_shift (struct agent_expr *ax, int distance)
00543 {
00544   if (distance > 0)
00545     {
00546       ax_const_l (ax, distance);
00547       ax_simple (ax, aop_lsh);
00548     }
00549   else if (distance < 0)
00550     {
00551       ax_const_l (ax, -distance);
00552       ax_simple (ax, aop_rsh_unsigned);
00553     }
00554 }
00555 
00556 
00557 
00558 /* Generating bytecode from GDB expressions: symbol references */
00559 
00560 /* Generate code to push the base address of the argument portion of
00561    the top stack frame.  */
00562 static void
00563 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
00564 {
00565   int frame_reg;
00566   LONGEST frame_offset;
00567 
00568   gdbarch_virtual_frame_pointer (gdbarch,
00569                                  ax->scope, &frame_reg, &frame_offset);
00570   ax_reg (ax, frame_reg);
00571   gen_offset (ax, frame_offset);
00572 }
00573 
00574 
00575 /* Generate code to push the base address of the locals portion of the
00576    top stack frame.  */
00577 static void
00578 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
00579 {
00580   int frame_reg;
00581   LONGEST frame_offset;
00582 
00583   gdbarch_virtual_frame_pointer (gdbarch,
00584                                  ax->scope, &frame_reg, &frame_offset);
00585   ax_reg (ax, frame_reg);
00586   gen_offset (ax, frame_offset);
00587 }
00588 
00589 
00590 /* Generate code to add OFFSET to the top of the stack.  Try to
00591    generate short and readable code.  We use this for getting to
00592    variables on the stack, and structure members.  If we were
00593    programming in ML, it would be clearer why these are the same
00594    thing.  */
00595 static void
00596 gen_offset (struct agent_expr *ax, int offset)
00597 {
00598   /* It would suffice to simply push the offset and add it, but this
00599      makes it easier to read positive and negative offsets in the
00600      bytecode.  */
00601   if (offset > 0)
00602     {
00603       ax_const_l (ax, offset);
00604       ax_simple (ax, aop_add);
00605     }
00606   else if (offset < 0)
00607     {
00608       ax_const_l (ax, -offset);
00609       ax_simple (ax, aop_sub);
00610     }
00611 }
00612 
00613 
00614 /* In many cases, a symbol's value is the offset from some other
00615    address (stack frame, base register, etc.)  Generate code to add
00616    VAR's value to the top of the stack.  */
00617 static void
00618 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
00619 {
00620   gen_offset (ax, SYMBOL_VALUE (var));
00621 }
00622 
00623 
00624 /* Generate code for a variable reference to AX.  The variable is the
00625    symbol VAR.  Set VALUE to describe the result.  */
00626 
00627 static void
00628 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
00629              struct axs_value *value, struct symbol *var)
00630 {
00631   /* Dereference any typedefs.  */
00632   value->type = check_typedef (SYMBOL_TYPE (var));
00633   value->optimized_out = 0;
00634 
00635   if (SYMBOL_COMPUTED_OPS (var) != NULL)
00636     {
00637       SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
00638       return;
00639     }
00640 
00641   /* I'm imitating the code in read_var_value.  */
00642   switch (SYMBOL_CLASS (var))
00643     {
00644     case LOC_CONST:             /* A constant, like an enum value.  */
00645       ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
00646       value->kind = axs_rvalue;
00647       break;
00648 
00649     case LOC_LABEL:             /* A goto label, being used as a value.  */
00650       ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
00651       value->kind = axs_rvalue;
00652       break;
00653 
00654     case LOC_CONST_BYTES:
00655       internal_error (__FILE__, __LINE__,
00656                       _("gen_var_ref: LOC_CONST_BYTES "
00657                         "symbols are not supported"));
00658 
00659       /* Variable at a fixed location in memory.  Easy.  */
00660     case LOC_STATIC:
00661       /* Push the address of the variable.  */
00662       ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
00663       value->kind = axs_lvalue_memory;
00664       break;
00665 
00666     case LOC_ARG:               /* var lives in argument area of frame */
00667       gen_frame_args_address (gdbarch, ax);
00668       gen_sym_offset (ax, var);
00669       value->kind = axs_lvalue_memory;
00670       break;
00671 
00672     case LOC_REF_ARG:           /* As above, but the frame slot really
00673                                    holds the address of the variable.  */
00674       gen_frame_args_address (gdbarch, ax);
00675       gen_sym_offset (ax, var);
00676       /* Don't assume any particular pointer size.  */
00677       gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
00678       value->kind = axs_lvalue_memory;
00679       break;
00680 
00681     case LOC_LOCAL:             /* var lives in locals area of frame */
00682       gen_frame_locals_address (gdbarch, ax);
00683       gen_sym_offset (ax, var);
00684       value->kind = axs_lvalue_memory;
00685       break;
00686 
00687     case LOC_TYPEDEF:
00688       error (_("Cannot compute value of typedef `%s'."),
00689              SYMBOL_PRINT_NAME (var));
00690       break;
00691 
00692     case LOC_BLOCK:
00693       ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
00694       value->kind = axs_rvalue;
00695       break;
00696 
00697     case LOC_REGISTER:
00698       /* Don't generate any code at all; in the process of treating
00699          this as an lvalue or rvalue, the caller will generate the
00700          right code.  */
00701       value->kind = axs_lvalue_register;
00702       value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
00703       break;
00704 
00705       /* A lot like LOC_REF_ARG, but the pointer lives directly in a
00706          register, not on the stack.  Simpler than LOC_REGISTER
00707          because it's just like any other case where the thing
00708          has a real address.  */
00709     case LOC_REGPARM_ADDR:
00710       ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
00711       value->kind = axs_lvalue_memory;
00712       break;
00713 
00714     case LOC_UNRESOLVED:
00715       {
00716         struct minimal_symbol *msym
00717           = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
00718 
00719         if (!msym)
00720           error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
00721 
00722         /* Push the address of the variable.  */
00723         ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
00724         value->kind = axs_lvalue_memory;
00725       }
00726       break;
00727 
00728     case LOC_COMPUTED:
00729       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
00730 
00731     case LOC_OPTIMIZED_OUT:
00732       /* Flag this, but don't say anything; leave it up to callers to
00733          warn the user.  */
00734       value->optimized_out = 1;
00735       break;
00736 
00737     default:
00738       error (_("Cannot find value of botched symbol `%s'."),
00739              SYMBOL_PRINT_NAME (var));
00740       break;
00741     }
00742 }
00743 
00744 
00745 
00746 /* Generating bytecode from GDB expressions: literals */
00747 
00748 static void
00749 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
00750                  struct type *type)
00751 {
00752   ax_const_l (ax, k);
00753   value->kind = axs_rvalue;
00754   value->type = check_typedef (type);
00755 }
00756 
00757 
00758 
00759 /* Generating bytecode from GDB expressions: unary conversions, casts */
00760 
00761 /* Take what's on the top of the stack (as described by VALUE), and
00762    try to make an rvalue out of it.  Signal an error if we can't do
00763    that.  */
00764 void
00765 require_rvalue (struct agent_expr *ax, struct axs_value *value)
00766 {
00767   /* Only deal with scalars, structs and such may be too large
00768      to fit in a stack entry.  */
00769   value->type = check_typedef (value->type);
00770   if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
00771       || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
00772       || TYPE_CODE (value->type) == TYPE_CODE_UNION
00773       || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
00774     error (_("Value not scalar: cannot be an rvalue."));
00775 
00776   switch (value->kind)
00777     {
00778     case axs_rvalue:
00779       /* It's already an rvalue.  */
00780       break;
00781 
00782     case axs_lvalue_memory:
00783       /* The top of stack is the address of the object.  Dereference.  */
00784       gen_fetch (ax, value->type);
00785       break;
00786 
00787     case axs_lvalue_register:
00788       /* There's nothing on the stack, but value->u.reg is the
00789          register number containing the value.
00790 
00791          When we add floating-point support, this is going to have to
00792          change.  What about SPARC register pairs, for example?  */
00793       ax_reg (ax, value->u.reg);
00794       gen_extend (ax, value->type);
00795       break;
00796     }
00797 
00798   value->kind = axs_rvalue;
00799 }
00800 
00801 
00802 /* Assume the top of the stack is described by VALUE, and perform the
00803    usual unary conversions.  This is motivated by ANSI 6.2.2, but of
00804    course GDB expressions are not ANSI; they're the mishmash union of
00805    a bunch of languages.  Rah.
00806 
00807    NOTE!  This function promises to produce an rvalue only when the
00808    incoming value is of an appropriate type.  In other words, the
00809    consumer of the value this function produces may assume the value
00810    is an rvalue only after checking its type.
00811 
00812    The immediate issue is that if the user tries to use a structure or
00813    union as an operand of, say, the `+' operator, we don't want to try
00814    to convert that structure to an rvalue; require_rvalue will bomb on
00815    structs and unions.  Rather, we want to simply pass the struct
00816    lvalue through unchanged, and let `+' raise an error.  */
00817 
00818 static void
00819 gen_usual_unary (struct expression *exp, struct agent_expr *ax,
00820                  struct axs_value *value)
00821 {
00822   /* We don't have to generate any code for the usual integral
00823      conversions, since values are always represented as full-width on
00824      the stack.  Should we tweak the type?  */
00825 
00826   /* Some types require special handling.  */
00827   switch (TYPE_CODE (value->type))
00828     {
00829       /* Functions get converted to a pointer to the function.  */
00830     case TYPE_CODE_FUNC:
00831       value->type = lookup_pointer_type (value->type);
00832       value->kind = axs_rvalue; /* Should always be true, but just in case.  */
00833       break;
00834 
00835       /* Arrays get converted to a pointer to their first element, and
00836          are no longer an lvalue.  */
00837     case TYPE_CODE_ARRAY:
00838       {
00839         struct type *elements = TYPE_TARGET_TYPE (value->type);
00840 
00841         value->type = lookup_pointer_type (elements);
00842         value->kind = axs_rvalue;
00843         /* We don't need to generate any code; the address of the array
00844            is also the address of its first element.  */
00845       }
00846       break;
00847 
00848       /* Don't try to convert structures and unions to rvalues.  Let the
00849          consumer signal an error.  */
00850     case TYPE_CODE_STRUCT:
00851     case TYPE_CODE_UNION:
00852       return;
00853     }
00854 
00855   /* If the value is an lvalue, dereference it.  */
00856   require_rvalue (ax, value);
00857 }
00858 
00859 
00860 /* Return non-zero iff the type TYPE1 is considered "wider" than the
00861    type TYPE2, according to the rules described in gen_usual_arithmetic.  */
00862 static int
00863 type_wider_than (struct type *type1, struct type *type2)
00864 {
00865   return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
00866           || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
00867               && TYPE_UNSIGNED (type1)
00868               && !TYPE_UNSIGNED (type2)));
00869 }
00870 
00871 
00872 /* Return the "wider" of the two types TYPE1 and TYPE2.  */
00873 static struct type *
00874 max_type (struct type *type1, struct type *type2)
00875 {
00876   return type_wider_than (type1, type2) ? type1 : type2;
00877 }
00878 
00879 
00880 /* Generate code to convert a scalar value of type FROM to type TO.  */
00881 static void
00882 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
00883 {
00884   /* Perhaps there is a more graceful way to state these rules.  */
00885 
00886   /* If we're converting to a narrower type, then we need to clear out
00887      the upper bits.  */
00888   if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
00889     gen_extend (ax, from);
00890 
00891   /* If the two values have equal width, but different signednesses,
00892      then we need to extend.  */
00893   else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
00894     {
00895       if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
00896         gen_extend (ax, to);
00897     }
00898 
00899   /* If we're converting to a wider type, and becoming unsigned, then
00900      we need to zero out any possible sign bits.  */
00901   else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
00902     {
00903       if (TYPE_UNSIGNED (to))
00904         gen_extend (ax, to);
00905     }
00906 }
00907 
00908 
00909 /* Return non-zero iff the type FROM will require any bytecodes to be
00910    emitted to be converted to the type TO.  */
00911 static int
00912 is_nontrivial_conversion (struct type *from, struct type *to)
00913 {
00914   struct agent_expr *ax = new_agent_expr (NULL, 0);
00915   int nontrivial;
00916 
00917   /* Actually generate the code, and see if anything came out.  At the
00918      moment, it would be trivial to replicate the code in
00919      gen_conversion here, but in the future, when we're supporting
00920      floating point and the like, it may not be.  Doing things this
00921      way allows this function to be independent of the logic in
00922      gen_conversion.  */
00923   gen_conversion (ax, from, to);
00924   nontrivial = ax->len > 0;
00925   free_agent_expr (ax);
00926   return nontrivial;
00927 }
00928 
00929 
00930 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
00931    6.2.1.5) for the two operands of an arithmetic operator.  This
00932    effectively finds a "least upper bound" type for the two arguments,
00933    and promotes each argument to that type.  *VALUE1 and *VALUE2
00934    describe the values as they are passed in, and as they are left.  */
00935 static void
00936 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
00937                       struct axs_value *value1, struct axs_value *value2)
00938 {
00939   /* Do the usual binary conversions.  */
00940   if (TYPE_CODE (value1->type) == TYPE_CODE_INT
00941       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
00942     {
00943       /* The ANSI integral promotions seem to work this way: Order the
00944          integer types by size, and then by signedness: an n-bit
00945          unsigned type is considered "wider" than an n-bit signed
00946          type.  Promote to the "wider" of the two types, and always
00947          promote at least to int.  */
00948       struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
00949                                       max_type (value1->type, value2->type));
00950 
00951       /* Deal with value2, on the top of the stack.  */
00952       gen_conversion (ax, value2->type, target);
00953 
00954       /* Deal with value1, not on the top of the stack.  Don't
00955          generate the `swap' instructions if we're not actually going
00956          to do anything.  */
00957       if (is_nontrivial_conversion (value1->type, target))
00958         {
00959           ax_simple (ax, aop_swap);
00960           gen_conversion (ax, value1->type, target);
00961           ax_simple (ax, aop_swap);
00962         }
00963 
00964       value1->type = value2->type = check_typedef (target);
00965     }
00966 }
00967 
00968 
00969 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
00970    the value on the top of the stack, as described by VALUE.  Assume
00971    the value has integral type.  */
00972 static void
00973 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
00974                          struct axs_value *value)
00975 {
00976   const struct builtin_type *builtin = builtin_type (exp->gdbarch);
00977 
00978   if (!type_wider_than (value->type, builtin->builtin_int))
00979     {
00980       gen_conversion (ax, value->type, builtin->builtin_int);
00981       value->type = builtin->builtin_int;
00982     }
00983   else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
00984     {
00985       gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
00986       value->type = builtin->builtin_unsigned_int;
00987     }
00988 }
00989 
00990 
00991 /* Generate code for a cast to TYPE.  */
00992 static void
00993 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
00994 {
00995   /* GCC does allow casts to yield lvalues, so this should be fixed
00996      before merging these changes into the trunk.  */
00997   require_rvalue (ax, value);
00998   /* Dereference typedefs.  */
00999   type = check_typedef (type);
01000 
01001   switch (TYPE_CODE (type))
01002     {
01003     case TYPE_CODE_PTR:
01004     case TYPE_CODE_REF:
01005       /* It's implementation-defined, and I'll bet this is what GCC
01006          does.  */
01007       break;
01008 
01009     case TYPE_CODE_ARRAY:
01010     case TYPE_CODE_STRUCT:
01011     case TYPE_CODE_UNION:
01012     case TYPE_CODE_FUNC:
01013       error (_("Invalid type cast: intended type must be scalar."));
01014 
01015     case TYPE_CODE_ENUM:
01016     case TYPE_CODE_BOOL:
01017       /* We don't have to worry about the size of the value, because
01018          all our integral values are fully sign-extended, and when
01019          casting pointers we can do anything we like.  Is there any
01020          way for us to know what GCC actually does with a cast like
01021          this?  */
01022       break;
01023 
01024     case TYPE_CODE_INT:
01025       gen_conversion (ax, value->type, type);
01026       break;
01027 
01028     case TYPE_CODE_VOID:
01029       /* We could pop the value, and rely on everyone else to check
01030          the type and notice that this value doesn't occupy a stack
01031          slot.  But for now, leave the value on the stack, and
01032          preserve the "value == stack element" assumption.  */
01033       break;
01034 
01035     default:
01036       error (_("Casts to requested type are not yet implemented."));
01037     }
01038 
01039   value->type = type;
01040 }
01041 
01042 
01043 
01044 /* Generating bytecode from GDB expressions: arithmetic */
01045 
01046 /* Scale the integer on the top of the stack by the size of the target
01047    of the pointer type TYPE.  */
01048 static void
01049 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
01050 {
01051   struct type *element = TYPE_TARGET_TYPE (type);
01052 
01053   if (TYPE_LENGTH (element) != 1)
01054     {
01055       ax_const_l (ax, TYPE_LENGTH (element));
01056       ax_simple (ax, op);
01057     }
01058 }
01059 
01060 
01061 /* Generate code for pointer arithmetic PTR + INT.  */
01062 static void
01063 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
01064             struct axs_value *value1, struct axs_value *value2)
01065 {
01066   gdb_assert (pointer_type (value1->type));
01067   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
01068 
01069   gen_scale (ax, aop_mul, value1->type);
01070   ax_simple (ax, aop_add);
01071   gen_extend (ax, value1->type);        /* Catch overflow.  */
01072   value->type = value1->type;
01073   value->kind = axs_rvalue;
01074 }
01075 
01076 
01077 /* Generate code for pointer arithmetic PTR - INT.  */
01078 static void
01079 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
01080             struct axs_value *value1, struct axs_value *value2)
01081 {
01082   gdb_assert (pointer_type (value1->type));
01083   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
01084 
01085   gen_scale (ax, aop_mul, value1->type);
01086   ax_simple (ax, aop_sub);
01087   gen_extend (ax, value1->type);        /* Catch overflow.  */
01088   value->type = value1->type;
01089   value->kind = axs_rvalue;
01090 }
01091 
01092 
01093 /* Generate code for pointer arithmetic PTR - PTR.  */
01094 static void
01095 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
01096              struct axs_value *value1, struct axs_value *value2,
01097              struct type *result_type)
01098 {
01099   gdb_assert (pointer_type (value1->type));
01100   gdb_assert (pointer_type (value2->type));
01101 
01102   if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
01103       != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
01104     error (_("\
01105 First argument of `-' is a pointer, but second argument is neither\n\
01106 an integer nor a pointer of the same type."));
01107 
01108   ax_simple (ax, aop_sub);
01109   gen_scale (ax, aop_div_unsigned, value1->type);
01110   value->type = result_type;
01111   value->kind = axs_rvalue;
01112 }
01113 
01114 static void
01115 gen_equal (struct agent_expr *ax, struct axs_value *value,
01116            struct axs_value *value1, struct axs_value *value2,
01117            struct type *result_type)
01118 {
01119   if (pointer_type (value1->type) || pointer_type (value2->type))
01120     ax_simple (ax, aop_equal);
01121   else
01122     gen_binop (ax, value, value1, value2,
01123                aop_equal, aop_equal, 0, "equal");
01124   value->type = result_type;
01125   value->kind = axs_rvalue;
01126 }
01127 
01128 static void
01129 gen_less (struct agent_expr *ax, struct axs_value *value,
01130           struct axs_value *value1, struct axs_value *value2,
01131           struct type *result_type)
01132 {
01133   if (pointer_type (value1->type) || pointer_type (value2->type))
01134     ax_simple (ax, aop_less_unsigned);
01135   else
01136     gen_binop (ax, value, value1, value2,
01137                aop_less_signed, aop_less_unsigned, 0, "less than");
01138   value->type = result_type;
01139   value->kind = axs_rvalue;
01140 }
01141 
01142 /* Generate code for a binary operator that doesn't do pointer magic.
01143    We set VALUE to describe the result value; we assume VALUE1 and
01144    VALUE2 describe the two operands, and that they've undergone the
01145    usual binary conversions.  MAY_CARRY should be non-zero iff the
01146    result needs to be extended.  NAME is the English name of the
01147    operator, used in error messages */
01148 static void
01149 gen_binop (struct agent_expr *ax, struct axs_value *value,
01150            struct axs_value *value1, struct axs_value *value2,
01151            enum agent_op op, enum agent_op op_unsigned,
01152            int may_carry, char *name)
01153 {
01154   /* We only handle INT op INT.  */
01155   if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
01156       || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
01157     error (_("Invalid combination of types in %s."), name);
01158 
01159   ax_simple (ax,
01160              TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
01161   if (may_carry)
01162     gen_extend (ax, value1->type);      /* catch overflow */
01163   value->type = value1->type;
01164   value->kind = axs_rvalue;
01165 }
01166 
01167 
01168 static void
01169 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
01170                  struct type *result_type)
01171 {
01172   if (TYPE_CODE (value->type) != TYPE_CODE_INT
01173       && TYPE_CODE (value->type) != TYPE_CODE_PTR)
01174     error (_("Invalid type of operand to `!'."));
01175 
01176   ax_simple (ax, aop_log_not);
01177   value->type = result_type;
01178 }
01179 
01180 
01181 static void
01182 gen_complement (struct agent_expr *ax, struct axs_value *value)
01183 {
01184   if (TYPE_CODE (value->type) != TYPE_CODE_INT)
01185     error (_("Invalid type of operand to `~'."));
01186 
01187   ax_simple (ax, aop_bit_not);
01188   gen_extend (ax, value->type);
01189 }
01190 
01191 
01192 
01193 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
01194 
01195 /* Dereference the value on the top of the stack.  */
01196 static void
01197 gen_deref (struct agent_expr *ax, struct axs_value *value)
01198 {
01199   /* The caller should check the type, because several operators use
01200      this, and we don't know what error message to generate.  */
01201   if (!pointer_type (value->type))
01202     internal_error (__FILE__, __LINE__,
01203                     _("gen_deref: expected a pointer"));
01204 
01205   /* We've got an rvalue now, which is a pointer.  We want to yield an
01206      lvalue, whose address is exactly that pointer.  So we don't
01207      actually emit any code; we just change the type from "Pointer to
01208      T" to "T", and mark the value as an lvalue in memory.  Leave it
01209      to the consumer to actually dereference it.  */
01210   value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
01211   if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
01212     error (_("Attempt to dereference a generic pointer."));
01213   value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
01214                  ? axs_rvalue : axs_lvalue_memory);
01215 }
01216 
01217 
01218 /* Produce the address of the lvalue on the top of the stack.  */
01219 static void
01220 gen_address_of (struct agent_expr *ax, struct axs_value *value)
01221 {
01222   /* Special case for taking the address of a function.  The ANSI
01223      standard describes this as a special case, too, so this
01224      arrangement is not without motivation.  */
01225   if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
01226     /* The value's already an rvalue on the stack, so we just need to
01227        change the type.  */
01228     value->type = lookup_pointer_type (value->type);
01229   else
01230     switch (value->kind)
01231       {
01232       case axs_rvalue:
01233         error (_("Operand of `&' is an rvalue, which has no address."));
01234 
01235       case axs_lvalue_register:
01236         error (_("Operand of `&' is in a register, and has no address."));
01237 
01238       case axs_lvalue_memory:
01239         value->kind = axs_rvalue;
01240         value->type = lookup_pointer_type (value->type);
01241         break;
01242       }
01243 }
01244 
01245 /* Generate code to push the value of a bitfield of a structure whose
01246    address is on the top of the stack.  START and END give the
01247    starting and one-past-ending *bit* numbers of the field within the
01248    structure.  */
01249 static void
01250 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
01251                   struct axs_value *value, struct type *type,
01252                   int start, int end)
01253 {
01254   /* Note that ops[i] fetches 8 << i bits.  */
01255   static enum agent_op ops[]
01256     = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
01257   static int num_ops = (sizeof (ops) / sizeof (ops[0]));
01258 
01259   /* We don't want to touch any byte that the bitfield doesn't
01260      actually occupy; we shouldn't make any accesses we're not
01261      explicitly permitted to.  We rely here on the fact that the
01262      bytecode `ref' operators work on unaligned addresses.
01263 
01264      It takes some fancy footwork to get the stack to work the way
01265      we'd like.  Say we're retrieving a bitfield that requires three
01266      fetches.  Initially, the stack just contains the address:
01267      addr
01268      For the first fetch, we duplicate the address
01269      addr addr
01270      then add the byte offset, do the fetch, and shift and mask as
01271      needed, yielding a fragment of the value, properly aligned for
01272      the final bitwise or:
01273      addr frag1
01274      then we swap, and repeat the process:
01275      frag1 addr                    --- address on top
01276      frag1 addr addr               --- duplicate it
01277      frag1 addr frag2              --- get second fragment
01278      frag1 frag2 addr              --- swap again
01279      frag1 frag2 frag3             --- get third fragment
01280      Notice that, since the third fragment is the last one, we don't
01281      bother duplicating the address this time.  Now we have all the
01282      fragments on the stack, and we can simply `or' them together,
01283      yielding the final value of the bitfield.  */
01284 
01285   /* The first and one-after-last bits in the field, but rounded down
01286      and up to byte boundaries.  */
01287   int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
01288   int bound_end = (((end + TARGET_CHAR_BIT - 1)
01289                     / TARGET_CHAR_BIT)
01290                    * TARGET_CHAR_BIT);
01291 
01292   /* current bit offset within the structure */
01293   int offset;
01294 
01295   /* The index in ops of the opcode we're considering.  */
01296   int op;
01297 
01298   /* The number of fragments we generated in the process.  Probably
01299      equal to the number of `one' bits in bytesize, but who cares?  */
01300   int fragment_count;
01301 
01302   /* Dereference any typedefs.  */
01303   type = check_typedef (type);
01304 
01305   /* Can we fetch the number of bits requested at all?  */
01306   if ((end - start) > ((1 << num_ops) * 8))
01307     internal_error (__FILE__, __LINE__,
01308                     _("gen_bitfield_ref: bitfield too wide"));
01309 
01310   /* Note that we know here that we only need to try each opcode once.
01311      That may not be true on machines with weird byte sizes.  */
01312   offset = bound_start;
01313   fragment_count = 0;
01314   for (op = num_ops - 1; op >= 0; op--)
01315     {
01316       /* number of bits that ops[op] would fetch */
01317       int op_size = 8 << op;
01318 
01319       /* The stack at this point, from bottom to top, contains zero or
01320          more fragments, then the address.  */
01321 
01322       /* Does this fetch fit within the bitfield?  */
01323       if (offset + op_size <= bound_end)
01324         {
01325           /* Is this the last fragment?  */
01326           int last_frag = (offset + op_size == bound_end);
01327 
01328           if (!last_frag)
01329             ax_simple (ax, aop_dup);    /* keep a copy of the address */
01330 
01331           /* Add the offset.  */
01332           gen_offset (ax, offset / TARGET_CHAR_BIT);
01333 
01334           if (ax->tracing)
01335             {
01336               /* Record the area of memory we're about to fetch.  */
01337               ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
01338             }
01339 
01340           /* Perform the fetch.  */
01341           ax_simple (ax, ops[op]);
01342 
01343           /* Shift the bits we have to their proper position.
01344              gen_left_shift will generate right shifts when the operand
01345              is negative.
01346 
01347              A big-endian field diagram to ponder:
01348              byte 0  byte 1  byte 2  byte 3  byte 4  byte 5  byte 6  byte 7
01349              +------++------++------++------++------++------++------++------+
01350              xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
01351              ^               ^               ^    ^
01352              bit number      16              32              48   53
01353              These are bit numbers as supplied by GDB.  Note that the
01354              bit numbers run from right to left once you've fetched the
01355              value!
01356 
01357              A little-endian field diagram to ponder:
01358              byte 7  byte 6  byte 5  byte 4  byte 3  byte 2  byte 1  byte 0
01359              +------++------++------++------++------++------++------++------+
01360              xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
01361              ^               ^               ^           ^   ^
01362              bit number     48              32              16          4   0
01363 
01364              In both cases, the most significant end is on the left
01365              (i.e. normal numeric writing order), which means that you
01366              don't go crazy thinking about `left' and `right' shifts.
01367 
01368              We don't have to worry about masking yet:
01369              - If they contain garbage off the least significant end, then we
01370              must be looking at the low end of the field, and the right
01371              shift will wipe them out.
01372              - If they contain garbage off the most significant end, then we
01373              must be looking at the most significant end of the word, and
01374              the sign/zero extension will wipe them out.
01375              - If we're in the interior of the word, then there is no garbage
01376              on either end, because the ref operators zero-extend.  */
01377           if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
01378             gen_left_shift (ax, end - (offset + op_size));
01379           else
01380             gen_left_shift (ax, offset - start);
01381 
01382           if (!last_frag)
01383             /* Bring the copy of the address up to the top.  */
01384             ax_simple (ax, aop_swap);
01385 
01386           offset += op_size;
01387           fragment_count++;
01388         }
01389     }
01390 
01391   /* Generate enough bitwise `or' operations to combine all the
01392      fragments we left on the stack.  */
01393   while (fragment_count-- > 1)
01394     ax_simple (ax, aop_bit_or);
01395 
01396   /* Sign- or zero-extend the value as appropriate.  */
01397   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
01398 
01399   /* This is *not* an lvalue.  Ugh.  */
01400   value->kind = axs_rvalue;
01401   value->type = type;
01402 }
01403 
01404 /* Generate bytecodes for field number FIELDNO of type TYPE.  OFFSET
01405    is an accumulated offset (in bytes), will be nonzero for objects
01406    embedded in other objects, like C++ base classes.  Behavior should
01407    generally follow value_primitive_field.  */
01408 
01409 static void
01410 gen_primitive_field (struct expression *exp,
01411                      struct agent_expr *ax, struct axs_value *value,
01412                      int offset, int fieldno, struct type *type)
01413 {
01414   /* Is this a bitfield?  */
01415   if (TYPE_FIELD_PACKED (type, fieldno))
01416     gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
01417                       (offset * TARGET_CHAR_BIT
01418                        + TYPE_FIELD_BITPOS (type, fieldno)),
01419                       (offset * TARGET_CHAR_BIT
01420                        + TYPE_FIELD_BITPOS (type, fieldno)
01421                        + TYPE_FIELD_BITSIZE (type, fieldno)));
01422   else
01423     {
01424       gen_offset (ax, offset
01425                   + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
01426       value->kind = axs_lvalue_memory;
01427       value->type = TYPE_FIELD_TYPE (type, fieldno);
01428     }
01429 }
01430 
01431 /* Search for the given field in either the given type or one of its
01432    base classes.  Return 1 if found, 0 if not.  */
01433 
01434 static int
01435 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
01436                           struct axs_value *value,
01437                           char *field, int offset, struct type *type)
01438 {
01439   int i, rslt;
01440   int nbases = TYPE_N_BASECLASSES (type);
01441 
01442   CHECK_TYPEDEF (type);
01443 
01444   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
01445     {
01446       const char *this_name = TYPE_FIELD_NAME (type, i);
01447 
01448       if (this_name)
01449         {
01450           if (strcmp (field, this_name) == 0)
01451             {
01452               /* Note that bytecodes for the struct's base (aka
01453                  "this") will have been generated already, which will
01454                  be unnecessary but not harmful if the static field is
01455                  being handled as a global.  */
01456               if (field_is_static (&TYPE_FIELD (type, i)))
01457                 {
01458                   gen_static_field (exp->gdbarch, ax, value, type, i);
01459                   if (value->optimized_out)
01460                     error (_("static field `%s' has been "
01461                              "optimized out, cannot use"),
01462                            field);
01463                   return 1;
01464                 }
01465 
01466               gen_primitive_field (exp, ax, value, offset, i, type);
01467               return 1;
01468             }
01469 #if 0 /* is this right? */
01470           if (this_name[0] == '\0')
01471             internal_error (__FILE__, __LINE__,
01472                             _("find_field: anonymous unions not supported"));
01473 #endif
01474         }
01475     }
01476 
01477   /* Now scan through base classes recursively.  */
01478   for (i = 0; i < nbases; i++)
01479     {
01480       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
01481 
01482       rslt = gen_struct_ref_recursive (exp, ax, value, field,
01483                                        offset + TYPE_BASECLASS_BITPOS (type, i)
01484                                        / TARGET_CHAR_BIT,
01485                                        basetype);
01486       if (rslt)
01487         return 1;
01488     }
01489 
01490   /* Not found anywhere, flag so caller can complain.  */
01491   return 0;
01492 }
01493 
01494 /* Generate code to reference the member named FIELD of a structure or
01495    union.  The top of the stack, as described by VALUE, should have
01496    type (pointer to a)* struct/union.  OPERATOR_NAME is the name of
01497    the operator being compiled, and OPERAND_NAME is the kind of thing
01498    it operates on; we use them in error messages.  */
01499 static void
01500 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
01501                 struct axs_value *value, char *field,
01502                 char *operator_name, char *operand_name)
01503 {
01504   struct type *type;
01505   int found;
01506 
01507   /* Follow pointers until we reach a non-pointer.  These aren't the C
01508      semantics, but they're what the normal GDB evaluator does, so we
01509      should at least be consistent.  */
01510   while (pointer_type (value->type))
01511     {
01512       require_rvalue (ax, value);
01513       gen_deref (ax, value);
01514     }
01515   type = check_typedef (value->type);
01516 
01517   /* This must yield a structure or a union.  */
01518   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
01519       && TYPE_CODE (type) != TYPE_CODE_UNION)
01520     error (_("The left operand of `%s' is not a %s."),
01521            operator_name, operand_name);
01522 
01523   /* And it must be in memory; we don't deal with structure rvalues,
01524      or structures living in registers.  */
01525   if (value->kind != axs_lvalue_memory)
01526     error (_("Structure does not live in memory."));
01527 
01528   /* Search through fields and base classes recursively.  */
01529   found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
01530   
01531   if (!found)
01532     error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
01533            field, TYPE_TAG_NAME (type));
01534 }
01535 
01536 static int
01537 gen_namespace_elt (struct expression *exp,
01538                    struct agent_expr *ax, struct axs_value *value,
01539                    const struct type *curtype, char *name);
01540 static int
01541 gen_maybe_namespace_elt (struct expression *exp,
01542                          struct agent_expr *ax, struct axs_value *value,
01543                          const struct type *curtype, char *name);
01544 
01545 static void
01546 gen_static_field (struct gdbarch *gdbarch,
01547                   struct agent_expr *ax, struct axs_value *value,
01548                   struct type *type, int fieldno)
01549 {
01550   if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
01551     {
01552       ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
01553       value->kind = axs_lvalue_memory;
01554       value->type = TYPE_FIELD_TYPE (type, fieldno);
01555       value->optimized_out = 0;
01556     }
01557   else
01558     {
01559       const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
01560       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
01561 
01562       if (sym)
01563         {
01564           gen_var_ref (gdbarch, ax, value, sym);
01565   
01566           /* Don't error if the value was optimized out, we may be
01567              scanning all static fields and just want to pass over this
01568              and continue with the rest.  */
01569         }
01570       else
01571         {
01572           /* Silently assume this was optimized out; class printing
01573              will let the user know why the data is missing.  */
01574           value->optimized_out = 1;
01575         }
01576     }
01577 }
01578 
01579 static int
01580 gen_struct_elt_for_reference (struct expression *exp,
01581                               struct agent_expr *ax, struct axs_value *value,
01582                               struct type *type, char *fieldname)
01583 {
01584   struct type *t = type;
01585   int i;
01586 
01587   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
01588       && TYPE_CODE (t) != TYPE_CODE_UNION)
01589     internal_error (__FILE__, __LINE__,
01590                     _("non-aggregate type to gen_struct_elt_for_reference"));
01591 
01592   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
01593     {
01594       const char *t_field_name = TYPE_FIELD_NAME (t, i);
01595 
01596       if (t_field_name && strcmp (t_field_name, fieldname) == 0)
01597         {
01598           if (field_is_static (&TYPE_FIELD (t, i)))
01599             {
01600               gen_static_field (exp->gdbarch, ax, value, t, i);
01601               if (value->optimized_out)
01602                 error (_("static field `%s' has been "
01603                          "optimized out, cannot use"),
01604                        fieldname);
01605               return 1;
01606             }
01607           if (TYPE_FIELD_PACKED (t, i))
01608             error (_("pointers to bitfield members not allowed"));
01609 
01610           /* FIXME we need a way to do "want_address" equivalent */       
01611 
01612           error (_("Cannot reference non-static field \"%s\""), fieldname);
01613         }
01614     }
01615 
01616   /* FIXME add other scoped-reference cases here */
01617 
01618   /* Do a last-ditch lookup.  */
01619   return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
01620 }
01621 
01622 /* C++: Return the member NAME of the namespace given by the type
01623    CURTYPE.  */
01624 
01625 static int
01626 gen_namespace_elt (struct expression *exp,
01627                    struct agent_expr *ax, struct axs_value *value,
01628                    const struct type *curtype, char *name)
01629 {
01630   int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
01631 
01632   if (!found)
01633     error (_("No symbol \"%s\" in namespace \"%s\"."), 
01634            name, TYPE_TAG_NAME (curtype));
01635 
01636   return found;
01637 }
01638 
01639 /* A helper function used by value_namespace_elt and
01640    value_struct_elt_for_reference.  It looks up NAME inside the
01641    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
01642    is a class and NAME refers to a type in CURTYPE itself (as opposed
01643    to, say, some base class of CURTYPE).  */
01644 
01645 static int
01646 gen_maybe_namespace_elt (struct expression *exp,
01647                          struct agent_expr *ax, struct axs_value *value,
01648                          const struct type *curtype, char *name)
01649 {
01650   const char *namespace_name = TYPE_TAG_NAME (curtype);
01651   struct symbol *sym;
01652 
01653   sym = cp_lookup_symbol_namespace (namespace_name, name,
01654                                     block_for_pc (ax->scope),
01655                                     VAR_DOMAIN);
01656 
01657   if (sym == NULL)
01658     return 0;
01659 
01660   gen_var_ref (exp->gdbarch, ax, value, sym);
01661 
01662   if (value->optimized_out)
01663     error (_("`%s' has been optimized out, cannot use"),
01664            SYMBOL_PRINT_NAME (sym));
01665 
01666   return 1;
01667 }
01668 
01669 
01670 static int
01671 gen_aggregate_elt_ref (struct expression *exp,
01672                        struct agent_expr *ax, struct axs_value *value,
01673                        struct type *type, char *field,
01674                        char *operator_name, char *operand_name)
01675 {
01676   switch (TYPE_CODE (type))
01677     {
01678     case TYPE_CODE_STRUCT:
01679     case TYPE_CODE_UNION:
01680       return gen_struct_elt_for_reference (exp, ax, value, type, field);
01681       break;
01682     case TYPE_CODE_NAMESPACE:
01683       return gen_namespace_elt (exp, ax, value, type, field);
01684       break;
01685     default:
01686       internal_error (__FILE__, __LINE__,
01687                       _("non-aggregate type in gen_aggregate_elt_ref"));
01688     }
01689 
01690   return 0;
01691 }
01692 
01693 /* Generate code for GDB's magical `repeat' operator.
01694    LVALUE @ INT creates an array INT elements long, and whose elements
01695    have the same type as LVALUE, located in memory so that LVALUE is
01696    its first element.  For example, argv[0]@argc gives you the array
01697    of command-line arguments.
01698 
01699    Unfortunately, because we have to know the types before we actually
01700    have a value for the expression, we can't implement this perfectly
01701    without changing the type system, having values that occupy two
01702    stack slots, doing weird things with sizeof, etc.  So we require
01703    the right operand to be a constant expression.  */
01704 static void
01705 gen_repeat (struct expression *exp, union exp_element **pc,
01706             struct agent_expr *ax, struct axs_value *value)
01707 {
01708   struct axs_value value1;
01709 
01710   /* We don't want to turn this into an rvalue, so no conversions
01711      here.  */
01712   gen_expr (exp, pc, ax, &value1);
01713   if (value1.kind != axs_lvalue_memory)
01714     error (_("Left operand of `@' must be an object in memory."));
01715 
01716   /* Evaluate the length; it had better be a constant.  */
01717   {
01718     struct value *v = const_expr (pc);
01719     int length;
01720 
01721     if (!v)
01722       error (_("Right operand of `@' must be a "
01723                "constant, in agent expressions."));
01724     if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
01725       error (_("Right operand of `@' must be an integer."));
01726     length = value_as_long (v);
01727     if (length <= 0)
01728       error (_("Right operand of `@' must be positive."));
01729 
01730     /* The top of the stack is already the address of the object, so
01731        all we need to do is frob the type of the lvalue.  */
01732     {
01733       /* FIXME-type-allocation: need a way to free this type when we are
01734          done with it.  */
01735       struct type *array
01736         = lookup_array_range_type (value1.type, 0, length - 1);
01737 
01738       value->kind = axs_lvalue_memory;
01739       value->type = array;
01740     }
01741   }
01742 }
01743 
01744 
01745 /* Emit code for the `sizeof' operator.
01746    *PC should point at the start of the operand expression; we advance it
01747    to the first instruction after the operand.  */
01748 static void
01749 gen_sizeof (struct expression *exp, union exp_element **pc,
01750             struct agent_expr *ax, struct axs_value *value,
01751             struct type *size_type)
01752 {
01753   /* We don't care about the value of the operand expression; we only
01754      care about its type.  However, in the current arrangement, the
01755      only way to find an expression's type is to generate code for it.
01756      So we generate code for the operand, and then throw it away,
01757      replacing it with code that simply pushes its size.  */
01758   int start = ax->len;
01759 
01760   gen_expr (exp, pc, ax, value);
01761 
01762   /* Throw away the code we just generated.  */
01763   ax->len = start;
01764 
01765   ax_const_l (ax, TYPE_LENGTH (value->type));
01766   value->kind = axs_rvalue;
01767   value->type = size_type;
01768 }
01769 
01770 
01771 /* Generating bytecode from GDB expressions: general recursive thingy  */
01772 
01773 /* XXX: i18n */
01774 /* A gen_expr function written by a Gen-X'er guy.
01775    Append code for the subexpression of EXPR starting at *POS_P to AX.  */
01776 void
01777 gen_expr (struct expression *exp, union exp_element **pc,
01778           struct agent_expr *ax, struct axs_value *value)
01779 {
01780   /* Used to hold the descriptions of operand expressions.  */
01781   struct axs_value value1, value2, value3;
01782   enum exp_opcode op = (*pc)[0].opcode, op2;
01783   int if1, go1, if2, go2, end;
01784   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
01785 
01786   /* If we're looking at a constant expression, just push its value.  */
01787   {
01788     struct value *v = maybe_const_expr (pc);
01789 
01790     if (v)
01791       {
01792         ax_const_l (ax, value_as_long (v));
01793         value->kind = axs_rvalue;
01794         value->type = check_typedef (value_type (v));
01795         return;
01796       }
01797   }
01798 
01799   /* Otherwise, go ahead and generate code for it.  */
01800   switch (op)
01801     {
01802       /* Binary arithmetic operators.  */
01803     case BINOP_ADD:
01804     case BINOP_SUB:
01805     case BINOP_MUL:
01806     case BINOP_DIV:
01807     case BINOP_REM:
01808     case BINOP_LSH:
01809     case BINOP_RSH:
01810     case BINOP_SUBSCRIPT:
01811     case BINOP_BITWISE_AND:
01812     case BINOP_BITWISE_IOR:
01813     case BINOP_BITWISE_XOR:
01814     case BINOP_EQUAL:
01815     case BINOP_NOTEQUAL:
01816     case BINOP_LESS:
01817     case BINOP_GTR:
01818     case BINOP_LEQ:
01819     case BINOP_GEQ:
01820       (*pc)++;
01821       gen_expr (exp, pc, ax, &value1);
01822       gen_usual_unary (exp, ax, &value1);
01823       gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
01824       break;
01825 
01826     case BINOP_LOGICAL_AND:
01827       (*pc)++;
01828       /* Generate the obvious sequence of tests and jumps.  */
01829       gen_expr (exp, pc, ax, &value1);
01830       gen_usual_unary (exp, ax, &value1);
01831       if1 = ax_goto (ax, aop_if_goto);
01832       go1 = ax_goto (ax, aop_goto);
01833       ax_label (ax, if1, ax->len);
01834       gen_expr (exp, pc, ax, &value2);
01835       gen_usual_unary (exp, ax, &value2);
01836       if2 = ax_goto (ax, aop_if_goto);
01837       go2 = ax_goto (ax, aop_goto);
01838       ax_label (ax, if2, ax->len);
01839       ax_const_l (ax, 1);
01840       end = ax_goto (ax, aop_goto);
01841       ax_label (ax, go1, ax->len);
01842       ax_label (ax, go2, ax->len);
01843       ax_const_l (ax, 0);
01844       ax_label (ax, end, ax->len);
01845       value->kind = axs_rvalue;
01846       value->type = int_type;
01847       break;
01848 
01849     case BINOP_LOGICAL_OR:
01850       (*pc)++;
01851       /* Generate the obvious sequence of tests and jumps.  */
01852       gen_expr (exp, pc, ax, &value1);
01853       gen_usual_unary (exp, ax, &value1);
01854       if1 = ax_goto (ax, aop_if_goto);
01855       gen_expr (exp, pc, ax, &value2);
01856       gen_usual_unary (exp, ax, &value2);
01857       if2 = ax_goto (ax, aop_if_goto);
01858       ax_const_l (ax, 0);
01859       end = ax_goto (ax, aop_goto);
01860       ax_label (ax, if1, ax->len);
01861       ax_label (ax, if2, ax->len);
01862       ax_const_l (ax, 1);
01863       ax_label (ax, end, ax->len);
01864       value->kind = axs_rvalue;
01865       value->type = int_type;
01866       break;
01867 
01868     case TERNOP_COND:
01869       (*pc)++;
01870       gen_expr (exp, pc, ax, &value1);
01871       gen_usual_unary (exp, ax, &value1);
01872       /* For (A ? B : C), it's easiest to generate subexpression
01873          bytecodes in order, but if_goto jumps on true, so we invert
01874          the sense of A.  Then we can do B by dropping through, and
01875          jump to do C.  */
01876       gen_logical_not (ax, &value1, int_type);
01877       if1 = ax_goto (ax, aop_if_goto);
01878       gen_expr (exp, pc, ax, &value2);
01879       gen_usual_unary (exp, ax, &value2);
01880       end = ax_goto (ax, aop_goto);
01881       ax_label (ax, if1, ax->len);
01882       gen_expr (exp, pc, ax, &value3);
01883       gen_usual_unary (exp, ax, &value3);
01884       ax_label (ax, end, ax->len);
01885       /* This is arbitary - what if B and C are incompatible types? */
01886       value->type = value2.type;
01887       value->kind = value2.kind;
01888       break;
01889 
01890     case BINOP_ASSIGN:
01891       (*pc)++;
01892       if ((*pc)[0].opcode == OP_INTERNALVAR)
01893         {
01894           char *name = internalvar_name ((*pc)[1].internalvar);
01895           struct trace_state_variable *tsv;
01896 
01897           (*pc) += 3;
01898           gen_expr (exp, pc, ax, value);
01899           tsv = find_trace_state_variable (name);
01900           if (tsv)
01901             {
01902               ax_tsv (ax, aop_setv, tsv->number);
01903               if (ax->tracing)
01904                 ax_tsv (ax, aop_tracev, tsv->number);
01905             }
01906           else
01907             error (_("$%s is not a trace state variable, "
01908                      "may not assign to it"), name);
01909         }
01910       else
01911         error (_("May only assign to trace state variables"));
01912       break;
01913 
01914     case BINOP_ASSIGN_MODIFY:
01915       (*pc)++;
01916       op2 = (*pc)[0].opcode;
01917       (*pc)++;
01918       (*pc)++;
01919       if ((*pc)[0].opcode == OP_INTERNALVAR)
01920         {
01921           char *name = internalvar_name ((*pc)[1].internalvar);
01922           struct trace_state_variable *tsv;
01923 
01924           (*pc) += 3;
01925           tsv = find_trace_state_variable (name);
01926           if (tsv)
01927             {
01928               /* The tsv will be the left half of the binary operation.  */
01929               ax_tsv (ax, aop_getv, tsv->number);
01930               if (ax->tracing)
01931                 ax_tsv (ax, aop_tracev, tsv->number);
01932               /* Trace state variables are always 64-bit integers.  */
01933               value1.kind = axs_rvalue;
01934               value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
01935               /* Now do right half of expression.  */
01936               gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
01937               /* We have a result of the binary op, set the tsv.  */
01938               ax_tsv (ax, aop_setv, tsv->number);
01939               if (ax->tracing)
01940                 ax_tsv (ax, aop_tracev, tsv->number);
01941             }
01942           else
01943             error (_("$%s is not a trace state variable, "
01944                      "may not assign to it"), name);
01945         }
01946       else
01947         error (_("May only assign to trace state variables"));
01948       break;
01949 
01950       /* Note that we need to be a little subtle about generating code
01951          for comma.  In C, we can do some optimizations here because
01952          we know the left operand is only being evaluated for effect.
01953          However, if the tracing kludge is in effect, then we always
01954          need to evaluate the left hand side fully, so that all the
01955          variables it mentions get traced.  */
01956     case BINOP_COMMA:
01957       (*pc)++;
01958       gen_expr (exp, pc, ax, &value1);
01959       /* Don't just dispose of the left operand.  We might be tracing,
01960          in which case we want to emit code to trace it if it's an
01961          lvalue.  */
01962       gen_traced_pop (exp->gdbarch, ax, &value1);
01963       gen_expr (exp, pc, ax, value);
01964       /* It's the consumer's responsibility to trace the right operand.  */
01965       break;
01966 
01967     case OP_LONG:               /* some integer constant */
01968       {
01969         struct type *type = (*pc)[1].type;
01970         LONGEST k = (*pc)[2].longconst;
01971 
01972         (*pc) += 4;
01973         gen_int_literal (ax, value, k, type);
01974       }
01975       break;
01976 
01977     case OP_VAR_VALUE:
01978       gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
01979 
01980       if (value->optimized_out)
01981         error (_("`%s' has been optimized out, cannot use"),
01982                SYMBOL_PRINT_NAME ((*pc)[2].symbol));
01983 
01984       (*pc) += 4;
01985       break;
01986 
01987     case OP_REGISTER:
01988       {
01989         const char *name = &(*pc)[2].string;
01990         int reg;
01991 
01992         (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
01993         reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
01994         if (reg == -1)
01995           internal_error (__FILE__, __LINE__,
01996                           _("Register $%s not available"), name);
01997         /* No support for tracing user registers yet.  */
01998         if (reg >= gdbarch_num_regs (exp->gdbarch)
01999             + gdbarch_num_pseudo_regs (exp->gdbarch))
02000           error (_("'%s' is a user-register; "
02001                    "GDB cannot yet trace user-register contents."),
02002                  name);
02003         value->kind = axs_lvalue_register;
02004         value->u.reg = reg;
02005         value->type = register_type (exp->gdbarch, reg);
02006       }
02007       break;
02008 
02009     case OP_INTERNALVAR:
02010       {
02011         struct internalvar *var = (*pc)[1].internalvar;
02012         const char *name = internalvar_name (var);
02013         struct trace_state_variable *tsv;
02014 
02015         (*pc) += 3;
02016         tsv = find_trace_state_variable (name);
02017         if (tsv)
02018           {
02019             ax_tsv (ax, aop_getv, tsv->number);
02020             if (ax->tracing)
02021               ax_tsv (ax, aop_tracev, tsv->number);
02022             /* Trace state variables are always 64-bit integers.  */
02023             value->kind = axs_rvalue;
02024             value->type = builtin_type (exp->gdbarch)->builtin_long_long;
02025           }
02026         else if (! compile_internalvar_to_ax (var, ax, value))
02027           error (_("$%s is not a trace state variable; GDB agent "
02028                    "expressions cannot use convenience variables."), name);
02029       }
02030       break;
02031 
02032       /* Weirdo operator: see comments for gen_repeat for details.  */
02033     case BINOP_REPEAT:
02034       /* Note that gen_repeat handles its own argument evaluation.  */
02035       (*pc)++;
02036       gen_repeat (exp, pc, ax, value);
02037       break;
02038 
02039     case UNOP_CAST:
02040       {
02041         struct type *type = (*pc)[1].type;
02042 
02043         (*pc) += 3;
02044         gen_expr (exp, pc, ax, value);
02045         gen_cast (ax, value, type);
02046       }
02047       break;
02048 
02049     case UNOP_CAST_TYPE:
02050       {
02051         int offset;
02052         struct value *val;
02053         struct type *type;
02054 
02055         ++*pc;
02056         offset = *pc - exp->elts;
02057         val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
02058         type = value_type (val);
02059         *pc = &exp->elts[offset];
02060 
02061         gen_expr (exp, pc, ax, value);
02062         gen_cast (ax, value, type);
02063       }
02064       break;
02065 
02066     case UNOP_MEMVAL:
02067       {
02068         struct type *type = check_typedef ((*pc)[1].type);
02069 
02070         (*pc) += 3;
02071         gen_expr (exp, pc, ax, value);
02072 
02073         /* If we have an axs_rvalue or an axs_lvalue_memory, then we
02074            already have the right value on the stack.  For
02075            axs_lvalue_register, we must convert.  */
02076         if (value->kind == axs_lvalue_register)
02077           require_rvalue (ax, value);
02078 
02079         value->type = type;
02080         value->kind = axs_lvalue_memory;
02081       }
02082       break;
02083 
02084     case UNOP_MEMVAL_TYPE:
02085       {
02086         int offset;
02087         struct value *val;
02088         struct type *type;
02089 
02090         ++*pc;
02091         offset = *pc - exp->elts;
02092         val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
02093         type = value_type (val);
02094         *pc = &exp->elts[offset];
02095 
02096         gen_expr (exp, pc, ax, value);
02097 
02098         /* If we have an axs_rvalue or an axs_lvalue_memory, then we
02099            already have the right value on the stack.  For
02100            axs_lvalue_register, we must convert.  */
02101         if (value->kind == axs_lvalue_register)
02102           require_rvalue (ax, value);
02103 
02104         value->type = type;
02105         value->kind = axs_lvalue_memory;
02106       }
02107       break;
02108 
02109     case UNOP_PLUS:
02110       (*pc)++;
02111       /* + FOO is equivalent to 0 + FOO, which can be optimized.  */
02112       gen_expr (exp, pc, ax, value);
02113       gen_usual_unary (exp, ax, value);
02114       break;
02115       
02116     case UNOP_NEG:
02117       (*pc)++;
02118       /* -FOO is equivalent to 0 - FOO.  */
02119       gen_int_literal (ax, &value1, 0,
02120                        builtin_type (exp->gdbarch)->builtin_int);
02121       gen_usual_unary (exp, ax, &value1);       /* shouldn't do much */
02122       gen_expr (exp, pc, ax, &value2);
02123       gen_usual_unary (exp, ax, &value2);
02124       gen_usual_arithmetic (exp, ax, &value1, &value2);
02125       gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
02126       break;
02127 
02128     case UNOP_LOGICAL_NOT:
02129       (*pc)++;
02130       gen_expr (exp, pc, ax, value);
02131       gen_usual_unary (exp, ax, value);
02132       gen_logical_not (ax, value, int_type);
02133       break;
02134 
02135     case UNOP_COMPLEMENT:
02136       (*pc)++;
02137       gen_expr (exp, pc, ax, value);
02138       gen_usual_unary (exp, ax, value);
02139       gen_integral_promotions (exp, ax, value);
02140       gen_complement (ax, value);
02141       break;
02142 
02143     case UNOP_IND:
02144       (*pc)++;
02145       gen_expr (exp, pc, ax, value);
02146       gen_usual_unary (exp, ax, value);
02147       if (!pointer_type (value->type))
02148         error (_("Argument of unary `*' is not a pointer."));
02149       gen_deref (ax, value);
02150       break;
02151 
02152     case UNOP_ADDR:
02153       (*pc)++;
02154       gen_expr (exp, pc, ax, value);
02155       gen_address_of (ax, value);
02156       break;
02157 
02158     case UNOP_SIZEOF:
02159       (*pc)++;
02160       /* Notice that gen_sizeof handles its own operand, unlike most
02161          of the other unary operator functions.  This is because we
02162          have to throw away the code we generate.  */
02163       gen_sizeof (exp, pc, ax, value,
02164                   builtin_type (exp->gdbarch)->builtin_int);
02165       break;
02166 
02167     case STRUCTOP_STRUCT:
02168     case STRUCTOP_PTR:
02169       {
02170         int length = (*pc)[1].longconst;
02171         char *name = &(*pc)[2].string;
02172 
02173         (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
02174         gen_expr (exp, pc, ax, value);
02175         if (op == STRUCTOP_STRUCT)
02176           gen_struct_ref (exp, ax, value, name, ".", "structure or union");
02177         else if (op == STRUCTOP_PTR)
02178           gen_struct_ref (exp, ax, value, name, "->",
02179                           "pointer to a structure or union");
02180         else
02181           /* If this `if' chain doesn't handle it, then the case list
02182              shouldn't mention it, and we shouldn't be here.  */
02183           internal_error (__FILE__, __LINE__,
02184                           _("gen_expr: unhandled struct case"));
02185       }
02186       break;
02187 
02188     case OP_THIS:
02189       {
02190         struct symbol *sym, *func;
02191         struct block *b;
02192         const struct language_defn *lang;
02193 
02194         b = block_for_pc (ax->scope);
02195         func = block_linkage_function (b);
02196         lang = language_def (SYMBOL_LANGUAGE (func));
02197 
02198         sym = lookup_language_this (lang, b);
02199         if (!sym)
02200           error (_("no `%s' found"), lang->la_name_of_this);
02201 
02202         gen_var_ref (exp->gdbarch, ax, value, sym);
02203 
02204         if (value->optimized_out)
02205           error (_("`%s' has been optimized out, cannot use"),
02206                  SYMBOL_PRINT_NAME (sym));
02207 
02208         (*pc) += 2;
02209       }
02210       break;
02211 
02212     case OP_SCOPE:
02213       {
02214         struct type *type = (*pc)[1].type;
02215         int length = longest_to_int ((*pc)[2].longconst);
02216         char *name = &(*pc)[3].string;
02217         int found;
02218 
02219         found = gen_aggregate_elt_ref (exp, ax, value, type, name,
02220                                        "?", "??");
02221         if (!found)
02222           error (_("There is no field named %s"), name);
02223         (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
02224       }
02225       break;
02226 
02227     case OP_TYPE:
02228     case OP_TYPEOF:
02229     case OP_DECLTYPE:
02230       error (_("Attempt to use a type name as an expression."));
02231 
02232     default:
02233       error (_("Unsupported operator %s (%d) in expression."),
02234              op_name (exp, op), op);
02235     }
02236 }
02237 
02238 /* This handles the middle-to-right-side of code generation for binary
02239    expressions, which is shared between regular binary operations and
02240    assign-modify (+= and friends) expressions.  */
02241 
02242 static void
02243 gen_expr_binop_rest (struct expression *exp,
02244                      enum exp_opcode op, union exp_element **pc,
02245                      struct agent_expr *ax, struct axs_value *value,
02246                      struct axs_value *value1, struct axs_value *value2)
02247 {
02248   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
02249 
02250   gen_expr (exp, pc, ax, value2);
02251   gen_usual_unary (exp, ax, value2);
02252   gen_usual_arithmetic (exp, ax, value1, value2);
02253   switch (op)
02254     {
02255     case BINOP_ADD:
02256       if (TYPE_CODE (value1->type) == TYPE_CODE_INT
02257           && pointer_type (value2->type))
02258         {
02259           /* Swap the values and proceed normally.  */
02260           ax_simple (ax, aop_swap);
02261           gen_ptradd (ax, value, value2, value1);
02262         }
02263       else if (pointer_type (value1->type)
02264                && TYPE_CODE (value2->type) == TYPE_CODE_INT)
02265         gen_ptradd (ax, value, value1, value2);
02266       else
02267         gen_binop (ax, value, value1, value2,
02268                    aop_add, aop_add, 1, "addition");
02269       break;
02270     case BINOP_SUB:
02271       if (pointer_type (value1->type)
02272           && TYPE_CODE (value2->type) == TYPE_CODE_INT)
02273         gen_ptrsub (ax,value, value1, value2);
02274       else if (pointer_type (value1->type)
02275                && pointer_type (value2->type))
02276         /* FIXME --- result type should be ptrdiff_t */
02277         gen_ptrdiff (ax, value, value1, value2,
02278                      builtin_type (exp->gdbarch)->builtin_long);
02279       else
02280         gen_binop (ax, value, value1, value2,
02281                    aop_sub, aop_sub, 1, "subtraction");
02282       break;
02283     case BINOP_MUL:
02284       gen_binop (ax, value, value1, value2,
02285                  aop_mul, aop_mul, 1, "multiplication");
02286       break;
02287     case BINOP_DIV:
02288       gen_binop (ax, value, value1, value2,
02289                  aop_div_signed, aop_div_unsigned, 1, "division");
02290       break;
02291     case BINOP_REM:
02292       gen_binop (ax, value, value1, value2,
02293                  aop_rem_signed, aop_rem_unsigned, 1, "remainder");
02294       break;
02295     case BINOP_LSH:
02296       gen_binop (ax, value, value1, value2,
02297                  aop_lsh, aop_lsh, 1, "left shift");
02298       break;
02299     case BINOP_RSH:
02300       gen_binop (ax, value, value1, value2,
02301                  aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
02302       break;
02303     case BINOP_SUBSCRIPT:
02304       {
02305         struct type *type;
02306 
02307         if (binop_types_user_defined_p (op, value1->type, value2->type))
02308           {
02309             error (_("cannot subscript requested type: "
02310                      "cannot call user defined functions"));
02311           }
02312         else
02313           {
02314             /* If the user attempts to subscript something that is not
02315                an array or pointer type (like a plain int variable for
02316                example), then report this as an error.  */
02317             type = check_typedef (value1->type);
02318             if (TYPE_CODE (type) != TYPE_CODE_ARRAY
02319                 && TYPE_CODE (type) != TYPE_CODE_PTR)
02320               {
02321                 if (TYPE_NAME (type))
02322                   error (_("cannot subscript something of type `%s'"),
02323                          TYPE_NAME (type));
02324                 else
02325                   error (_("cannot subscript requested type"));
02326               }
02327           }
02328 
02329         if (!is_integral_type (value2->type))
02330           error (_("Argument to arithmetic operation "
02331                    "not a number or boolean."));
02332 
02333         gen_ptradd (ax, value, value1, value2);
02334         gen_deref (ax, value);
02335         break;
02336       }
02337     case BINOP_BITWISE_AND:
02338       gen_binop (ax, value, value1, value2,
02339                  aop_bit_and, aop_bit_and, 0, "bitwise and");
02340       break;
02341 
02342     case BINOP_BITWISE_IOR:
02343       gen_binop (ax, value, value1, value2,
02344                  aop_bit_or, aop_bit_or, 0, "bitwise or");
02345       break;
02346       
02347     case BINOP_BITWISE_XOR:
02348       gen_binop (ax, value, value1, value2,
02349                  aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
02350       break;
02351 
02352     case BINOP_EQUAL:
02353       gen_equal (ax, value, value1, value2, int_type);
02354       break;
02355 
02356     case BINOP_NOTEQUAL:
02357       gen_equal (ax, value, value1, value2, int_type);
02358       gen_logical_not (ax, value, int_type);
02359       break;
02360 
02361     case BINOP_LESS:
02362       gen_less (ax, value, value1, value2, int_type);
02363       break;
02364 
02365     case BINOP_GTR:
02366       ax_simple (ax, aop_swap);
02367       gen_less (ax, value, value1, value2, int_type);
02368       break;
02369 
02370     case BINOP_LEQ:
02371       ax_simple (ax, aop_swap);
02372       gen_less (ax, value, value1, value2, int_type);
02373       gen_logical_not (ax, value, int_type);
02374       break;
02375 
02376     case BINOP_GEQ:
02377       gen_less (ax, value, value1, value2, int_type);
02378       gen_logical_not (ax, value, int_type);
02379       break;
02380 
02381     default:
02382       /* We should only list operators in the outer case statement
02383          that we actually handle in the inner case statement.  */
02384       internal_error (__FILE__, __LINE__,
02385                       _("gen_expr: op case sets don't match"));
02386     }
02387 }
02388 
02389 
02390 /* Given a single variable and a scope, generate bytecodes to trace
02391    its value.  This is for use in situations where we have only a
02392    variable's name, and no parsed expression; for instance, when the
02393    name comes from a list of local variables of a function.  */
02394 
02395 struct agent_expr *
02396 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
02397                    struct symbol *var, int trace_string)
02398 {
02399   struct cleanup *old_chain = 0;
02400   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
02401   struct axs_value value;
02402 
02403   old_chain = make_cleanup_free_agent_expr (ax);
02404 
02405   ax->tracing = 1;
02406   ax->trace_string = trace_string;
02407   gen_var_ref (gdbarch, ax, &value, var);
02408 
02409   /* If there is no actual variable to trace, flag it by returning
02410      an empty agent expression.  */
02411   if (value.optimized_out)
02412     {
02413       do_cleanups (old_chain);
02414       return NULL;
02415     }
02416 
02417   /* Make sure we record the final object, and get rid of it.  */
02418   gen_traced_pop (gdbarch, ax, &value);
02419 
02420   /* Oh, and terminate.  */
02421   ax_simple (ax, aop_end);
02422 
02423   /* We have successfully built the agent expr, so cancel the cleanup
02424      request.  If we add more cleanups that we always want done, this
02425      will have to get more complicated.  */
02426   discard_cleanups (old_chain);
02427   return ax;
02428 }
02429 
02430 /* Generating bytecode from GDB expressions: driver */
02431 
02432 /* Given a GDB expression EXPR, return bytecode to trace its value.
02433    The result will use the `trace' and `trace_quick' bytecodes to
02434    record the value of all memory touched by the expression.  The
02435    caller can then use the ax_reqs function to discover which
02436    registers it relies upon.  */
02437 struct agent_expr *
02438 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
02439                     int trace_string)
02440 {
02441   struct cleanup *old_chain = 0;
02442   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
02443   union exp_element *pc;
02444   struct axs_value value;
02445 
02446   old_chain = make_cleanup_free_agent_expr (ax);
02447 
02448   pc = expr->elts;
02449   ax->tracing = 1;
02450   ax->trace_string = trace_string;
02451   value.optimized_out = 0;
02452   gen_expr (expr, &pc, ax, &value);
02453 
02454   /* Make sure we record the final object, and get rid of it.  */
02455   gen_traced_pop (expr->gdbarch, ax, &value);
02456 
02457   /* Oh, and terminate.  */
02458   ax_simple (ax, aop_end);
02459 
02460   /* We have successfully built the agent expr, so cancel the cleanup
02461      request.  If we add more cleanups that we always want done, this
02462      will have to get more complicated.  */
02463   discard_cleanups (old_chain);
02464   return ax;
02465 }
02466 
02467 /* Given a GDB expression EXPR, return a bytecode sequence that will
02468    evaluate and return a result.  The bytecodes will do a direct
02469    evaluation, using the current data on the target, rather than
02470    recording blocks of memory and registers for later use, as
02471    gen_trace_for_expr does.  The generated bytecode sequence leaves
02472    the result of expression evaluation on the top of the stack.  */
02473 
02474 struct agent_expr *
02475 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
02476 {
02477   struct cleanup *old_chain = 0;
02478   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
02479   union exp_element *pc;
02480   struct axs_value value;
02481 
02482   old_chain = make_cleanup_free_agent_expr (ax);
02483 
02484   pc = expr->elts;
02485   ax->tracing = 0;
02486   value.optimized_out = 0;
02487   gen_expr (expr, &pc, ax, &value);
02488 
02489   require_rvalue (ax, &value);
02490 
02491   /* Oh, and terminate.  */
02492   ax_simple (ax, aop_end);
02493 
02494   /* We have successfully built the agent expr, so cancel the cleanup
02495      request.  If we add more cleanups that we always want done, this
02496      will have to get more complicated.  */
02497   discard_cleanups (old_chain);
02498   return ax;
02499 }
02500 
02501 struct agent_expr *
02502 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
02503                               int trace_string)
02504 {
02505   struct cleanup *old_chain = 0;
02506   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
02507   struct axs_value value;
02508 
02509   old_chain = make_cleanup_free_agent_expr (ax);
02510 
02511   ax->tracing = 1;
02512   ax->trace_string = trace_string;
02513 
02514   gdbarch_gen_return_address (gdbarch, ax, &value, scope);
02515 
02516   /* Make sure we record the final object, and get rid of it.  */
02517   gen_traced_pop (gdbarch, ax, &value);
02518 
02519   /* Oh, and terminate.  */
02520   ax_simple (ax, aop_end);
02521 
02522   /* We have successfully built the agent expr, so cancel the cleanup
02523      request.  If we add more cleanups that we always want done, this
02524      will have to get more complicated.  */
02525   discard_cleanups (old_chain);
02526   return ax;
02527 }
02528 
02529 /* Given a collection of printf-style arguments, generate code to
02530    evaluate the arguments and pass everything to a special
02531    bytecode.  */
02532 
02533 struct agent_expr *
02534 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
02535             CORE_ADDR function, LONGEST channel,
02536             const char *format, int fmtlen,
02537             struct format_piece *frags,
02538             int nargs, struct expression **exprs)
02539 {
02540   struct cleanup *old_chain = 0;
02541   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
02542   union exp_element *pc;
02543   struct axs_value value;
02544   int tem;
02545 
02546   old_chain = make_cleanup_free_agent_expr (ax);
02547 
02548   /* We're computing values, not doing side effects.  */
02549   ax->tracing = 0;
02550 
02551   /* Evaluate and push the args on the stack in reverse order,
02552      for simplicity of collecting them on the target side.  */
02553   for (tem = nargs - 1; tem >= 0; --tem)
02554     {
02555       pc = exprs[tem]->elts;
02556       value.optimized_out = 0;
02557       gen_expr (exprs[tem], &pc, ax, &value);
02558       require_rvalue (ax, &value);
02559     }
02560 
02561   /* Push function and channel.  */
02562   ax_const_l (ax, channel);
02563   ax_const_l (ax, function);
02564 
02565   /* Issue the printf bytecode proper.  */
02566   ax_simple (ax, aop_printf);
02567   ax_simple (ax, nargs);
02568   ax_string (ax, format, fmtlen);
02569 
02570   /* And terminate.  */
02571   ax_simple (ax, aop_end);
02572 
02573   /* We have successfully built the agent expr, so cancel the cleanup
02574      request.  If we add more cleanups that we always want done, this
02575      will have to get more complicated.  */
02576   discard_cleanups (old_chain);
02577 
02578   return ax;
02579 }
02580 
02581 static void
02582 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
02583 {
02584   struct cleanup *old_chain = 0;
02585   struct expression *expr;
02586   struct agent_expr *agent;
02587   const char *arg;
02588   int trace_string = 0;
02589 
02590   if (!eval)
02591     {
02592       if (*exp == '/')
02593         exp = decode_agent_options (exp, &trace_string);
02594     }
02595 
02596   arg = exp;
02597   if (!eval && strcmp (arg, "$_ret") == 0)
02598     {
02599       agent = gen_trace_for_return_address (pc, get_current_arch (),
02600                                             trace_string);
02601       old_chain = make_cleanup_free_agent_expr (agent);
02602     }
02603   else
02604     {
02605       expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
02606       old_chain = make_cleanup (free_current_contents, &expr);
02607       if (eval)
02608         {
02609           gdb_assert (trace_string == 0);
02610           agent = gen_eval_for_expr (pc, expr);
02611         }
02612       else
02613         agent = gen_trace_for_expr (pc, expr, trace_string);
02614       make_cleanup_free_agent_expr (agent);
02615     }
02616 
02617   ax_reqs (agent);
02618   ax_print (gdb_stdout, agent);
02619 
02620   /* It would be nice to call ax_reqs here to gather some general info
02621      about the expression, and then print out the result.  */
02622 
02623   do_cleanups (old_chain);
02624   dont_repeat ();
02625 }
02626 
02627 static void
02628 agent_command_1 (char *exp, int eval)
02629 {
02630   /* We don't deal with overlay debugging at the moment.  We need to
02631      think more carefully about this.  If you copy this code into
02632      another command, change the error message; the user shouldn't
02633      have to know anything about agent expressions.  */
02634   if (overlay_debugging)
02635     error (_("GDB can't do agent expression translation with overlays."));
02636 
02637   if (exp == 0)
02638     error_no_arg (_("expression to translate"));
02639 
02640   if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
02641     {
02642       struct linespec_result canonical;
02643       int ix;
02644       struct linespec_sals *iter;
02645       struct cleanup *old_chain;
02646 
02647       exp = skip_spaces (exp);
02648       init_linespec_result (&canonical);
02649       decode_line_full (&exp, DECODE_LINE_FUNFIRSTLINE,
02650                         (struct symtab *) NULL, 0, &canonical,
02651                         NULL, NULL);
02652       old_chain = make_cleanup_destroy_linespec_result (&canonical);
02653       exp = skip_spaces (exp);
02654       if (exp[0] == ',')
02655         {
02656           exp++;
02657           exp = skip_spaces (exp);
02658         }
02659       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
02660         {
02661           int i;
02662 
02663           for (i = 0; i < iter->sals.nelts; i++)
02664             agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
02665         }
02666       do_cleanups (old_chain);
02667     }
02668   else
02669     agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
02670 
02671   dont_repeat ();
02672 }
02673 
02674 static void
02675 agent_command (char *exp, int from_tty)
02676 {
02677   agent_command_1 (exp, 0);
02678 }
02679 
02680 /* Parse the given expression, compile it into an agent expression
02681    that does direct evaluation, and display the resulting
02682    expression.  */
02683 
02684 static void
02685 agent_eval_command (char *exp, int from_tty)
02686 {
02687   agent_command_1 (exp, 1);
02688 }
02689 
02690 /* Parse the given expression, compile it into an agent expression
02691    that does a printf, and display the resulting expression.  */
02692 
02693 static void
02694 maint_agent_printf_command (char *exp, int from_tty)
02695 {
02696   struct cleanup *old_chain = 0;
02697   struct expression *expr;
02698   struct expression *argvec[100];
02699   struct agent_expr *agent;
02700   struct frame_info *fi = get_current_frame (); /* need current scope */
02701   const char *cmdrest;
02702   const char *format_start, *format_end;
02703   struct format_piece *fpieces;
02704   int nargs;
02705 
02706   /* We don't deal with overlay debugging at the moment.  We need to
02707      think more carefully about this.  If you copy this code into
02708      another command, change the error message; the user shouldn't
02709      have to know anything about agent expressions.  */
02710   if (overlay_debugging)
02711     error (_("GDB can't do agent expression translation with overlays."));
02712 
02713   if (exp == 0)
02714     error_no_arg (_("expression to translate"));
02715 
02716   cmdrest = exp;
02717 
02718   cmdrest = skip_spaces_const (cmdrest);
02719 
02720   if (*cmdrest++ != '"')
02721     error (_("Must start with a format string."));
02722 
02723   format_start = cmdrest;
02724 
02725   fpieces = parse_format_string (&cmdrest);
02726 
02727   old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
02728 
02729   format_end = cmdrest;
02730 
02731   if (*cmdrest++ != '"')
02732     error (_("Bad format string, non-terminated '\"'."));
02733   
02734   cmdrest = skip_spaces_const (cmdrest);
02735 
02736   if (*cmdrest != ',' && *cmdrest != 0)
02737     error (_("Invalid argument syntax"));
02738 
02739   if (*cmdrest == ',')
02740     cmdrest++;
02741   cmdrest = skip_spaces_const (cmdrest);
02742 
02743   nargs = 0;
02744   while (*cmdrest != '\0')
02745     {
02746       const char *cmd1;
02747 
02748       cmd1 = cmdrest;
02749       expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
02750       argvec[nargs] = expr;
02751       ++nargs;
02752       cmdrest = cmd1;
02753       if (*cmdrest == ',')
02754         ++cmdrest;
02755       /* else complain? */
02756     }
02757 
02758 
02759   agent = gen_printf (get_frame_pc (fi), get_current_arch (), 0, 0,
02760                       format_start, format_end - format_start,
02761                       fpieces, nargs, argvec);
02762   make_cleanup_free_agent_expr (agent);
02763   ax_reqs (agent);
02764   ax_print (gdb_stdout, agent);
02765 
02766   /* It would be nice to call ax_reqs here to gather some general info
02767      about the expression, and then print out the result.  */
02768 
02769   do_cleanups (old_chain);
02770   dont_repeat ();
02771 }
02772 
02773 
02774 /* Initialization code.  */
02775 
02776 void _initialize_ax_gdb (void);
02777 void
02778 _initialize_ax_gdb (void)
02779 {
02780   add_cmd ("agent", class_maintenance, agent_command,
02781            _("\
02782 Translate an expression into remote agent bytecode for tracing.\n\
02783 Usage: maint agent [-at location,] EXPRESSION\n\
02784 If -at is given, generate remote agent bytecode for this location.\n\
02785 If not, generate remote agent bytecode for current frame pc address."),
02786            &maintenancelist);
02787 
02788   add_cmd ("agent-eval", class_maintenance, agent_eval_command,
02789            _("\
02790 Translate an expression into remote agent bytecode for evaluation.\n\
02791 Usage: maint agent-eval [-at location,] EXPRESSION\n\
02792 If -at is given, generate remote agent bytecode for this location.\n\
02793 If not, generate remote agent bytecode for current frame pc address."),
02794            &maintenancelist);
02795 
02796   add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
02797            _("Translate an expression into remote "
02798              "agent bytecode for evaluation and display the bytecodes."),
02799            &maintenancelist);
02800 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines