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