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