]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ax-gdb.c
* ax-general.c (gen_traced_pop, gen_int_literal)
[thirdparty/binutils-gdb.git] / gdb / ax-gdb.c
CommitLineData
1bac305b
AC
1/* GDB-specific functions for operating on agent expressions.
2
6aba47ca
DJ
3 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2007
4 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 20
c906108c
SS
21#include "defs.h"
22#include "symtab.h"
23#include "symfile.h"
24#include "gdbtypes.h"
25#include "value.h"
26#include "expression.h"
27#include "command.h"
28#include "gdbcmd.h"
29#include "frame.h"
30#include "target.h"
31#include "ax.h"
32#include "ax-gdb.h"
309367d4 33#include "gdb_string.h"
fe898f56 34#include "block.h"
7b83296f 35#include "regcache.h"
c906108c 36
6426a772
JM
37/* To make sense of this file, you should read doc/agentexpr.texi.
38 Then look at the types and enums in ax-gdb.h. For the code itself,
39 look at gen_expr, towards the bottom; that's the main function that
40 looks at the GDB expressions and calls everything else to generate
41 code.
c906108c
SS
42
43 I'm beginning to wonder whether it wouldn't be nicer to internally
44 generate trees, with types, and then spit out the bytecode in
45 linear form afterwards; we could generate fewer `swap', `ext', and
46 `zero_ext' bytecodes that way; it would make good constant folding
47 easier, too. But at the moment, I think we should be willing to
48 pay for the simplicity of this code with less-than-optimal bytecode
49 strings.
50
c5aa993b
JM
51 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
52\f
c906108c
SS
53
54
c906108c
SS
55/* Prototypes for local functions. */
56
57/* There's a standard order to the arguments of these functions:
58 union exp_element ** --- pointer into expression
59 struct agent_expr * --- agent expression buffer to generate code into
60 struct axs_value * --- describes value left on top of stack */
c5aa993b 61
a14ed312
KB
62static struct value *const_var_ref (struct symbol *var);
63static struct value *const_expr (union exp_element **pc);
64static struct value *maybe_const_expr (union exp_element **pc);
65
66static void gen_traced_pop (struct agent_expr *, struct axs_value *);
67
68static void gen_sign_extend (struct agent_expr *, struct type *);
69static void gen_extend (struct agent_expr *, struct type *);
70static void gen_fetch (struct agent_expr *, struct type *);
71static void gen_left_shift (struct agent_expr *, int);
72
73
74static void gen_frame_args_address (struct agent_expr *);
75static void gen_frame_locals_address (struct agent_expr *);
76static void gen_offset (struct agent_expr *ax, int offset);
77static void gen_sym_offset (struct agent_expr *, struct symbol *);
78static void gen_var_ref (struct agent_expr *ax,
79 struct axs_value *value, struct symbol *var);
80
81
82static void gen_int_literal (struct agent_expr *ax,
83 struct axs_value *value,
84 LONGEST k, struct type *type);
85
86
87static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
88static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
89static int type_wider_than (struct type *type1, struct type *type2);
90static struct type *max_type (struct type *type1, struct type *type2);
91static void gen_conversion (struct agent_expr *ax,
92 struct type *from, struct type *to);
93static int is_nontrivial_conversion (struct type *from, struct type *to);
94static void gen_usual_arithmetic (struct agent_expr *ax,
95 struct axs_value *value1,
96 struct axs_value *value2);
97static void gen_integral_promotions (struct agent_expr *ax,
98 struct axs_value *value);
99static void gen_cast (struct agent_expr *ax,
100 struct axs_value *value, struct type *type);
101static void gen_scale (struct agent_expr *ax,
102 enum agent_op op, struct type *type);
103static void gen_add (struct agent_expr *ax,
104 struct axs_value *value,
105 struct axs_value *value1,
106 struct axs_value *value2, char *name);
107static void gen_sub (struct agent_expr *ax,
108 struct axs_value *value,
109 struct axs_value *value1, struct axs_value *value2);
110static void gen_binop (struct agent_expr *ax,
111 struct axs_value *value,
112 struct axs_value *value1,
113 struct axs_value *value2,
114 enum agent_op op,
115 enum agent_op op_unsigned, int may_carry, char *name);
116static void gen_logical_not (struct agent_expr *ax, struct axs_value *value);
117static void gen_complement (struct agent_expr *ax, struct axs_value *value);
118static void gen_deref (struct agent_expr *, struct axs_value *);
119static void gen_address_of (struct agent_expr *, struct axs_value *);
120static int find_field (struct type *type, char *name);
121static void gen_bitfield_ref (struct agent_expr *ax,
122 struct axs_value *value,
123 struct type *type, int start, int end);
124static void gen_struct_ref (struct agent_expr *ax,
125 struct axs_value *value,
126 char *field,
127 char *operator_name, char *operand_name);
128static void gen_repeat (union exp_element **pc,
129 struct agent_expr *ax, struct axs_value *value);
130static void gen_sizeof (union exp_element **pc,
131 struct agent_expr *ax, struct axs_value *value);
132static void gen_expr (union exp_element **pc,
133 struct agent_expr *ax, struct axs_value *value);
c5aa993b 134
a14ed312 135static void agent_command (char *exp, int from_tty);
c906108c 136\f
c5aa993b 137
c906108c
SS
138/* Detecting constant expressions. */
139
140/* If the variable reference at *PC is a constant, return its value.
141 Otherwise, return zero.
142
143 Hey, Wally! How can a variable reference be a constant?
144
145 Well, Beav, this function really handles the OP_VAR_VALUE operator,
146 not specifically variable references. GDB uses OP_VAR_VALUE to
147 refer to any kind of symbolic reference: function names, enum
148 elements, and goto labels are all handled through the OP_VAR_VALUE
149 operator, even though they're constants. It makes sense given the
150 situation.
151
152 Gee, Wally, don'cha wonder sometimes if data representations that
153 subvert commonly accepted definitions of terms in favor of heavily
154 context-specific interpretations are really just a tool of the
155 programming hegemony to preserve their power and exclude the
156 proletariat? */
157
158static struct value *
fba45db2 159const_var_ref (struct symbol *var)
c906108c
SS
160{
161 struct type *type = SYMBOL_TYPE (var);
162
163 switch (SYMBOL_CLASS (var))
164 {
165 case LOC_CONST:
166 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
167
168 case LOC_LABEL:
4478b372 169 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
c906108c
SS
170
171 default:
172 return 0;
173 }
174}
175
176
177/* If the expression starting at *PC has a constant value, return it.
178 Otherwise, return zero. If we return a value, then *PC will be
179 advanced to the end of it. If we return zero, *PC could be
180 anywhere. */
181static struct value *
fba45db2 182const_expr (union exp_element **pc)
c906108c
SS
183{
184 enum exp_opcode op = (*pc)->opcode;
185 struct value *v1;
186
187 switch (op)
188 {
189 case OP_LONG:
190 {
191 struct type *type = (*pc)[1].type;
192 LONGEST k = (*pc)[2].longconst;
193 (*pc) += 4;
194 return value_from_longest (type, k);
195 }
196
197 case OP_VAR_VALUE:
198 {
199 struct value *v = const_var_ref ((*pc)[2].symbol);
200 (*pc) += 4;
201 return v;
202 }
203
c5aa993b 204 /* We could add more operators in here. */
c906108c
SS
205
206 case UNOP_NEG:
207 (*pc)++;
208 v1 = const_expr (pc);
209 if (v1)
210 return value_neg (v1);
211 else
212 return 0;
213
214 default:
215 return 0;
216 }
217}
218
219
220/* Like const_expr, but guarantee also that *PC is undisturbed if the
221 expression is not constant. */
222static struct value *
fba45db2 223maybe_const_expr (union exp_element **pc)
c906108c
SS
224{
225 union exp_element *tentative_pc = *pc;
226 struct value *v = const_expr (&tentative_pc);
227
228 /* If we got a value, then update the real PC. */
229 if (v)
230 *pc = tentative_pc;
c5aa993b 231
c906108c
SS
232 return v;
233}
c906108c 234\f
c5aa993b 235
c906108c
SS
236/* Generating bytecode from GDB expressions: general assumptions */
237
238/* Here are a few general assumptions made throughout the code; if you
239 want to make a change that contradicts one of these, then you'd
240 better scan things pretty thoroughly.
241
242 - We assume that all values occupy one stack element. For example,
c5aa993b
JM
243 sometimes we'll swap to get at the left argument to a binary
244 operator. If we decide that void values should occupy no stack
245 elements, or that synthetic arrays (whose size is determined at
246 run time, created by the `@' operator) should occupy two stack
247 elements (address and length), then this will cause trouble.
c906108c
SS
248
249 - We assume the stack elements are infinitely wide, and that we
c5aa993b
JM
250 don't have to worry what happens if the user requests an
251 operation that is wider than the actual interpreter's stack.
252 That is, it's up to the interpreter to handle directly all the
253 integer widths the user has access to. (Woe betide the language
254 with bignums!)
c906108c
SS
255
256 - We don't support side effects. Thus, we don't have to worry about
c5aa993b 257 GCC's generalized lvalues, function calls, etc.
c906108c
SS
258
259 - We don't support floating point. Many places where we switch on
c5aa993b
JM
260 some type don't bother to include cases for floating point; there
261 may be even more subtle ways this assumption exists. For
262 example, the arguments to % must be integers.
c906108c
SS
263
264 - We assume all subexpressions have a static, unchanging type. If
c5aa993b
JM
265 we tried to support convenience variables, this would be a
266 problem.
c906108c
SS
267
268 - All values on the stack should always be fully zero- or
c5aa993b
JM
269 sign-extended.
270
271 (I wasn't sure whether to choose this or its opposite --- that
272 only addresses are assumed extended --- but it turns out that
273 neither convention completely eliminates spurious extend
274 operations (if everything is always extended, then you have to
275 extend after add, because it could overflow; if nothing is
276 extended, then you end up producing extends whenever you change
277 sizes), and this is simpler.) */
c906108c 278\f
c5aa993b 279
c906108c
SS
280/* Generating bytecode from GDB expressions: the `trace' kludge */
281
282/* The compiler in this file is a general-purpose mechanism for
283 translating GDB expressions into bytecode. One ought to be able to
284 find a million and one uses for it.
285
286 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
287 of expediency. Let he who is without sin cast the first stone.
288
289 For the data tracing facility, we need to insert `trace' bytecodes
290 before each data fetch; this records all the memory that the
291 expression touches in the course of evaluation, so that memory will
292 be available when the user later tries to evaluate the expression
293 in GDB.
294
295 This should be done (I think) in a post-processing pass, that walks
296 an arbitrary agent expression and inserts `trace' operations at the
297 appropriate points. But it's much faster to just hack them
298 directly into the code. And since we're in a crunch, that's what
299 I've done.
300
301 Setting the flag trace_kludge to non-zero enables the code that
302 emits the trace bytecodes at the appropriate points. */
303static int trace_kludge;
304
305/* Trace the lvalue on the stack, if it needs it. In either case, pop
306 the value. Useful on the left side of a comma, and at the end of
307 an expression being used for tracing. */
308static void
fba45db2 309gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
310{
311 if (trace_kludge)
312 switch (value->kind)
313 {
314 case axs_rvalue:
315 /* We don't trace rvalues, just the lvalues necessary to
c5aa993b 316 produce them. So just dispose of this value. */
c906108c
SS
317 ax_simple (ax, aop_pop);
318 break;
319
320 case axs_lvalue_memory:
321 {
648027cc 322 int length = TYPE_LENGTH (check_typedef (value->type));
c906108c
SS
323
324 /* There's no point in trying to use a trace_quick bytecode
325 here, since "trace_quick SIZE pop" is three bytes, whereas
326 "const8 SIZE trace" is also three bytes, does the same
327 thing, and the simplest code which generates that will also
328 work correctly for objects with large sizes. */
329 ax_const_l (ax, length);
330 ax_simple (ax, aop_trace);
331 }
c5aa993b 332 break;
c906108c
SS
333
334 case axs_lvalue_register:
335 /* We need to mention the register somewhere in the bytecode,
336 so ax_reqs will pick it up and add it to the mask of
337 registers used. */
338 ax_reg (ax, value->u.reg);
339 ax_simple (ax, aop_pop);
340 break;
341 }
342 else
343 /* If we're not tracing, just pop the value. */
344 ax_simple (ax, aop_pop);
345}
c5aa993b 346\f
c906108c
SS
347
348
c906108c
SS
349/* Generating bytecode from GDB expressions: helper functions */
350
351/* Assume that the lower bits of the top of the stack is a value of
352 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
353static void
fba45db2 354gen_sign_extend (struct agent_expr *ax, struct type *type)
c906108c
SS
355{
356 /* Do we need to sign-extend this? */
c5aa993b 357 if (!TYPE_UNSIGNED (type))
0004e5a2 358 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
c906108c
SS
359}
360
361
362/* Assume the lower bits of the top of the stack hold a value of type
363 TYPE, and the upper bits are garbage. Sign-extend or truncate as
364 needed. */
365static void
fba45db2 366gen_extend (struct agent_expr *ax, struct type *type)
c906108c 367{
0004e5a2 368 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
c906108c
SS
369 /* I just had to. */
370 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
371}
372
373
374/* Assume that the top of the stack contains a value of type "pointer
375 to TYPE"; generate code to fetch its value. Note that TYPE is the
376 target type, not the pointer type. */
377static void
fba45db2 378gen_fetch (struct agent_expr *ax, struct type *type)
c906108c
SS
379{
380 if (trace_kludge)
381 {
382 /* Record the area of memory we're about to fetch. */
383 ax_trace_quick (ax, TYPE_LENGTH (type));
384 }
385
0004e5a2 386 switch (TYPE_CODE (type))
c906108c
SS
387 {
388 case TYPE_CODE_PTR:
389 case TYPE_CODE_ENUM:
390 case TYPE_CODE_INT:
391 case TYPE_CODE_CHAR:
392 /* It's a scalar value, so we know how to dereference it. How
393 many bytes long is it? */
0004e5a2 394 switch (TYPE_LENGTH (type))
c906108c 395 {
c5aa993b
JM
396 case 8 / TARGET_CHAR_BIT:
397 ax_simple (ax, aop_ref8);
398 break;
399 case 16 / TARGET_CHAR_BIT:
400 ax_simple (ax, aop_ref16);
401 break;
402 case 32 / TARGET_CHAR_BIT:
403 ax_simple (ax, aop_ref32);
404 break;
405 case 64 / TARGET_CHAR_BIT:
406 ax_simple (ax, aop_ref64);
407 break;
c906108c
SS
408
409 /* Either our caller shouldn't have asked us to dereference
410 that pointer (other code's fault), or we're not
411 implementing something we should be (this code's fault).
412 In any case, it's a bug the user shouldn't see. */
413 default:
8e65ff28 414 internal_error (__FILE__, __LINE__,
3d263c1d 415 _("gen_fetch: strange size"));
c906108c
SS
416 }
417
418 gen_sign_extend (ax, type);
419 break;
420
421 default:
422 /* Either our caller shouldn't have asked us to dereference that
c5aa993b
JM
423 pointer (other code's fault), or we're not implementing
424 something we should be (this code's fault). In any case,
425 it's a bug the user shouldn't see. */
8e65ff28 426 internal_error (__FILE__, __LINE__,
3d263c1d 427 _("gen_fetch: bad type code"));
c906108c
SS
428 }
429}
430
431
432/* Generate code to left shift the top of the stack by DISTANCE bits, or
433 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
434 unsigned (logical) right shifts. */
435static void
fba45db2 436gen_left_shift (struct agent_expr *ax, int distance)
c906108c
SS
437{
438 if (distance > 0)
439 {
440 ax_const_l (ax, distance);
441 ax_simple (ax, aop_lsh);
442 }
443 else if (distance < 0)
444 {
445 ax_const_l (ax, -distance);
446 ax_simple (ax, aop_rsh_unsigned);
447 }
448}
c5aa993b 449\f
c906108c
SS
450
451
c906108c
SS
452/* Generating bytecode from GDB expressions: symbol references */
453
454/* Generate code to push the base address of the argument portion of
455 the top stack frame. */
456static void
fba45db2 457gen_frame_args_address (struct agent_expr *ax)
c906108c 458{
39d4ef09
AC
459 int frame_reg;
460 LONGEST frame_offset;
c906108c 461
c7bb205c
UW
462 gdbarch_virtual_frame_pointer (current_gdbarch,
463 ax->scope, &frame_reg, &frame_offset);
c5aa993b 464 ax_reg (ax, frame_reg);
c906108c
SS
465 gen_offset (ax, frame_offset);
466}
467
468
469/* Generate code to push the base address of the locals portion of the
470 top stack frame. */
471static void
fba45db2 472gen_frame_locals_address (struct agent_expr *ax)
c906108c 473{
39d4ef09
AC
474 int frame_reg;
475 LONGEST frame_offset;
c906108c 476
c7bb205c
UW
477 gdbarch_virtual_frame_pointer (current_gdbarch,
478 ax->scope, &frame_reg, &frame_offset);
c5aa993b 479 ax_reg (ax, frame_reg);
c906108c
SS
480 gen_offset (ax, frame_offset);
481}
482
483
484/* Generate code to add OFFSET to the top of the stack. Try to
485 generate short and readable code. We use this for getting to
486 variables on the stack, and structure members. If we were
487 programming in ML, it would be clearer why these are the same
488 thing. */
489static void
fba45db2 490gen_offset (struct agent_expr *ax, int offset)
c906108c
SS
491{
492 /* It would suffice to simply push the offset and add it, but this
493 makes it easier to read positive and negative offsets in the
494 bytecode. */
495 if (offset > 0)
496 {
497 ax_const_l (ax, offset);
498 ax_simple (ax, aop_add);
499 }
500 else if (offset < 0)
501 {
502 ax_const_l (ax, -offset);
503 ax_simple (ax, aop_sub);
504 }
505}
506
507
508/* In many cases, a symbol's value is the offset from some other
509 address (stack frame, base register, etc.) Generate code to add
510 VAR's value to the top of the stack. */
511static void
fba45db2 512gen_sym_offset (struct agent_expr *ax, struct symbol *var)
c906108c
SS
513{
514 gen_offset (ax, SYMBOL_VALUE (var));
515}
516
517
518/* Generate code for a variable reference to AX. The variable is the
519 symbol VAR. Set VALUE to describe the result. */
520
521static void
fba45db2 522gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
c906108c
SS
523{
524 /* Dereference any typedefs. */
525 value->type = check_typedef (SYMBOL_TYPE (var));
526
527 /* I'm imitating the code in read_var_value. */
528 switch (SYMBOL_CLASS (var))
529 {
530 case LOC_CONST: /* A constant, like an enum value. */
531 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
532 value->kind = axs_rvalue;
533 break;
534
535 case LOC_LABEL: /* A goto label, being used as a value. */
536 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
537 value->kind = axs_rvalue;
538 break;
539
540 case LOC_CONST_BYTES:
8e65ff28 541 internal_error (__FILE__, __LINE__,
3d263c1d 542 _("gen_var_ref: LOC_CONST_BYTES symbols are not supported"));
c906108c
SS
543
544 /* Variable at a fixed location in memory. Easy. */
545 case LOC_STATIC:
546 /* Push the address of the variable. */
547 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
548 value->kind = axs_lvalue_memory;
549 break;
550
551 case LOC_ARG: /* var lives in argument area of frame */
552 gen_frame_args_address (ax);
553 gen_sym_offset (ax, var);
554 value->kind = axs_lvalue_memory;
555 break;
556
557 case LOC_REF_ARG: /* As above, but the frame slot really
558 holds the address of the variable. */
559 gen_frame_args_address (ax);
560 gen_sym_offset (ax, var);
561 /* Don't assume any particular pointer size. */
562 gen_fetch (ax, lookup_pointer_type (builtin_type_void));
563 value->kind = axs_lvalue_memory;
564 break;
565
566 case LOC_LOCAL: /* var lives in locals area of frame */
567 case LOC_LOCAL_ARG:
568 gen_frame_locals_address (ax);
569 gen_sym_offset (ax, var);
570 value->kind = axs_lvalue_memory;
571 break;
572
573 case LOC_BASEREG: /* relative to some base register */
574 case LOC_BASEREG_ARG:
575 ax_reg (ax, SYMBOL_BASEREG (var));
576 gen_sym_offset (ax, var);
577 value->kind = axs_lvalue_memory;
578 break;
579
580 case LOC_TYPEDEF:
3d263c1d 581 error (_("Cannot compute value of typedef `%s'."),
de5ad195 582 SYMBOL_PRINT_NAME (var));
c906108c
SS
583 break;
584
585 case LOC_BLOCK:
586 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
587 value->kind = axs_rvalue;
588 break;
589
590 case LOC_REGISTER:
591 case LOC_REGPARM:
592 /* Don't generate any code at all; in the process of treating
593 this as an lvalue or rvalue, the caller will generate the
594 right code. */
595 value->kind = axs_lvalue_register;
596 value->u.reg = SYMBOL_VALUE (var);
597 break;
598
599 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
c5aa993b
JM
600 register, not on the stack. Simpler than LOC_REGISTER and
601 LOC_REGPARM, because it's just like any other case where the
602 thing has a real address. */
c906108c
SS
603 case LOC_REGPARM_ADDR:
604 ax_reg (ax, SYMBOL_VALUE (var));
605 value->kind = axs_lvalue_memory;
606 break;
607
608 case LOC_UNRESOLVED:
609 {
c5aa993b 610 struct minimal_symbol *msym
22abf04a 611 = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (var), NULL, NULL);
c5aa993b 612 if (!msym)
3d263c1d 613 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
c5aa993b 614
c906108c
SS
615 /* Push the address of the variable. */
616 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
617 value->kind = axs_lvalue_memory;
618 }
c5aa993b 619 break;
c906108c 620
a55cc764
DJ
621 case LOC_COMPUTED:
622 case LOC_COMPUTED_ARG:
a67af2b9
AC
623 /* FIXME: cagney/2004-01-26: It should be possible to
624 unconditionally call the SYMBOL_OPS method when available.
d3efc286 625 Unfortunately DWARF 2 stores the frame-base (instead of the
a67af2b9
AC
626 function) location in a function's symbol. Oops! For the
627 moment enable this when/where applicable. */
628 SYMBOL_OPS (var)->tracepoint_var_ref (var, ax, value);
a55cc764
DJ
629 break;
630
c906108c 631 case LOC_OPTIMIZED_OUT:
3d263c1d 632 error (_("The variable `%s' has been optimized out."),
de5ad195 633 SYMBOL_PRINT_NAME (var));
c906108c
SS
634 break;
635
636 default:
3d263c1d 637 error (_("Cannot find value of botched symbol `%s'."),
de5ad195 638 SYMBOL_PRINT_NAME (var));
c906108c
SS
639 break;
640 }
641}
c5aa993b 642\f
c906108c
SS
643
644
c906108c
SS
645/* Generating bytecode from GDB expressions: literals */
646
647static void
fba45db2
KB
648gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
649 struct type *type)
c906108c
SS
650{
651 ax_const_l (ax, k);
652 value->kind = axs_rvalue;
648027cc 653 value->type = check_typedef (type);
c906108c 654}
c5aa993b 655\f
c906108c
SS
656
657
c906108c
SS
658/* Generating bytecode from GDB expressions: unary conversions, casts */
659
660/* Take what's on the top of the stack (as described by VALUE), and
661 try to make an rvalue out of it. Signal an error if we can't do
662 that. */
663static void
fba45db2 664require_rvalue (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
665{
666 switch (value->kind)
667 {
668 case axs_rvalue:
669 /* It's already an rvalue. */
670 break;
671
672 case axs_lvalue_memory:
673 /* The top of stack is the address of the object. Dereference. */
674 gen_fetch (ax, value->type);
675 break;
676
677 case axs_lvalue_register:
678 /* There's nothing on the stack, but value->u.reg is the
679 register number containing the value.
680
c5aa993b
JM
681 When we add floating-point support, this is going to have to
682 change. What about SPARC register pairs, for example? */
c906108c
SS
683 ax_reg (ax, value->u.reg);
684 gen_extend (ax, value->type);
685 break;
686 }
687
688 value->kind = axs_rvalue;
689}
690
691
692/* Assume the top of the stack is described by VALUE, and perform the
693 usual unary conversions. This is motivated by ANSI 6.2.2, but of
694 course GDB expressions are not ANSI; they're the mishmash union of
695 a bunch of languages. Rah.
696
697 NOTE! This function promises to produce an rvalue only when the
698 incoming value is of an appropriate type. In other words, the
699 consumer of the value this function produces may assume the value
700 is an rvalue only after checking its type.
701
702 The immediate issue is that if the user tries to use a structure or
703 union as an operand of, say, the `+' operator, we don't want to try
704 to convert that structure to an rvalue; require_rvalue will bomb on
705 structs and unions. Rather, we want to simply pass the struct
706 lvalue through unchanged, and let `+' raise an error. */
707
708static void
fba45db2 709gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
710{
711 /* We don't have to generate any code for the usual integral
712 conversions, since values are always represented as full-width on
713 the stack. Should we tweak the type? */
714
715 /* Some types require special handling. */
0004e5a2 716 switch (TYPE_CODE (value->type))
c906108c
SS
717 {
718 /* Functions get converted to a pointer to the function. */
719 case TYPE_CODE_FUNC:
720 value->type = lookup_pointer_type (value->type);
721 value->kind = axs_rvalue; /* Should always be true, but just in case. */
722 break;
723
724 /* Arrays get converted to a pointer to their first element, and
c5aa993b 725 are no longer an lvalue. */
c906108c
SS
726 case TYPE_CODE_ARRAY:
727 {
728 struct type *elements = TYPE_TARGET_TYPE (value->type);
729 value->type = lookup_pointer_type (elements);
730 value->kind = axs_rvalue;
731 /* We don't need to generate any code; the address of the array
732 is also the address of its first element. */
733 }
c5aa993b 734 break;
c906108c 735
c5aa993b
JM
736 /* Don't try to convert structures and unions to rvalues. Let the
737 consumer signal an error. */
c906108c
SS
738 case TYPE_CODE_STRUCT:
739 case TYPE_CODE_UNION:
740 return;
741
742 /* If the value is an enum, call it an integer. */
743 case TYPE_CODE_ENUM:
744 value->type = builtin_type_int;
745 break;
746 }
747
748 /* If the value is an lvalue, dereference it. */
749 require_rvalue (ax, value);
750}
751
752
753/* Return non-zero iff the type TYPE1 is considered "wider" than the
754 type TYPE2, according to the rules described in gen_usual_arithmetic. */
755static int
fba45db2 756type_wider_than (struct type *type1, struct type *type2)
c906108c
SS
757{
758 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
759 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
760 && TYPE_UNSIGNED (type1)
c5aa993b 761 && !TYPE_UNSIGNED (type2)));
c906108c
SS
762}
763
764
765/* Return the "wider" of the two types TYPE1 and TYPE2. */
766static struct type *
fba45db2 767max_type (struct type *type1, struct type *type2)
c906108c
SS
768{
769 return type_wider_than (type1, type2) ? type1 : type2;
770}
771
772
773/* Generate code to convert a scalar value of type FROM to type TO. */
774static void
fba45db2 775gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
c906108c
SS
776{
777 /* Perhaps there is a more graceful way to state these rules. */
778
779 /* If we're converting to a narrower type, then we need to clear out
780 the upper bits. */
781 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
782 gen_extend (ax, from);
783
784 /* If the two values have equal width, but different signednesses,
785 then we need to extend. */
786 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
787 {
788 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
789 gen_extend (ax, to);
790 }
791
792 /* If we're converting to a wider type, and becoming unsigned, then
793 we need to zero out any possible sign bits. */
794 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
795 {
796 if (TYPE_UNSIGNED (to))
797 gen_extend (ax, to);
798 }
799}
800
801
802/* Return non-zero iff the type FROM will require any bytecodes to be
803 emitted to be converted to the type TO. */
804static int
fba45db2 805is_nontrivial_conversion (struct type *from, struct type *to)
c906108c
SS
806{
807 struct agent_expr *ax = new_agent_expr (0);
808 int nontrivial;
809
810 /* Actually generate the code, and see if anything came out. At the
811 moment, it would be trivial to replicate the code in
812 gen_conversion here, but in the future, when we're supporting
813 floating point and the like, it may not be. Doing things this
814 way allows this function to be independent of the logic in
815 gen_conversion. */
816 gen_conversion (ax, from, to);
817 nontrivial = ax->len > 0;
818 free_agent_expr (ax);
819 return nontrivial;
820}
821
822
823/* Generate code to perform the "usual arithmetic conversions" (ANSI C
824 6.2.1.5) for the two operands of an arithmetic operator. This
825 effectively finds a "least upper bound" type for the two arguments,
826 and promotes each argument to that type. *VALUE1 and *VALUE2
827 describe the values as they are passed in, and as they are left. */
828static void
fba45db2
KB
829gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
830 struct axs_value *value2)
c906108c
SS
831{
832 /* Do the usual binary conversions. */
833 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
834 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
835 {
836 /* The ANSI integral promotions seem to work this way: Order the
c5aa993b
JM
837 integer types by size, and then by signedness: an n-bit
838 unsigned type is considered "wider" than an n-bit signed
839 type. Promote to the "wider" of the two types, and always
840 promote at least to int. */
c906108c
SS
841 struct type *target = max_type (builtin_type_int,
842 max_type (value1->type, value2->type));
843
844 /* Deal with value2, on the top of the stack. */
845 gen_conversion (ax, value2->type, target);
846
847 /* Deal with value1, not on the top of the stack. Don't
848 generate the `swap' instructions if we're not actually going
849 to do anything. */
850 if (is_nontrivial_conversion (value1->type, target))
851 {
852 ax_simple (ax, aop_swap);
853 gen_conversion (ax, value1->type, target);
854 ax_simple (ax, aop_swap);
855 }
856
648027cc 857 value1->type = value2->type = check_typedef (target);
c906108c
SS
858 }
859}
860
861
862/* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
863 the value on the top of the stack, as described by VALUE. Assume
864 the value has integral type. */
865static void
fba45db2 866gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
c906108c 867{
c5aa993b 868 if (!type_wider_than (value->type, builtin_type_int))
c906108c
SS
869 {
870 gen_conversion (ax, value->type, builtin_type_int);
871 value->type = builtin_type_int;
872 }
c5aa993b 873 else if (!type_wider_than (value->type, builtin_type_unsigned_int))
c906108c
SS
874 {
875 gen_conversion (ax, value->type, builtin_type_unsigned_int);
876 value->type = builtin_type_unsigned_int;
877 }
878}
879
880
881/* Generate code for a cast to TYPE. */
882static void
fba45db2 883gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
c906108c
SS
884{
885 /* GCC does allow casts to yield lvalues, so this should be fixed
886 before merging these changes into the trunk. */
887 require_rvalue (ax, value);
888 /* Dereference typedefs. */
889 type = check_typedef (type);
890
0004e5a2 891 switch (TYPE_CODE (type))
c906108c
SS
892 {
893 case TYPE_CODE_PTR:
894 /* It's implementation-defined, and I'll bet this is what GCC
895 does. */
896 break;
897
898 case TYPE_CODE_ARRAY:
899 case TYPE_CODE_STRUCT:
900 case TYPE_CODE_UNION:
901 case TYPE_CODE_FUNC:
3d263c1d 902 error (_("Invalid type cast: intended type must be scalar."));
c906108c
SS
903
904 case TYPE_CODE_ENUM:
905 /* We don't have to worry about the size of the value, because
906 all our integral values are fully sign-extended, and when
907 casting pointers we can do anything we like. Is there any
908 way for us to actually know what GCC actually does with a
909 cast like this? */
910 value->type = type;
911 break;
c5aa993b 912
c906108c
SS
913 case TYPE_CODE_INT:
914 gen_conversion (ax, value->type, type);
915 break;
916
917 case TYPE_CODE_VOID:
918 /* We could pop the value, and rely on everyone else to check
c5aa993b
JM
919 the type and notice that this value doesn't occupy a stack
920 slot. But for now, leave the value on the stack, and
921 preserve the "value == stack element" assumption. */
c906108c
SS
922 break;
923
924 default:
3d263c1d 925 error (_("Casts to requested type are not yet implemented."));
c906108c
SS
926 }
927
928 value->type = type;
929}
c5aa993b 930\f
c906108c
SS
931
932
c906108c
SS
933/* Generating bytecode from GDB expressions: arithmetic */
934
935/* Scale the integer on the top of the stack by the size of the target
936 of the pointer type TYPE. */
937static void
fba45db2 938gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
c906108c
SS
939{
940 struct type *element = TYPE_TARGET_TYPE (type);
941
0004e5a2 942 if (TYPE_LENGTH (element) != 1)
c906108c 943 {
0004e5a2 944 ax_const_l (ax, TYPE_LENGTH (element));
c906108c
SS
945 ax_simple (ax, op);
946 }
947}
948
949
950/* Generate code for an addition; non-trivial because we deal with
951 pointer arithmetic. We set VALUE to describe the result value; we
952 assume VALUE1 and VALUE2 describe the two operands, and that
953 they've undergone the usual binary conversions. Used by both
954 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
955static void
fba45db2
KB
956gen_add (struct agent_expr *ax, struct axs_value *value,
957 struct axs_value *value1, struct axs_value *value2, char *name)
c906108c
SS
958{
959 /* Is it INT+PTR? */
0004e5a2
DJ
960 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
961 && TYPE_CODE (value2->type) == TYPE_CODE_PTR)
c906108c
SS
962 {
963 /* Swap the values and proceed normally. */
964 ax_simple (ax, aop_swap);
965 gen_scale (ax, aop_mul, value2->type);
966 ax_simple (ax, aop_add);
c5aa993b 967 gen_extend (ax, value2->type); /* Catch overflow. */
c906108c
SS
968 value->type = value2->type;
969 }
970
971 /* Is it PTR+INT? */
0004e5a2
DJ
972 else if (TYPE_CODE (value1->type) == TYPE_CODE_PTR
973 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
c906108c
SS
974 {
975 gen_scale (ax, aop_mul, value1->type);
976 ax_simple (ax, aop_add);
c5aa993b 977 gen_extend (ax, value1->type); /* Catch overflow. */
c906108c
SS
978 value->type = value1->type;
979 }
980
981 /* Must be number + number; the usual binary conversions will have
982 brought them both to the same width. */
0004e5a2
DJ
983 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
984 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
c906108c
SS
985 {
986 ax_simple (ax, aop_add);
c5aa993b 987 gen_extend (ax, value1->type); /* Catch overflow. */
c906108c
SS
988 value->type = value1->type;
989 }
990
991 else
3d263c1d 992 error (_("Invalid combination of types in %s."), name);
c906108c
SS
993
994 value->kind = axs_rvalue;
995}
996
997
998/* Generate code for an addition; non-trivial because we have to deal
999 with pointer arithmetic. We set VALUE to describe the result
1000 value; we assume VALUE1 and VALUE2 describe the two operands, and
1001 that they've undergone the usual binary conversions. */
1002static void
fba45db2
KB
1003gen_sub (struct agent_expr *ax, struct axs_value *value,
1004 struct axs_value *value1, struct axs_value *value2)
c906108c 1005{
0004e5a2 1006 if (TYPE_CODE (value1->type) == TYPE_CODE_PTR)
c906108c
SS
1007 {
1008 /* Is it PTR - INT? */
0004e5a2 1009 if (TYPE_CODE (value2->type) == TYPE_CODE_INT)
c906108c
SS
1010 {
1011 gen_scale (ax, aop_mul, value1->type);
1012 ax_simple (ax, aop_sub);
c5aa993b 1013 gen_extend (ax, value1->type); /* Catch overflow. */
c906108c
SS
1014 value->type = value1->type;
1015 }
1016
1017 /* Is it PTR - PTR? Strictly speaking, the types ought to
c5aa993b
JM
1018 match, but this is what the normal GDB expression evaluator
1019 tests for. */
0004e5a2 1020 else if (TYPE_CODE (value2->type) == TYPE_CODE_PTR
c906108c
SS
1021 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1022 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1023 {
1024 ax_simple (ax, aop_sub);
1025 gen_scale (ax, aop_div_unsigned, value1->type);
c5aa993b 1026 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
c906108c
SS
1027 }
1028 else
3d263c1d 1029 error (_("\
c906108c 1030First argument of `-' is a pointer, but second argument is neither\n\
3d263c1d 1031an integer nor a pointer of the same type."));
c906108c
SS
1032 }
1033
1034 /* Must be number + number. */
0004e5a2
DJ
1035 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
1036 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
c906108c
SS
1037 {
1038 ax_simple (ax, aop_sub);
c5aa993b 1039 gen_extend (ax, value1->type); /* Catch overflow. */
c906108c
SS
1040 value->type = value1->type;
1041 }
c5aa993b 1042
c906108c 1043 else
3d263c1d 1044 error (_("Invalid combination of types in subtraction."));
c906108c
SS
1045
1046 value->kind = axs_rvalue;
1047}
1048
1049/* Generate code for a binary operator that doesn't do pointer magic.
1050 We set VALUE to describe the result value; we assume VALUE1 and
1051 VALUE2 describe the two operands, and that they've undergone the
1052 usual binary conversions. MAY_CARRY should be non-zero iff the
1053 result needs to be extended. NAME is the English name of the
1054 operator, used in error messages */
1055static void
fba45db2
KB
1056gen_binop (struct agent_expr *ax, struct axs_value *value,
1057 struct axs_value *value1, struct axs_value *value2, enum agent_op op,
1058 enum agent_op op_unsigned, int may_carry, char *name)
c906108c
SS
1059{
1060 /* We only handle INT op INT. */
0004e5a2
DJ
1061 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1062 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
3d263c1d 1063 error (_("Invalid combination of types in %s."), name);
c5aa993b 1064
c906108c
SS
1065 ax_simple (ax,
1066 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1067 if (may_carry)
c5aa993b 1068 gen_extend (ax, value1->type); /* catch overflow */
c906108c
SS
1069 value->type = value1->type;
1070 value->kind = axs_rvalue;
1071}
1072
1073
1074static void
fba45db2 1075gen_logical_not (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1076{
1077 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1078 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
3d263c1d 1079 error (_("Invalid type of operand to `!'."));
c906108c
SS
1080
1081 gen_usual_unary (ax, value);
1082 ax_simple (ax, aop_log_not);
1083 value->type = builtin_type_int;
1084}
1085
1086
1087static void
fba45db2 1088gen_complement (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1089{
1090 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
3d263c1d 1091 error (_("Invalid type of operand to `~'."));
c906108c
SS
1092
1093 gen_usual_unary (ax, value);
1094 gen_integral_promotions (ax, value);
1095 ax_simple (ax, aop_bit_not);
1096 gen_extend (ax, value->type);
1097}
c5aa993b 1098\f
c906108c
SS
1099
1100
c906108c
SS
1101/* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1102
1103/* Dereference the value on the top of the stack. */
1104static void
fba45db2 1105gen_deref (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1106{
1107 /* The caller should check the type, because several operators use
1108 this, and we don't know what error message to generate. */
0004e5a2 1109 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
8e65ff28 1110 internal_error (__FILE__, __LINE__,
3d263c1d 1111 _("gen_deref: expected a pointer"));
c906108c
SS
1112
1113 /* We've got an rvalue now, which is a pointer. We want to yield an
1114 lvalue, whose address is exactly that pointer. So we don't
1115 actually emit any code; we just change the type from "Pointer to
1116 T" to "T", and mark the value as an lvalue in memory. Leave it
1117 to the consumer to actually dereference it. */
1118 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
0004e5a2 1119 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
c906108c
SS
1120 ? axs_rvalue : axs_lvalue_memory);
1121}
1122
1123
1124/* Produce the address of the lvalue on the top of the stack. */
1125static void
fba45db2 1126gen_address_of (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1127{
1128 /* Special case for taking the address of a function. The ANSI
1129 standard describes this as a special case, too, so this
1130 arrangement is not without motivation. */
0004e5a2 1131 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
c906108c
SS
1132 /* The value's already an rvalue on the stack, so we just need to
1133 change the type. */
1134 value->type = lookup_pointer_type (value->type);
1135 else
1136 switch (value->kind)
1137 {
1138 case axs_rvalue:
3d263c1d 1139 error (_("Operand of `&' is an rvalue, which has no address."));
c906108c
SS
1140
1141 case axs_lvalue_register:
3d263c1d 1142 error (_("Operand of `&' is in a register, and has no address."));
c906108c
SS
1143
1144 case axs_lvalue_memory:
1145 value->kind = axs_rvalue;
1146 value->type = lookup_pointer_type (value->type);
1147 break;
1148 }
1149}
1150
1151
1152/* A lot of this stuff will have to change to support C++. But we're
1153 not going to deal with that at the moment. */
1154
1155/* Find the field in the structure type TYPE named NAME, and return
1156 its index in TYPE's field array. */
1157static int
fba45db2 1158find_field (struct type *type, char *name)
c906108c
SS
1159{
1160 int i;
1161
1162 CHECK_TYPEDEF (type);
1163
1164 /* Make sure this isn't C++. */
1165 if (TYPE_N_BASECLASSES (type) != 0)
8e65ff28 1166 internal_error (__FILE__, __LINE__,
3d263c1d 1167 _("find_field: derived classes supported"));
c906108c
SS
1168
1169 for (i = 0; i < TYPE_NFIELDS (type); i++)
1170 {
1171 char *this_name = TYPE_FIELD_NAME (type, i);
1172
747f3d18
MS
1173 if (this_name)
1174 {
1175 if (strcmp (name, this_name) == 0)
1176 return i;
c906108c 1177
747f3d18
MS
1178 if (this_name[0] == '\0')
1179 internal_error (__FILE__, __LINE__,
1180 _("find_field: anonymous unions not supported"));
1181 }
c906108c
SS
1182 }
1183
3d263c1d 1184 error (_("Couldn't find member named `%s' in struct/union `%s'"),
7495dfdb 1185 name, TYPE_TAG_NAME (type));
c906108c
SS
1186
1187 return 0;
1188}
1189
1190
1191/* Generate code to push the value of a bitfield of a structure whose
1192 address is on the top of the stack. START and END give the
1193 starting and one-past-ending *bit* numbers of the field within the
1194 structure. */
1195static void
fba45db2
KB
1196gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1197 struct type *type, int start, int end)
c906108c
SS
1198{
1199 /* Note that ops[i] fetches 8 << i bits. */
1200 static enum agent_op ops[]
c5aa993b
JM
1201 =
1202 {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
c906108c
SS
1203 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1204
1205 /* We don't want to touch any byte that the bitfield doesn't
1206 actually occupy; we shouldn't make any accesses we're not
1207 explicitly permitted to. We rely here on the fact that the
1208 bytecode `ref' operators work on unaligned addresses.
1209
1210 It takes some fancy footwork to get the stack to work the way
1211 we'd like. Say we're retrieving a bitfield that requires three
1212 fetches. Initially, the stack just contains the address:
c5aa993b 1213 addr
c906108c 1214 For the first fetch, we duplicate the address
c5aa993b 1215 addr addr
c906108c
SS
1216 then add the byte offset, do the fetch, and shift and mask as
1217 needed, yielding a fragment of the value, properly aligned for
1218 the final bitwise or:
c5aa993b 1219 addr frag1
c906108c 1220 then we swap, and repeat the process:
c5aa993b
JM
1221 frag1 addr --- address on top
1222 frag1 addr addr --- duplicate it
1223 frag1 addr frag2 --- get second fragment
1224 frag1 frag2 addr --- swap again
1225 frag1 frag2 frag3 --- get third fragment
c906108c
SS
1226 Notice that, since the third fragment is the last one, we don't
1227 bother duplicating the address this time. Now we have all the
1228 fragments on the stack, and we can simply `or' them together,
1229 yielding the final value of the bitfield. */
1230
1231 /* The first and one-after-last bits in the field, but rounded down
1232 and up to byte boundaries. */
1233 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
c5aa993b
JM
1234 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1235 / TARGET_CHAR_BIT)
1236 * TARGET_CHAR_BIT);
c906108c
SS
1237
1238 /* current bit offset within the structure */
1239 int offset;
1240
1241 /* The index in ops of the opcode we're considering. */
1242 int op;
1243
1244 /* The number of fragments we generated in the process. Probably
1245 equal to the number of `one' bits in bytesize, but who cares? */
1246 int fragment_count;
1247
1248 /* Dereference any typedefs. */
1249 type = check_typedef (type);
1250
1251 /* Can we fetch the number of bits requested at all? */
1252 if ((end - start) > ((1 << num_ops) * 8))
8e65ff28 1253 internal_error (__FILE__, __LINE__,
3d263c1d 1254 _("gen_bitfield_ref: bitfield too wide"));
c906108c
SS
1255
1256 /* Note that we know here that we only need to try each opcode once.
1257 That may not be true on machines with weird byte sizes. */
1258 offset = bound_start;
1259 fragment_count = 0;
1260 for (op = num_ops - 1; op >= 0; op--)
1261 {
1262 /* number of bits that ops[op] would fetch */
1263 int op_size = 8 << op;
1264
1265 /* The stack at this point, from bottom to top, contains zero or
c5aa993b
JM
1266 more fragments, then the address. */
1267
c906108c
SS
1268 /* Does this fetch fit within the bitfield? */
1269 if (offset + op_size <= bound_end)
1270 {
1271 /* Is this the last fragment? */
1272 int last_frag = (offset + op_size == bound_end);
1273
c5aa993b
JM
1274 if (!last_frag)
1275 ax_simple (ax, aop_dup); /* keep a copy of the address */
1276
c906108c
SS
1277 /* Add the offset. */
1278 gen_offset (ax, offset / TARGET_CHAR_BIT);
1279
1280 if (trace_kludge)
1281 {
1282 /* Record the area of memory we're about to fetch. */
1283 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1284 }
1285
1286 /* Perform the fetch. */
1287 ax_simple (ax, ops[op]);
c5aa993b
JM
1288
1289 /* Shift the bits we have to their proper position.
c906108c
SS
1290 gen_left_shift will generate right shifts when the operand
1291 is negative.
1292
c5aa993b
JM
1293 A big-endian field diagram to ponder:
1294 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1295 +------++------++------++------++------++------++------++------+
1296 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1297 ^ ^ ^ ^
1298 bit number 16 32 48 53
c906108c
SS
1299 These are bit numbers as supplied by GDB. Note that the
1300 bit numbers run from right to left once you've fetched the
1301 value!
1302
c5aa993b
JM
1303 A little-endian field diagram to ponder:
1304 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1305 +------++------++------++------++------++------++------++------+
1306 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1307 ^ ^ ^ ^ ^
1308 bit number 48 32 16 4 0
1309
1310 In both cases, the most significant end is on the left
1311 (i.e. normal numeric writing order), which means that you
1312 don't go crazy thinking about `left' and `right' shifts.
1313
1314 We don't have to worry about masking yet:
1315 - If they contain garbage off the least significant end, then we
1316 must be looking at the low end of the field, and the right
1317 shift will wipe them out.
1318 - If they contain garbage off the most significant end, then we
1319 must be looking at the most significant end of the word, and
1320 the sign/zero extension will wipe them out.
1321 - If we're in the interior of the word, then there is no garbage
1322 on either end, because the ref operators zero-extend. */
0d20ae72 1323 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
c906108c 1324 gen_left_shift (ax, end - (offset + op_size));
c5aa993b 1325 else
c906108c
SS
1326 gen_left_shift (ax, offset - start);
1327
c5aa993b 1328 if (!last_frag)
c906108c
SS
1329 /* Bring the copy of the address up to the top. */
1330 ax_simple (ax, aop_swap);
1331
1332 offset += op_size;
1333 fragment_count++;
1334 }
1335 }
1336
1337 /* Generate enough bitwise `or' operations to combine all the
1338 fragments we left on the stack. */
1339 while (fragment_count-- > 1)
1340 ax_simple (ax, aop_bit_or);
1341
1342 /* Sign- or zero-extend the value as appropriate. */
1343 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1344
1345 /* This is *not* an lvalue. Ugh. */
1346 value->kind = axs_rvalue;
1347 value->type = type;
1348}
1349
1350
1351/* Generate code to reference the member named FIELD of a structure or
1352 union. The top of the stack, as described by VALUE, should have
1353 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1354 the operator being compiled, and OPERAND_NAME is the kind of thing
1355 it operates on; we use them in error messages. */
1356static void
fba45db2
KB
1357gen_struct_ref (struct agent_expr *ax, struct axs_value *value, char *field,
1358 char *operator_name, char *operand_name)
c906108c
SS
1359{
1360 struct type *type;
1361 int i;
1362
1363 /* Follow pointers until we reach a non-pointer. These aren't the C
1364 semantics, but they're what the normal GDB evaluator does, so we
1365 should at least be consistent. */
0004e5a2 1366 while (TYPE_CODE (value->type) == TYPE_CODE_PTR)
c906108c
SS
1367 {
1368 gen_usual_unary (ax, value);
1369 gen_deref (ax, value);
1370 }
e8860ec2 1371 type = check_typedef (value->type);
c906108c
SS
1372
1373 /* This must yield a structure or a union. */
1374 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1375 && TYPE_CODE (type) != TYPE_CODE_UNION)
3d263c1d 1376 error (_("The left operand of `%s' is not a %s."),
c906108c
SS
1377 operator_name, operand_name);
1378
1379 /* And it must be in memory; we don't deal with structure rvalues,
1380 or structures living in registers. */
1381 if (value->kind != axs_lvalue_memory)
3d263c1d 1382 error (_("Structure does not live in memory."));
c906108c
SS
1383
1384 i = find_field (type, field);
c5aa993b 1385
c906108c
SS
1386 /* Is this a bitfield? */
1387 if (TYPE_FIELD_PACKED (type, i))
1388 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1389 TYPE_FIELD_BITPOS (type, i),
1390 (TYPE_FIELD_BITPOS (type, i)
1391 + TYPE_FIELD_BITSIZE (type, i)));
1392 else
1393 {
1394 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1395 value->kind = axs_lvalue_memory;
1396 value->type = TYPE_FIELD_TYPE (type, i);
1397 }
1398}
1399
1400
1401/* Generate code for GDB's magical `repeat' operator.
1402 LVALUE @ INT creates an array INT elements long, and whose elements
1403 have the same type as LVALUE, located in memory so that LVALUE is
1404 its first element. For example, argv[0]@argc gives you the array
1405 of command-line arguments.
1406
1407 Unfortunately, because we have to know the types before we actually
1408 have a value for the expression, we can't implement this perfectly
1409 without changing the type system, having values that occupy two
1410 stack slots, doing weird things with sizeof, etc. So we require
1411 the right operand to be a constant expression. */
1412static void
fba45db2
KB
1413gen_repeat (union exp_element **pc, struct agent_expr *ax,
1414 struct axs_value *value)
c906108c
SS
1415{
1416 struct axs_value value1;
1417 /* We don't want to turn this into an rvalue, so no conversions
1418 here. */
1419 gen_expr (pc, ax, &value1);
1420 if (value1.kind != axs_lvalue_memory)
3d263c1d 1421 error (_("Left operand of `@' must be an object in memory."));
c906108c
SS
1422
1423 /* Evaluate the length; it had better be a constant. */
1424 {
1425 struct value *v = const_expr (pc);
1426 int length;
1427
c5aa993b 1428 if (!v)
3d263c1d 1429 error (_("Right operand of `@' must be a constant, in agent expressions."));
04624583 1430 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
3d263c1d 1431 error (_("Right operand of `@' must be an integer."));
c906108c
SS
1432 length = value_as_long (v);
1433 if (length <= 0)
3d263c1d 1434 error (_("Right operand of `@' must be positive."));
c906108c
SS
1435
1436 /* The top of the stack is already the address of the object, so
1437 all we need to do is frob the type of the lvalue. */
1438 {
1439 /* FIXME-type-allocation: need a way to free this type when we are
c5aa993b 1440 done with it. */
c906108c 1441 struct type *range
c5aa993b 1442 = create_range_type (0, builtin_type_int, 0, length - 1);
c906108c
SS
1443 struct type *array = create_array_type (0, value1.type, range);
1444
1445 value->kind = axs_lvalue_memory;
1446 value->type = array;
1447 }
1448 }
1449}
1450
1451
1452/* Emit code for the `sizeof' operator.
1453 *PC should point at the start of the operand expression; we advance it
1454 to the first instruction after the operand. */
1455static void
fba45db2
KB
1456gen_sizeof (union exp_element **pc, struct agent_expr *ax,
1457 struct axs_value *value)
c906108c
SS
1458{
1459 /* We don't care about the value of the operand expression; we only
1460 care about its type. However, in the current arrangement, the
1461 only way to find an expression's type is to generate code for it.
1462 So we generate code for the operand, and then throw it away,
1463 replacing it with code that simply pushes its size. */
1464 int start = ax->len;
1465 gen_expr (pc, ax, value);
1466
1467 /* Throw away the code we just generated. */
1468 ax->len = start;
c5aa993b 1469
c906108c
SS
1470 ax_const_l (ax, TYPE_LENGTH (value->type));
1471 value->kind = axs_rvalue;
1472 value->type = builtin_type_int;
1473}
c906108c 1474\f
c5aa993b 1475
c906108c
SS
1476/* Generating bytecode from GDB expressions: general recursive thingy */
1477
3d263c1d 1478/* XXX: i18n */
c906108c
SS
1479/* A gen_expr function written by a Gen-X'er guy.
1480 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1481static void
fba45db2
KB
1482gen_expr (union exp_element **pc, struct agent_expr *ax,
1483 struct axs_value *value)
c906108c
SS
1484{
1485 /* Used to hold the descriptions of operand expressions. */
1486 struct axs_value value1, value2;
1487 enum exp_opcode op = (*pc)[0].opcode;
1488
1489 /* If we're looking at a constant expression, just push its value. */
1490 {
1491 struct value *v = maybe_const_expr (pc);
c5aa993b 1492
c906108c
SS
1493 if (v)
1494 {
1495 ax_const_l (ax, value_as_long (v));
1496 value->kind = axs_rvalue;
df407dfe 1497 value->type = check_typedef (value_type (v));
c906108c
SS
1498 return;
1499 }
1500 }
1501
1502 /* Otherwise, go ahead and generate code for it. */
1503 switch (op)
1504 {
1505 /* Binary arithmetic operators. */
1506 case BINOP_ADD:
1507 case BINOP_SUB:
1508 case BINOP_MUL:
1509 case BINOP_DIV:
1510 case BINOP_REM:
1511 case BINOP_SUBSCRIPT:
1512 case BINOP_BITWISE_AND:
1513 case BINOP_BITWISE_IOR:
1514 case BINOP_BITWISE_XOR:
1515 (*pc)++;
1516 gen_expr (pc, ax, &value1);
1517 gen_usual_unary (ax, &value1);
1518 gen_expr (pc, ax, &value2);
1519 gen_usual_unary (ax, &value2);
1520 gen_usual_arithmetic (ax, &value1, &value2);
1521 switch (op)
1522 {
1523 case BINOP_ADD:
1524 gen_add (ax, value, &value1, &value2, "addition");
1525 break;
1526 case BINOP_SUB:
1527 gen_sub (ax, value, &value1, &value2);
1528 break;
1529 case BINOP_MUL:
1530 gen_binop (ax, value, &value1, &value2,
1531 aop_mul, aop_mul, 1, "multiplication");
1532 break;
1533 case BINOP_DIV:
1534 gen_binop (ax, value, &value1, &value2,
1535 aop_div_signed, aop_div_unsigned, 1, "division");
1536 break;
1537 case BINOP_REM:
1538 gen_binop (ax, value, &value1, &value2,
1539 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1540 break;
1541 case BINOP_SUBSCRIPT:
1542 gen_add (ax, value, &value1, &value2, "array subscripting");
1543 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
3d263c1d 1544 error (_("Invalid combination of types in array subscripting."));
c906108c
SS
1545 gen_deref (ax, value);
1546 break;
1547 case BINOP_BITWISE_AND:
1548 gen_binop (ax, value, &value1, &value2,
1549 aop_bit_and, aop_bit_and, 0, "bitwise and");
1550 break;
1551
1552 case BINOP_BITWISE_IOR:
1553 gen_binop (ax, value, &value1, &value2,
1554 aop_bit_or, aop_bit_or, 0, "bitwise or");
1555 break;
1556
1557 case BINOP_BITWISE_XOR:
1558 gen_binop (ax, value, &value1, &value2,
1559 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1560 break;
1561
1562 default:
1563 /* We should only list operators in the outer case statement
c5aa993b 1564 that we actually handle in the inner case statement. */
8e65ff28 1565 internal_error (__FILE__, __LINE__,
3d263c1d 1566 _("gen_expr: op case sets don't match"));
c906108c
SS
1567 }
1568 break;
1569
1570 /* Note that we need to be a little subtle about generating code
c5aa993b
JM
1571 for comma. In C, we can do some optimizations here because
1572 we know the left operand is only being evaluated for effect.
1573 However, if the tracing kludge is in effect, then we always
1574 need to evaluate the left hand side fully, so that all the
1575 variables it mentions get traced. */
c906108c
SS
1576 case BINOP_COMMA:
1577 (*pc)++;
1578 gen_expr (pc, ax, &value1);
1579 /* Don't just dispose of the left operand. We might be tracing,
c5aa993b
JM
1580 in which case we want to emit code to trace it if it's an
1581 lvalue. */
c906108c
SS
1582 gen_traced_pop (ax, &value1);
1583 gen_expr (pc, ax, value);
1584 /* It's the consumer's responsibility to trace the right operand. */
1585 break;
c5aa993b 1586
c906108c
SS
1587 case OP_LONG: /* some integer constant */
1588 {
1589 struct type *type = (*pc)[1].type;
1590 LONGEST k = (*pc)[2].longconst;
1591 (*pc) += 4;
1592 gen_int_literal (ax, value, k, type);
1593 }
c5aa993b 1594 break;
c906108c
SS
1595
1596 case OP_VAR_VALUE:
1597 gen_var_ref (ax, value, (*pc)[2].symbol);
1598 (*pc) += 4;
1599 break;
1600
1601 case OP_REGISTER:
1602 {
67f3407f
DJ
1603 const char *name = &(*pc)[2].string;
1604 int reg;
1605 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1606 reg = frame_map_name_to_regnum (deprecated_safe_get_selected_frame (),
1607 name, strlen (name));
1608 if (reg == -1)
1609 internal_error (__FILE__, __LINE__,
1610 _("Register $%s not available"), name);
c906108c
SS
1611 value->kind = axs_lvalue_register;
1612 value->u.reg = reg;
7b83296f 1613 value->type = register_type (current_gdbarch, reg);
c906108c 1614 }
c5aa993b 1615 break;
c906108c
SS
1616
1617 case OP_INTERNALVAR:
3d263c1d 1618 error (_("GDB agent expressions cannot use convenience variables."));
c906108c 1619
c5aa993b 1620 /* Weirdo operator: see comments for gen_repeat for details. */
c906108c
SS
1621 case BINOP_REPEAT:
1622 /* Note that gen_repeat handles its own argument evaluation. */
1623 (*pc)++;
1624 gen_repeat (pc, ax, value);
1625 break;
1626
1627 case UNOP_CAST:
1628 {
1629 struct type *type = (*pc)[1].type;
1630 (*pc) += 3;
1631 gen_expr (pc, ax, value);
1632 gen_cast (ax, value, type);
1633 }
c5aa993b 1634 break;
c906108c
SS
1635
1636 case UNOP_MEMVAL:
1637 {
1638 struct type *type = check_typedef ((*pc)[1].type);
1639 (*pc) += 3;
1640 gen_expr (pc, ax, value);
1641 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1642 it's just a hack for dealing with minsyms; you take some
1643 integer constant, pretend it's the address of an lvalue of
1644 the given type, and dereference it. */
1645 if (value->kind != axs_rvalue)
1646 /* This would be weird. */
8e65ff28 1647 internal_error (__FILE__, __LINE__,
3d263c1d 1648 _("gen_expr: OP_MEMVAL operand isn't an rvalue???"));
c906108c
SS
1649 value->type = type;
1650 value->kind = axs_lvalue_memory;
1651 }
c5aa993b 1652 break;
c906108c 1653
36e9969c
NS
1654 case UNOP_PLUS:
1655 (*pc)++;
1656 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
1657 gen_expr (pc, ax, value);
1658 gen_usual_unary (ax, value);
1659 break;
1660
c906108c
SS
1661 case UNOP_NEG:
1662 (*pc)++;
1663 /* -FOO is equivalent to 0 - FOO. */
1664 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
c5aa993b 1665 gen_usual_unary (ax, &value1); /* shouldn't do much */
c906108c
SS
1666 gen_expr (pc, ax, &value2);
1667 gen_usual_unary (ax, &value2);
1668 gen_usual_arithmetic (ax, &value1, &value2);
1669 gen_sub (ax, value, &value1, &value2);
1670 break;
1671
1672 case UNOP_LOGICAL_NOT:
1673 (*pc)++;
1674 gen_expr (pc, ax, value);
1675 gen_logical_not (ax, value);
1676 break;
1677
1678 case UNOP_COMPLEMENT:
1679 (*pc)++;
1680 gen_expr (pc, ax, value);
1681 gen_complement (ax, value);
1682 break;
1683
1684 case UNOP_IND:
1685 (*pc)++;
1686 gen_expr (pc, ax, value);
1687 gen_usual_unary (ax, value);
1688 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
3d263c1d 1689 error (_("Argument of unary `*' is not a pointer."));
c906108c
SS
1690 gen_deref (ax, value);
1691 break;
1692
1693 case UNOP_ADDR:
1694 (*pc)++;
1695 gen_expr (pc, ax, value);
1696 gen_address_of (ax, value);
1697 break;
1698
1699 case UNOP_SIZEOF:
1700 (*pc)++;
1701 /* Notice that gen_sizeof handles its own operand, unlike most
c5aa993b
JM
1702 of the other unary operator functions. This is because we
1703 have to throw away the code we generate. */
c906108c
SS
1704 gen_sizeof (pc, ax, value);
1705 break;
1706
1707 case STRUCTOP_STRUCT:
1708 case STRUCTOP_PTR:
1709 {
1710 int length = (*pc)[1].longconst;
1711 char *name = &(*pc)[2].string;
1712
1713 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1714 gen_expr (pc, ax, value);
1715 if (op == STRUCTOP_STRUCT)
1716 gen_struct_ref (ax, value, name, ".", "structure or union");
1717 else if (op == STRUCTOP_PTR)
1718 gen_struct_ref (ax, value, name, "->",
1719 "pointer to a structure or union");
1720 else
1721 /* If this `if' chain doesn't handle it, then the case list
c5aa993b 1722 shouldn't mention it, and we shouldn't be here. */
8e65ff28 1723 internal_error (__FILE__, __LINE__,
3d263c1d 1724 _("gen_expr: unhandled struct case"));
c906108c 1725 }
c5aa993b 1726 break;
c906108c
SS
1727
1728 case OP_TYPE:
3d263c1d 1729 error (_("Attempt to use a type name as an expression."));
c906108c
SS
1730
1731 default:
3d263c1d 1732 error (_("Unsupported operator in expression."));
c906108c
SS
1733 }
1734}
c906108c 1735\f
c5aa993b
JM
1736
1737
c906108c
SS
1738/* Generating bytecode from GDB expressions: driver */
1739
1740/* Given a GDB expression EXPR, produce a string of agent bytecode
1741 which computes its value. Return the agent expression, and set
1742 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1743struct agent_expr *
fba45db2 1744expr_to_agent (struct expression *expr, struct axs_value *value)
c906108c
SS
1745{
1746 struct cleanup *old_chain = 0;
6426a772 1747 struct agent_expr *ax = new_agent_expr (0);
c906108c
SS
1748 union exp_element *pc;
1749
f23d52e0 1750 old_chain = make_cleanup_free_agent_expr (ax);
c906108c
SS
1751
1752 pc = expr->elts;
1753 trace_kludge = 0;
1754 gen_expr (&pc, ax, value);
1755
1756 /* We have successfully built the agent expr, so cancel the cleanup
1757 request. If we add more cleanups that we always want done, this
1758 will have to get more complicated. */
1759 discard_cleanups (old_chain);
1760 return ax;
1761}
1762
1763
6426a772 1764#if 0 /* not used */
c906108c
SS
1765/* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1766 string of agent bytecode which will leave its address and size on
1767 the top of stack. Return the agent expression.
1768
1769 Not sure this function is useful at all. */
1770struct agent_expr *
fba45db2 1771expr_to_address_and_size (struct expression *expr)
c906108c
SS
1772{
1773 struct axs_value value;
1774 struct agent_expr *ax = expr_to_agent (expr, &value);
1775
1776 /* Complain if the result is not a memory lvalue. */
1777 if (value.kind != axs_lvalue_memory)
1778 {
1779 free_agent_expr (ax);
3d263c1d 1780 error (_("Expression does not denote an object in memory."));
c906108c
SS
1781 }
1782
1783 /* Push the object's size on the stack. */
1784 ax_const_l (ax, TYPE_LENGTH (value.type));
1785
1786 return ax;
1787}
6426a772 1788#endif
c906108c
SS
1789
1790/* Given a GDB expression EXPR, return bytecode to trace its value.
1791 The result will use the `trace' and `trace_quick' bytecodes to
1792 record the value of all memory touched by the expression. The
1793 caller can then use the ax_reqs function to discover which
1794 registers it relies upon. */
1795struct agent_expr *
fba45db2 1796gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
c906108c
SS
1797{
1798 struct cleanup *old_chain = 0;
1799 struct agent_expr *ax = new_agent_expr (scope);
1800 union exp_element *pc;
1801 struct axs_value value;
1802
f23d52e0 1803 old_chain = make_cleanup_free_agent_expr (ax);
c906108c
SS
1804
1805 pc = expr->elts;
1806 trace_kludge = 1;
1807 gen_expr (&pc, ax, &value);
1808
1809 /* Make sure we record the final object, and get rid of it. */
1810 gen_traced_pop (ax, &value);
1811
1812 /* Oh, and terminate. */
1813 ax_simple (ax, aop_end);
1814
1815 /* We have successfully built the agent expr, so cancel the cleanup
1816 request. If we add more cleanups that we always want done, this
1817 will have to get more complicated. */
1818 discard_cleanups (old_chain);
1819 return ax;
1820}
c906108c
SS
1821
1822static void
fba45db2 1823agent_command (char *exp, int from_tty)
c906108c
SS
1824{
1825 struct cleanup *old_chain = 0;
1826 struct expression *expr;
1827 struct agent_expr *agent;
6426a772 1828 struct frame_info *fi = get_current_frame (); /* need current scope */
c906108c
SS
1829
1830 /* We don't deal with overlay debugging at the moment. We need to
1831 think more carefully about this. If you copy this code into
1832 another command, change the error message; the user shouldn't
1833 have to know anything about agent expressions. */
1834 if (overlay_debugging)
3d263c1d 1835 error (_("GDB can't do agent expression translation with overlays."));
c906108c
SS
1836
1837 if (exp == 0)
3d263c1d 1838 error_no_arg (_("expression to translate"));
c5aa993b 1839
c906108c 1840 expr = parse_expression (exp);
c13c43fd 1841 old_chain = make_cleanup (free_current_contents, &expr);
bdd78e62 1842 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
f23d52e0 1843 make_cleanup_free_agent_expr (agent);
c906108c 1844 ax_print (gdb_stdout, agent);
085dd6e6
JM
1845
1846 /* It would be nice to call ax_reqs here to gather some general info
1847 about the expression, and then print out the result. */
c906108c
SS
1848
1849 do_cleanups (old_chain);
1850 dont_repeat ();
1851}
c906108c 1852\f
c5aa993b 1853
c906108c
SS
1854/* Initialization code. */
1855
a14ed312 1856void _initialize_ax_gdb (void);
c906108c 1857void
fba45db2 1858_initialize_ax_gdb (void)
c906108c 1859{
c906108c 1860 add_cmd ("agent", class_maintenance, agent_command,
3d263c1d 1861 _("Translate an expression into remote agent bytecode."),
c906108c
SS
1862 &maintenancelist);
1863}