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