]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ax-gdb.c
Update Copyright year range in all files maintained by GDB.
[thirdparty/binutils-gdb.git] / gdb / ax-gdb.c
1 /* GDB-specific functions for operating on agent expressions.
2
3 Copyright (C) 1998-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "symfile.h"
23 #include "gdbtypes.h"
24 #include "language.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 <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 "breakpoint.h"
40 #include "tracepoint.h"
41 #include "cp-support.h"
42 #include "arch-utils.h"
43 #include "cli/cli-utils.h"
44 #include "linespec.h"
45
46 #include "valprint.h"
47 #include "c-lang.h"
48
49 #include "format.h"
50
51 /* To make sense of this file, you should read doc/agentexpr.texi.
52 Then look at the types and enums in ax-gdb.h. For the code itself,
53 look at gen_expr, towards the bottom; that's the main function that
54 looks at the GDB expressions and calls everything else to generate
55 code.
56
57 I'm beginning to wonder whether it wouldn't be nicer to internally
58 generate trees, with types, and then spit out the bytecode in
59 linear form afterwards; we could generate fewer `swap', `ext', and
60 `zero_ext' bytecodes that way; it would make good constant folding
61 easier, too. But at the moment, I think we should be willing to
62 pay for the simplicity of this code with less-than-optimal bytecode
63 strings.
64
65 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
66 \f
67
68
69 /* Prototypes for local functions. */
70
71 /* There's a standard order to the arguments of these functions:
72 union exp_element ** --- pointer into expression
73 struct agent_expr * --- agent expression buffer to generate code into
74 struct axs_value * --- describes value left on top of stack */
75
76 static struct value *const_var_ref (struct symbol *var);
77 static struct value *const_expr (union exp_element **pc);
78 static struct value *maybe_const_expr (union exp_element **pc);
79
80 static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
81 struct axs_value *);
82
83 static void gen_sign_extend (struct agent_expr *, struct type *);
84 static void gen_extend (struct agent_expr *, struct type *);
85 static void gen_fetch (struct agent_expr *, struct type *);
86 static void gen_left_shift (struct agent_expr *, int);
87
88
89 static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
90 static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
91 static void gen_offset (struct agent_expr *ax, int offset);
92 static void gen_sym_offset (struct agent_expr *, struct symbol *);
93 static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
94 struct axs_value *value, struct symbol *var);
95
96
97 static void gen_int_literal (struct agent_expr *ax,
98 struct axs_value *value,
99 LONGEST k, struct type *type);
100
101 static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
102 struct axs_value *value);
103 static int type_wider_than (struct type *type1, struct type *type2);
104 static struct type *max_type (struct type *type1, struct type *type2);
105 static void gen_conversion (struct agent_expr *ax,
106 struct type *from, struct type *to);
107 static int is_nontrivial_conversion (struct type *from, struct type *to);
108 static void gen_usual_arithmetic (struct expression *exp,
109 struct agent_expr *ax,
110 struct axs_value *value1,
111 struct axs_value *value2);
112 static void gen_integral_promotions (struct expression *exp,
113 struct agent_expr *ax,
114 struct axs_value *value);
115 static void gen_cast (struct agent_expr *ax,
116 struct axs_value *value, struct type *type);
117 static void gen_scale (struct agent_expr *ax,
118 enum agent_op op, struct type *type);
119 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
120 struct axs_value *value1, struct axs_value *value2);
121 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
122 struct axs_value *value1, struct axs_value *value2);
123 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
124 struct axs_value *value1, struct axs_value *value2,
125 struct type *result_type);
126 static void gen_binop (struct agent_expr *ax,
127 struct axs_value *value,
128 struct axs_value *value1,
129 struct axs_value *value2,
130 enum agent_op op,
131 enum agent_op op_unsigned, int may_carry, char *name);
132 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
133 struct type *result_type);
134 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
135 static void gen_deref (struct agent_expr *, struct axs_value *);
136 static void gen_address_of (struct agent_expr *, struct axs_value *);
137 static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
138 struct axs_value *value,
139 struct type *type, int start, int end);
140 static void gen_primitive_field (struct expression *exp,
141 struct agent_expr *ax,
142 struct axs_value *value,
143 int offset, int fieldno, struct type *type);
144 static int gen_struct_ref_recursive (struct expression *exp,
145 struct agent_expr *ax,
146 struct axs_value *value,
147 char *field, int offset,
148 struct type *type);
149 static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
150 struct axs_value *value,
151 char *field,
152 char *operator_name, char *operand_name);
153 static void gen_static_field (struct gdbarch *gdbarch,
154 struct agent_expr *ax, struct axs_value *value,
155 struct type *type, int fieldno);
156 static void gen_repeat (struct expression *exp, union exp_element **pc,
157 struct agent_expr *ax, struct axs_value *value);
158 static void gen_sizeof (struct expression *exp, union exp_element **pc,
159 struct agent_expr *ax, struct axs_value *value,
160 struct type *size_type);
161 static void gen_expr_binop_rest (struct expression *exp,
162 enum exp_opcode op, union exp_element **pc,
163 struct agent_expr *ax,
164 struct axs_value *value,
165 struct axs_value *value1,
166 struct axs_value *value2);
167
168 static void agent_command (char *exp, int from_tty);
169 \f
170
171 /* Detecting constant expressions. */
172
173 /* If the variable reference at *PC is a constant, return its value.
174 Otherwise, return zero.
175
176 Hey, Wally! How can a variable reference be a constant?
177
178 Well, Beav, this function really handles the OP_VAR_VALUE operator,
179 not specifically variable references. GDB uses OP_VAR_VALUE to
180 refer to any kind of symbolic reference: function names, enum
181 elements, and goto labels are all handled through the OP_VAR_VALUE
182 operator, even though they're constants. It makes sense given the
183 situation.
184
185 Gee, Wally, don'cha wonder sometimes if data representations that
186 subvert commonly accepted definitions of terms in favor of heavily
187 context-specific interpretations are really just a tool of the
188 programming hegemony to preserve their power and exclude the
189 proletariat? */
190
191 static struct value *
192 const_var_ref (struct symbol *var)
193 {
194 struct type *type = SYMBOL_TYPE (var);
195
196 switch (SYMBOL_CLASS (var))
197 {
198 case LOC_CONST:
199 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
200
201 case LOC_LABEL:
202 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
203
204 default:
205 return 0;
206 }
207 }
208
209
210 /* If the expression starting at *PC has a constant value, return it.
211 Otherwise, return zero. If we return a value, then *PC will be
212 advanced to the end of it. If we return zero, *PC could be
213 anywhere. */
214 static struct value *
215 const_expr (union exp_element **pc)
216 {
217 enum exp_opcode op = (*pc)->opcode;
218 struct value *v1;
219
220 switch (op)
221 {
222 case OP_LONG:
223 {
224 struct type *type = (*pc)[1].type;
225 LONGEST k = (*pc)[2].longconst;
226
227 (*pc) += 4;
228 return value_from_longest (type, k);
229 }
230
231 case OP_VAR_VALUE:
232 {
233 struct value *v = const_var_ref ((*pc)[2].symbol);
234
235 (*pc) += 4;
236 return v;
237 }
238
239 /* We could add more operators in here. */
240
241 case UNOP_NEG:
242 (*pc)++;
243 v1 = const_expr (pc);
244 if (v1)
245 return value_neg (v1);
246 else
247 return 0;
248
249 default:
250 return 0;
251 }
252 }
253
254
255 /* Like const_expr, but guarantee also that *PC is undisturbed if the
256 expression is not constant. */
257 static struct value *
258 maybe_const_expr (union exp_element **pc)
259 {
260 union exp_element *tentative_pc = *pc;
261 struct value *v = const_expr (&tentative_pc);
262
263 /* If we got a value, then update the real PC. */
264 if (v)
265 *pc = tentative_pc;
266
267 return v;
268 }
269 \f
270
271 /* Generating bytecode from GDB expressions: general assumptions */
272
273 /* Here are a few general assumptions made throughout the code; if you
274 want to make a change that contradicts one of these, then you'd
275 better scan things pretty thoroughly.
276
277 - We assume that all values occupy one stack element. For example,
278 sometimes we'll swap to get at the left argument to a binary
279 operator. If we decide that void values should occupy no stack
280 elements, or that synthetic arrays (whose size is determined at
281 run time, created by the `@' operator) should occupy two stack
282 elements (address and length), then this will cause trouble.
283
284 - We assume the stack elements are infinitely wide, and that we
285 don't have to worry what happens if the user requests an
286 operation that is wider than the actual interpreter's stack.
287 That is, it's up to the interpreter to handle directly all the
288 integer widths the user has access to. (Woe betide the language
289 with bignums!)
290
291 - We don't support side effects. Thus, we don't have to worry about
292 GCC's generalized lvalues, function calls, etc.
293
294 - We don't support floating point. Many places where we switch on
295 some type don't bother to include cases for floating point; there
296 may be even more subtle ways this assumption exists. For
297 example, the arguments to % must be integers.
298
299 - We assume all subexpressions have a static, unchanging type. If
300 we tried to support convenience variables, this would be a
301 problem.
302
303 - All values on the stack should always be fully zero- or
304 sign-extended.
305
306 (I wasn't sure whether to choose this or its opposite --- that
307 only addresses are assumed extended --- but it turns out that
308 neither convention completely eliminates spurious extend
309 operations (if everything is always extended, then you have to
310 extend after add, because it could overflow; if nothing is
311 extended, then you end up producing extends whenever you change
312 sizes), and this is simpler.) */
313 \f
314
315 /* Scan for all static fields in the given class, including any base
316 classes, and generate tracing bytecodes for each. */
317
318 static void
319 gen_trace_static_fields (struct gdbarch *gdbarch,
320 struct agent_expr *ax,
321 struct type *type)
322 {
323 int i, nbases = TYPE_N_BASECLASSES (type);
324 struct axs_value value;
325
326 CHECK_TYPEDEF (type);
327
328 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
329 {
330 if (field_is_static (&TYPE_FIELD (type, i)))
331 {
332 gen_static_field (gdbarch, ax, &value, type, i);
333 if (value.optimized_out)
334 continue;
335 switch (value.kind)
336 {
337 case axs_lvalue_memory:
338 {
339 /* Initialize the TYPE_LENGTH if it is a typedef. */
340 check_typedef (value.type);
341 ax_const_l (ax, TYPE_LENGTH (value.type));
342 ax_simple (ax, aop_trace);
343 }
344 break;
345
346 case axs_lvalue_register:
347 /* We don't actually need the register's value to be pushed,
348 just note that we need it to be collected. */
349 ax_reg_mask (ax, value.u.reg);
350
351 default:
352 break;
353 }
354 }
355 }
356
357 /* Now scan through base classes recursively. */
358 for (i = 0; i < nbases; i++)
359 {
360 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
361
362 gen_trace_static_fields (gdbarch, ax, basetype);
363 }
364 }
365
366 /* Trace the lvalue on the stack, if it needs it. In either case, pop
367 the value. Useful on the left side of a comma, and at the end of
368 an expression being used for tracing. */
369 static void
370 gen_traced_pop (struct gdbarch *gdbarch,
371 struct agent_expr *ax, struct axs_value *value)
372 {
373 int string_trace = 0;
374 if (ax->trace_string
375 && TYPE_CODE (value->type) == TYPE_CODE_PTR
376 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
377 's'))
378 string_trace = 1;
379
380 if (ax->tracing)
381 switch (value->kind)
382 {
383 case axs_rvalue:
384 if (string_trace)
385 {
386 ax_const_l (ax, ax->trace_string);
387 ax_simple (ax, aop_tracenz);
388 }
389 else
390 /* We don't trace rvalues, just the lvalues necessary to
391 produce them. So just dispose of this value. */
392 ax_simple (ax, aop_pop);
393 break;
394
395 case axs_lvalue_memory:
396 {
397 if (string_trace)
398 ax_simple (ax, aop_dup);
399
400 /* Initialize the TYPE_LENGTH if it is a typedef. */
401 check_typedef (value->type);
402
403 /* There's no point in trying to use a trace_quick bytecode
404 here, since "trace_quick SIZE pop" is three bytes, whereas
405 "const8 SIZE trace" is also three bytes, does the same
406 thing, and the simplest code which generates that will also
407 work correctly for objects with large sizes. */
408 ax_const_l (ax, TYPE_LENGTH (value->type));
409 ax_simple (ax, aop_trace);
410
411 if (string_trace)
412 {
413 ax_simple (ax, aop_ref32);
414 ax_const_l (ax, ax->trace_string);
415 ax_simple (ax, aop_tracenz);
416 }
417 }
418 break;
419
420 case axs_lvalue_register:
421 /* We don't actually need the register's value to be on the
422 stack, and the target will get heartburn if the register is
423 larger than will fit in a stack, so just mark it for
424 collection and be done with it. */
425 ax_reg_mask (ax, value->u.reg);
426
427 /* But if the register points to a string, assume the value
428 will fit on the stack and push it anyway. */
429 if (string_trace)
430 {
431 ax_reg (ax, value->u.reg);
432 ax_const_l (ax, ax->trace_string);
433 ax_simple (ax, aop_tracenz);
434 }
435 break;
436 }
437 else
438 /* If we're not tracing, just pop the value. */
439 ax_simple (ax, aop_pop);
440
441 /* To trace C++ classes with static fields stored elsewhere. */
442 if (ax->tracing
443 && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
444 || TYPE_CODE (value->type) == TYPE_CODE_UNION))
445 gen_trace_static_fields (gdbarch, ax, value->type);
446 }
447 \f
448
449
450 /* Generating bytecode from GDB expressions: helper functions */
451
452 /* Assume that the lower bits of the top of the stack is a value of
453 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
454 static void
455 gen_sign_extend (struct agent_expr *ax, struct type *type)
456 {
457 /* Do we need to sign-extend this? */
458 if (!TYPE_UNSIGNED (type))
459 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
460 }
461
462
463 /* Assume the lower bits of the top of the stack hold a value of type
464 TYPE, and the upper bits are garbage. Sign-extend or truncate as
465 needed. */
466 static void
467 gen_extend (struct agent_expr *ax, struct type *type)
468 {
469 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
470
471 /* I just had to. */
472 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
473 }
474
475
476 /* Assume that the top of the stack contains a value of type "pointer
477 to TYPE"; generate code to fetch its value. Note that TYPE is the
478 target type, not the pointer type. */
479 static void
480 gen_fetch (struct agent_expr *ax, struct type *type)
481 {
482 if (ax->tracing)
483 {
484 /* Record the area of memory we're about to fetch. */
485 ax_trace_quick (ax, TYPE_LENGTH (type));
486 }
487
488 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
489 type = TYPE_TARGET_TYPE (type);
490
491 switch (TYPE_CODE (type))
492 {
493 case TYPE_CODE_PTR:
494 case TYPE_CODE_REF:
495 case TYPE_CODE_ENUM:
496 case TYPE_CODE_INT:
497 case TYPE_CODE_CHAR:
498 case TYPE_CODE_BOOL:
499 /* It's a scalar value, so we know how to dereference it. How
500 many bytes long is it? */
501 switch (TYPE_LENGTH (type))
502 {
503 case 8 / TARGET_CHAR_BIT:
504 ax_simple (ax, aop_ref8);
505 break;
506 case 16 / TARGET_CHAR_BIT:
507 ax_simple (ax, aop_ref16);
508 break;
509 case 32 / TARGET_CHAR_BIT:
510 ax_simple (ax, aop_ref32);
511 break;
512 case 64 / TARGET_CHAR_BIT:
513 ax_simple (ax, aop_ref64);
514 break;
515
516 /* Either our caller shouldn't have asked us to dereference
517 that pointer (other code's fault), or we're not
518 implementing something we should be (this code's fault).
519 In any case, it's a bug the user shouldn't see. */
520 default:
521 internal_error (__FILE__, __LINE__,
522 _("gen_fetch: strange size"));
523 }
524
525 gen_sign_extend (ax, type);
526 break;
527
528 default:
529 /* Our caller requested us to dereference a pointer from an unsupported
530 type. Error out and give callers a chance to handle the failure
531 gracefully. */
532 error (_("gen_fetch: Unsupported type code `%s'."),
533 TYPE_NAME (type));
534 }
535 }
536
537
538 /* Generate code to left shift the top of the stack by DISTANCE bits, or
539 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
540 unsigned (logical) right shifts. */
541 static void
542 gen_left_shift (struct agent_expr *ax, int distance)
543 {
544 if (distance > 0)
545 {
546 ax_const_l (ax, distance);
547 ax_simple (ax, aop_lsh);
548 }
549 else if (distance < 0)
550 {
551 ax_const_l (ax, -distance);
552 ax_simple (ax, aop_rsh_unsigned);
553 }
554 }
555 \f
556
557
558 /* Generating bytecode from GDB expressions: symbol references */
559
560 /* Generate code to push the base address of the argument portion of
561 the top stack frame. */
562 static void
563 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
564 {
565 int frame_reg;
566 LONGEST frame_offset;
567
568 gdbarch_virtual_frame_pointer (gdbarch,
569 ax->scope, &frame_reg, &frame_offset);
570 ax_reg (ax, frame_reg);
571 gen_offset (ax, frame_offset);
572 }
573
574
575 /* Generate code to push the base address of the locals portion of the
576 top stack frame. */
577 static void
578 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
579 {
580 int frame_reg;
581 LONGEST frame_offset;
582
583 gdbarch_virtual_frame_pointer (gdbarch,
584 ax->scope, &frame_reg, &frame_offset);
585 ax_reg (ax, frame_reg);
586 gen_offset (ax, frame_offset);
587 }
588
589
590 /* Generate code to add OFFSET to the top of the stack. Try to
591 generate short and readable code. We use this for getting to
592 variables on the stack, and structure members. If we were
593 programming in ML, it would be clearer why these are the same
594 thing. */
595 static void
596 gen_offset (struct agent_expr *ax, int offset)
597 {
598 /* It would suffice to simply push the offset and add it, but this
599 makes it easier to read positive and negative offsets in the
600 bytecode. */
601 if (offset > 0)
602 {
603 ax_const_l (ax, offset);
604 ax_simple (ax, aop_add);
605 }
606 else if (offset < 0)
607 {
608 ax_const_l (ax, -offset);
609 ax_simple (ax, aop_sub);
610 }
611 }
612
613
614 /* In many cases, a symbol's value is the offset from some other
615 address (stack frame, base register, etc.) Generate code to add
616 VAR's value to the top of the stack. */
617 static void
618 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
619 {
620 gen_offset (ax, SYMBOL_VALUE (var));
621 }
622
623
624 /* Generate code for a variable reference to AX. The variable is the
625 symbol VAR. Set VALUE to describe the result. */
626
627 static void
628 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
629 struct axs_value *value, struct symbol *var)
630 {
631 /* Dereference any typedefs. */
632 value->type = check_typedef (SYMBOL_TYPE (var));
633 value->optimized_out = 0;
634
635 if (SYMBOL_COMPUTED_OPS (var) != NULL)
636 {
637 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
638 return;
639 }
640
641 /* I'm imitating the code in read_var_value. */
642 switch (SYMBOL_CLASS (var))
643 {
644 case LOC_CONST: /* A constant, like an enum value. */
645 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
646 value->kind = axs_rvalue;
647 break;
648
649 case LOC_LABEL: /* A goto label, being used as a value. */
650 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
651 value->kind = axs_rvalue;
652 break;
653
654 case LOC_CONST_BYTES:
655 internal_error (__FILE__, __LINE__,
656 _("gen_var_ref: LOC_CONST_BYTES "
657 "symbols are not supported"));
658
659 /* Variable at a fixed location in memory. Easy. */
660 case LOC_STATIC:
661 /* Push the address of the variable. */
662 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
663 value->kind = axs_lvalue_memory;
664 break;
665
666 case LOC_ARG: /* var lives in argument area of frame */
667 gen_frame_args_address (gdbarch, ax);
668 gen_sym_offset (ax, var);
669 value->kind = axs_lvalue_memory;
670 break;
671
672 case LOC_REF_ARG: /* As above, but the frame slot really
673 holds the address of the variable. */
674 gen_frame_args_address (gdbarch, ax);
675 gen_sym_offset (ax, var);
676 /* Don't assume any particular pointer size. */
677 gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
678 value->kind = axs_lvalue_memory;
679 break;
680
681 case LOC_LOCAL: /* var lives in locals area of frame */
682 gen_frame_locals_address (gdbarch, ax);
683 gen_sym_offset (ax, var);
684 value->kind = axs_lvalue_memory;
685 break;
686
687 case LOC_TYPEDEF:
688 error (_("Cannot compute value of typedef `%s'."),
689 SYMBOL_PRINT_NAME (var));
690 break;
691
692 case LOC_BLOCK:
693 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
694 value->kind = axs_rvalue;
695 break;
696
697 case LOC_REGISTER:
698 /* Don't generate any code at all; in the process of treating
699 this as an lvalue or rvalue, the caller will generate the
700 right code. */
701 value->kind = axs_lvalue_register;
702 value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
703 break;
704
705 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
706 register, not on the stack. Simpler than LOC_REGISTER
707 because it's just like any other case where the thing
708 has a real address. */
709 case LOC_REGPARM_ADDR:
710 ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
711 value->kind = axs_lvalue_memory;
712 break;
713
714 case LOC_UNRESOLVED:
715 {
716 struct minimal_symbol *msym
717 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
718
719 if (!msym)
720 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
721
722 /* Push the address of the variable. */
723 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
724 value->kind = axs_lvalue_memory;
725 }
726 break;
727
728 case LOC_COMPUTED:
729 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
730
731 case LOC_OPTIMIZED_OUT:
732 /* Flag this, but don't say anything; leave it up to callers to
733 warn the user. */
734 value->optimized_out = 1;
735 break;
736
737 default:
738 error (_("Cannot find value of botched symbol `%s'."),
739 SYMBOL_PRINT_NAME (var));
740 break;
741 }
742 }
743 \f
744
745
746 /* Generating bytecode from GDB expressions: literals */
747
748 static void
749 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
750 struct type *type)
751 {
752 ax_const_l (ax, k);
753 value->kind = axs_rvalue;
754 value->type = check_typedef (type);
755 }
756 \f
757
758
759 /* Generating bytecode from GDB expressions: unary conversions, casts */
760
761 /* Take what's on the top of the stack (as described by VALUE), and
762 try to make an rvalue out of it. Signal an error if we can't do
763 that. */
764 void
765 require_rvalue (struct agent_expr *ax, struct axs_value *value)
766 {
767 /* Only deal with scalars, structs and such may be too large
768 to fit in a stack entry. */
769 value->type = check_typedef (value->type);
770 if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
771 || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
772 || TYPE_CODE (value->type) == TYPE_CODE_UNION
773 || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
774 error (_("Value not scalar: cannot be an rvalue."));
775
776 switch (value->kind)
777 {
778 case axs_rvalue:
779 /* It's already an rvalue. */
780 break;
781
782 case axs_lvalue_memory:
783 /* The top of stack is the address of the object. Dereference. */
784 gen_fetch (ax, value->type);
785 break;
786
787 case axs_lvalue_register:
788 /* There's nothing on the stack, but value->u.reg is the
789 register number containing the value.
790
791 When we add floating-point support, this is going to have to
792 change. What about SPARC register pairs, for example? */
793 ax_reg (ax, value->u.reg);
794 gen_extend (ax, value->type);
795 break;
796 }
797
798 value->kind = axs_rvalue;
799 }
800
801
802 /* Assume the top of the stack is described by VALUE, and perform the
803 usual unary conversions. This is motivated by ANSI 6.2.2, but of
804 course GDB expressions are not ANSI; they're the mishmash union of
805 a bunch of languages. Rah.
806
807 NOTE! This function promises to produce an rvalue only when the
808 incoming value is of an appropriate type. In other words, the
809 consumer of the value this function produces may assume the value
810 is an rvalue only after checking its type.
811
812 The immediate issue is that if the user tries to use a structure or
813 union as an operand of, say, the `+' operator, we don't want to try
814 to convert that structure to an rvalue; require_rvalue will bomb on
815 structs and unions. Rather, we want to simply pass the struct
816 lvalue through unchanged, and let `+' raise an error. */
817
818 static void
819 gen_usual_unary (struct expression *exp, struct agent_expr *ax,
820 struct axs_value *value)
821 {
822 /* We don't have to generate any code for the usual integral
823 conversions, since values are always represented as full-width on
824 the stack. Should we tweak the type? */
825
826 /* Some types require special handling. */
827 switch (TYPE_CODE (value->type))
828 {
829 /* Functions get converted to a pointer to the function. */
830 case TYPE_CODE_FUNC:
831 value->type = lookup_pointer_type (value->type);
832 value->kind = axs_rvalue; /* Should always be true, but just in case. */
833 break;
834
835 /* Arrays get converted to a pointer to their first element, and
836 are no longer an lvalue. */
837 case TYPE_CODE_ARRAY:
838 {
839 struct type *elements = TYPE_TARGET_TYPE (value->type);
840
841 value->type = lookup_pointer_type (elements);
842 value->kind = axs_rvalue;
843 /* We don't need to generate any code; the address of the array
844 is also the address of its first element. */
845 }
846 break;
847
848 /* Don't try to convert structures and unions to rvalues. Let the
849 consumer signal an error. */
850 case TYPE_CODE_STRUCT:
851 case TYPE_CODE_UNION:
852 return;
853 }
854
855 /* If the value is an lvalue, dereference it. */
856 require_rvalue (ax, value);
857 }
858
859
860 /* Return non-zero iff the type TYPE1 is considered "wider" than the
861 type TYPE2, according to the rules described in gen_usual_arithmetic. */
862 static int
863 type_wider_than (struct type *type1, struct type *type2)
864 {
865 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
866 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
867 && TYPE_UNSIGNED (type1)
868 && !TYPE_UNSIGNED (type2)));
869 }
870
871
872 /* Return the "wider" of the two types TYPE1 and TYPE2. */
873 static struct type *
874 max_type (struct type *type1, struct type *type2)
875 {
876 return type_wider_than (type1, type2) ? type1 : type2;
877 }
878
879
880 /* Generate code to convert a scalar value of type FROM to type TO. */
881 static void
882 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
883 {
884 /* Perhaps there is a more graceful way to state these rules. */
885
886 /* If we're converting to a narrower type, then we need to clear out
887 the upper bits. */
888 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
889 gen_extend (ax, from);
890
891 /* If the two values have equal width, but different signednesses,
892 then we need to extend. */
893 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
894 {
895 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
896 gen_extend (ax, to);
897 }
898
899 /* If we're converting to a wider type, and becoming unsigned, then
900 we need to zero out any possible sign bits. */
901 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
902 {
903 if (TYPE_UNSIGNED (to))
904 gen_extend (ax, to);
905 }
906 }
907
908
909 /* Return non-zero iff the type FROM will require any bytecodes to be
910 emitted to be converted to the type TO. */
911 static int
912 is_nontrivial_conversion (struct type *from, struct type *to)
913 {
914 struct agent_expr *ax = new_agent_expr (NULL, 0);
915 int nontrivial;
916
917 /* Actually generate the code, and see if anything came out. At the
918 moment, it would be trivial to replicate the code in
919 gen_conversion here, but in the future, when we're supporting
920 floating point and the like, it may not be. Doing things this
921 way allows this function to be independent of the logic in
922 gen_conversion. */
923 gen_conversion (ax, from, to);
924 nontrivial = ax->len > 0;
925 free_agent_expr (ax);
926 return nontrivial;
927 }
928
929
930 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
931 6.2.1.5) for the two operands of an arithmetic operator. This
932 effectively finds a "least upper bound" type for the two arguments,
933 and promotes each argument to that type. *VALUE1 and *VALUE2
934 describe the values as they are passed in, and as they are left. */
935 static void
936 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
937 struct axs_value *value1, struct axs_value *value2)
938 {
939 /* Do the usual binary conversions. */
940 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
941 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
942 {
943 /* The ANSI integral promotions seem to work this way: Order the
944 integer types by size, and then by signedness: an n-bit
945 unsigned type is considered "wider" than an n-bit signed
946 type. Promote to the "wider" of the two types, and always
947 promote at least to int. */
948 struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
949 max_type (value1->type, value2->type));
950
951 /* Deal with value2, on the top of the stack. */
952 gen_conversion (ax, value2->type, target);
953
954 /* Deal with value1, not on the top of the stack. Don't
955 generate the `swap' instructions if we're not actually going
956 to do anything. */
957 if (is_nontrivial_conversion (value1->type, target))
958 {
959 ax_simple (ax, aop_swap);
960 gen_conversion (ax, value1->type, target);
961 ax_simple (ax, aop_swap);
962 }
963
964 value1->type = value2->type = check_typedef (target);
965 }
966 }
967
968
969 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
970 the value on the top of the stack, as described by VALUE. Assume
971 the value has integral type. */
972 static void
973 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
974 struct axs_value *value)
975 {
976 const struct builtin_type *builtin = builtin_type (exp->gdbarch);
977
978 if (!type_wider_than (value->type, builtin->builtin_int))
979 {
980 gen_conversion (ax, value->type, builtin->builtin_int);
981 value->type = builtin->builtin_int;
982 }
983 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
984 {
985 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
986 value->type = builtin->builtin_unsigned_int;
987 }
988 }
989
990
991 /* Generate code for a cast to TYPE. */
992 static void
993 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
994 {
995 /* GCC does allow casts to yield lvalues, so this should be fixed
996 before merging these changes into the trunk. */
997 require_rvalue (ax, value);
998 /* Dereference typedefs. */
999 type = check_typedef (type);
1000
1001 switch (TYPE_CODE (type))
1002 {
1003 case TYPE_CODE_PTR:
1004 case TYPE_CODE_REF:
1005 /* It's implementation-defined, and I'll bet this is what GCC
1006 does. */
1007 break;
1008
1009 case TYPE_CODE_ARRAY:
1010 case TYPE_CODE_STRUCT:
1011 case TYPE_CODE_UNION:
1012 case TYPE_CODE_FUNC:
1013 error (_("Invalid type cast: intended type must be scalar."));
1014
1015 case TYPE_CODE_ENUM:
1016 case TYPE_CODE_BOOL:
1017 /* We don't have to worry about the size of the value, because
1018 all our integral values are fully sign-extended, and when
1019 casting pointers we can do anything we like. Is there any
1020 way for us to know what GCC actually does with a cast like
1021 this? */
1022 break;
1023
1024 case TYPE_CODE_INT:
1025 gen_conversion (ax, value->type, type);
1026 break;
1027
1028 case TYPE_CODE_VOID:
1029 /* We could pop the value, and rely on everyone else to check
1030 the type and notice that this value doesn't occupy a stack
1031 slot. But for now, leave the value on the stack, and
1032 preserve the "value == stack element" assumption. */
1033 break;
1034
1035 default:
1036 error (_("Casts to requested type are not yet implemented."));
1037 }
1038
1039 value->type = type;
1040 }
1041 \f
1042
1043
1044 /* Generating bytecode from GDB expressions: arithmetic */
1045
1046 /* Scale the integer on the top of the stack by the size of the target
1047 of the pointer type TYPE. */
1048 static void
1049 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1050 {
1051 struct type *element = TYPE_TARGET_TYPE (type);
1052
1053 if (TYPE_LENGTH (element) != 1)
1054 {
1055 ax_const_l (ax, TYPE_LENGTH (element));
1056 ax_simple (ax, op);
1057 }
1058 }
1059
1060
1061 /* Generate code for pointer arithmetic PTR + INT. */
1062 static void
1063 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1064 struct axs_value *value1, struct axs_value *value2)
1065 {
1066 gdb_assert (pointer_type (value1->type));
1067 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1068
1069 gen_scale (ax, aop_mul, value1->type);
1070 ax_simple (ax, aop_add);
1071 gen_extend (ax, value1->type); /* Catch overflow. */
1072 value->type = value1->type;
1073 value->kind = axs_rvalue;
1074 }
1075
1076
1077 /* Generate code for pointer arithmetic PTR - INT. */
1078 static void
1079 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1080 struct axs_value *value1, struct axs_value *value2)
1081 {
1082 gdb_assert (pointer_type (value1->type));
1083 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1084
1085 gen_scale (ax, aop_mul, value1->type);
1086 ax_simple (ax, aop_sub);
1087 gen_extend (ax, value1->type); /* Catch overflow. */
1088 value->type = value1->type;
1089 value->kind = axs_rvalue;
1090 }
1091
1092
1093 /* Generate code for pointer arithmetic PTR - PTR. */
1094 static void
1095 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1096 struct axs_value *value1, struct axs_value *value2,
1097 struct type *result_type)
1098 {
1099 gdb_assert (pointer_type (value1->type));
1100 gdb_assert (pointer_type (value2->type));
1101
1102 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1103 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1104 error (_("\
1105 First argument of `-' is a pointer, but second argument is neither\n\
1106 an integer nor a pointer of the same type."));
1107
1108 ax_simple (ax, aop_sub);
1109 gen_scale (ax, aop_div_unsigned, value1->type);
1110 value->type = result_type;
1111 value->kind = axs_rvalue;
1112 }
1113
1114 static void
1115 gen_equal (struct agent_expr *ax, struct axs_value *value,
1116 struct axs_value *value1, struct axs_value *value2,
1117 struct type *result_type)
1118 {
1119 if (pointer_type (value1->type) || pointer_type (value2->type))
1120 ax_simple (ax, aop_equal);
1121 else
1122 gen_binop (ax, value, value1, value2,
1123 aop_equal, aop_equal, 0, "equal");
1124 value->type = result_type;
1125 value->kind = axs_rvalue;
1126 }
1127
1128 static void
1129 gen_less (struct agent_expr *ax, struct axs_value *value,
1130 struct axs_value *value1, struct axs_value *value2,
1131 struct type *result_type)
1132 {
1133 if (pointer_type (value1->type) || pointer_type (value2->type))
1134 ax_simple (ax, aop_less_unsigned);
1135 else
1136 gen_binop (ax, value, value1, value2,
1137 aop_less_signed, aop_less_unsigned, 0, "less than");
1138 value->type = result_type;
1139 value->kind = axs_rvalue;
1140 }
1141
1142 /* Generate code for a binary operator that doesn't do pointer magic.
1143 We set VALUE to describe the result value; we assume VALUE1 and
1144 VALUE2 describe the two operands, and that they've undergone the
1145 usual binary conversions. MAY_CARRY should be non-zero iff the
1146 result needs to be extended. NAME is the English name of the
1147 operator, used in error messages */
1148 static void
1149 gen_binop (struct agent_expr *ax, struct axs_value *value,
1150 struct axs_value *value1, struct axs_value *value2,
1151 enum agent_op op, enum agent_op op_unsigned,
1152 int may_carry, char *name)
1153 {
1154 /* We only handle INT op INT. */
1155 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1156 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1157 error (_("Invalid combination of types in %s."), name);
1158
1159 ax_simple (ax,
1160 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1161 if (may_carry)
1162 gen_extend (ax, value1->type); /* catch overflow */
1163 value->type = value1->type;
1164 value->kind = axs_rvalue;
1165 }
1166
1167
1168 static void
1169 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1170 struct type *result_type)
1171 {
1172 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1173 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1174 error (_("Invalid type of operand to `!'."));
1175
1176 ax_simple (ax, aop_log_not);
1177 value->type = result_type;
1178 }
1179
1180
1181 static void
1182 gen_complement (struct agent_expr *ax, struct axs_value *value)
1183 {
1184 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1185 error (_("Invalid type of operand to `~'."));
1186
1187 ax_simple (ax, aop_bit_not);
1188 gen_extend (ax, value->type);
1189 }
1190 \f
1191
1192
1193 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1194
1195 /* Dereference the value on the top of the stack. */
1196 static void
1197 gen_deref (struct agent_expr *ax, struct axs_value *value)
1198 {
1199 /* The caller should check the type, because several operators use
1200 this, and we don't know what error message to generate. */
1201 if (!pointer_type (value->type))
1202 internal_error (__FILE__, __LINE__,
1203 _("gen_deref: expected a pointer"));
1204
1205 /* We've got an rvalue now, which is a pointer. We want to yield an
1206 lvalue, whose address is exactly that pointer. So we don't
1207 actually emit any code; we just change the type from "Pointer to
1208 T" to "T", and mark the value as an lvalue in memory. Leave it
1209 to the consumer to actually dereference it. */
1210 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1211 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1212 error (_("Attempt to dereference a generic pointer."));
1213 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1214 ? axs_rvalue : axs_lvalue_memory);
1215 }
1216
1217
1218 /* Produce the address of the lvalue on the top of the stack. */
1219 static void
1220 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1221 {
1222 /* Special case for taking the address of a function. The ANSI
1223 standard describes this as a special case, too, so this
1224 arrangement is not without motivation. */
1225 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1226 /* The value's already an rvalue on the stack, so we just need to
1227 change the type. */
1228 value->type = lookup_pointer_type (value->type);
1229 else
1230 switch (value->kind)
1231 {
1232 case axs_rvalue:
1233 error (_("Operand of `&' is an rvalue, which has no address."));
1234
1235 case axs_lvalue_register:
1236 error (_("Operand of `&' is in a register, and has no address."));
1237
1238 case axs_lvalue_memory:
1239 value->kind = axs_rvalue;
1240 value->type = lookup_pointer_type (value->type);
1241 break;
1242 }
1243 }
1244
1245 /* Generate code to push the value of a bitfield of a structure whose
1246 address is on the top of the stack. START and END give the
1247 starting and one-past-ending *bit* numbers of the field within the
1248 structure. */
1249 static void
1250 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1251 struct axs_value *value, struct type *type,
1252 int start, int end)
1253 {
1254 /* Note that ops[i] fetches 8 << i bits. */
1255 static enum agent_op ops[]
1256 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1257 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1258
1259 /* We don't want to touch any byte that the bitfield doesn't
1260 actually occupy; we shouldn't make any accesses we're not
1261 explicitly permitted to. We rely here on the fact that the
1262 bytecode `ref' operators work on unaligned addresses.
1263
1264 It takes some fancy footwork to get the stack to work the way
1265 we'd like. Say we're retrieving a bitfield that requires three
1266 fetches. Initially, the stack just contains the address:
1267 addr
1268 For the first fetch, we duplicate the address
1269 addr addr
1270 then add the byte offset, do the fetch, and shift and mask as
1271 needed, yielding a fragment of the value, properly aligned for
1272 the final bitwise or:
1273 addr frag1
1274 then we swap, and repeat the process:
1275 frag1 addr --- address on top
1276 frag1 addr addr --- duplicate it
1277 frag1 addr frag2 --- get second fragment
1278 frag1 frag2 addr --- swap again
1279 frag1 frag2 frag3 --- get third fragment
1280 Notice that, since the third fragment is the last one, we don't
1281 bother duplicating the address this time. Now we have all the
1282 fragments on the stack, and we can simply `or' them together,
1283 yielding the final value of the bitfield. */
1284
1285 /* The first and one-after-last bits in the field, but rounded down
1286 and up to byte boundaries. */
1287 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1288 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1289 / TARGET_CHAR_BIT)
1290 * TARGET_CHAR_BIT);
1291
1292 /* current bit offset within the structure */
1293 int offset;
1294
1295 /* The index in ops of the opcode we're considering. */
1296 int op;
1297
1298 /* The number of fragments we generated in the process. Probably
1299 equal to the number of `one' bits in bytesize, but who cares? */
1300 int fragment_count;
1301
1302 /* Dereference any typedefs. */
1303 type = check_typedef (type);
1304
1305 /* Can we fetch the number of bits requested at all? */
1306 if ((end - start) > ((1 << num_ops) * 8))
1307 internal_error (__FILE__, __LINE__,
1308 _("gen_bitfield_ref: bitfield too wide"));
1309
1310 /* Note that we know here that we only need to try each opcode once.
1311 That may not be true on machines with weird byte sizes. */
1312 offset = bound_start;
1313 fragment_count = 0;
1314 for (op = num_ops - 1; op >= 0; op--)
1315 {
1316 /* number of bits that ops[op] would fetch */
1317 int op_size = 8 << op;
1318
1319 /* The stack at this point, from bottom to top, contains zero or
1320 more fragments, then the address. */
1321
1322 /* Does this fetch fit within the bitfield? */
1323 if (offset + op_size <= bound_end)
1324 {
1325 /* Is this the last fragment? */
1326 int last_frag = (offset + op_size == bound_end);
1327
1328 if (!last_frag)
1329 ax_simple (ax, aop_dup); /* keep a copy of the address */
1330
1331 /* Add the offset. */
1332 gen_offset (ax, offset / TARGET_CHAR_BIT);
1333
1334 if (ax->tracing)
1335 {
1336 /* Record the area of memory we're about to fetch. */
1337 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1338 }
1339
1340 /* Perform the fetch. */
1341 ax_simple (ax, ops[op]);
1342
1343 /* Shift the bits we have to their proper position.
1344 gen_left_shift will generate right shifts when the operand
1345 is negative.
1346
1347 A big-endian field diagram to ponder:
1348 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1349 +------++------++------++------++------++------++------++------+
1350 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1351 ^ ^ ^ ^
1352 bit number 16 32 48 53
1353 These are bit numbers as supplied by GDB. Note that the
1354 bit numbers run from right to left once you've fetched the
1355 value!
1356
1357 A little-endian field diagram to ponder:
1358 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1359 +------++------++------++------++------++------++------++------+
1360 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1361 ^ ^ ^ ^ ^
1362 bit number 48 32 16 4 0
1363
1364 In both cases, the most significant end is on the left
1365 (i.e. normal numeric writing order), which means that you
1366 don't go crazy thinking about `left' and `right' shifts.
1367
1368 We don't have to worry about masking yet:
1369 - If they contain garbage off the least significant end, then we
1370 must be looking at the low end of the field, and the right
1371 shift will wipe them out.
1372 - If they contain garbage off the most significant end, then we
1373 must be looking at the most significant end of the word, and
1374 the sign/zero extension will wipe them out.
1375 - If we're in the interior of the word, then there is no garbage
1376 on either end, because the ref operators zero-extend. */
1377 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1378 gen_left_shift (ax, end - (offset + op_size));
1379 else
1380 gen_left_shift (ax, offset - start);
1381
1382 if (!last_frag)
1383 /* Bring the copy of the address up to the top. */
1384 ax_simple (ax, aop_swap);
1385
1386 offset += op_size;
1387 fragment_count++;
1388 }
1389 }
1390
1391 /* Generate enough bitwise `or' operations to combine all the
1392 fragments we left on the stack. */
1393 while (fragment_count-- > 1)
1394 ax_simple (ax, aop_bit_or);
1395
1396 /* Sign- or zero-extend the value as appropriate. */
1397 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1398
1399 /* This is *not* an lvalue. Ugh. */
1400 value->kind = axs_rvalue;
1401 value->type = type;
1402 }
1403
1404 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1405 is an accumulated offset (in bytes), will be nonzero for objects
1406 embedded in other objects, like C++ base classes. Behavior should
1407 generally follow value_primitive_field. */
1408
1409 static void
1410 gen_primitive_field (struct expression *exp,
1411 struct agent_expr *ax, struct axs_value *value,
1412 int offset, int fieldno, struct type *type)
1413 {
1414 /* Is this a bitfield? */
1415 if (TYPE_FIELD_PACKED (type, fieldno))
1416 gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1417 (offset * TARGET_CHAR_BIT
1418 + TYPE_FIELD_BITPOS (type, fieldno)),
1419 (offset * TARGET_CHAR_BIT
1420 + TYPE_FIELD_BITPOS (type, fieldno)
1421 + TYPE_FIELD_BITSIZE (type, fieldno)));
1422 else
1423 {
1424 gen_offset (ax, offset
1425 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1426 value->kind = axs_lvalue_memory;
1427 value->type = TYPE_FIELD_TYPE (type, fieldno);
1428 }
1429 }
1430
1431 /* Search for the given field in either the given type or one of its
1432 base classes. Return 1 if found, 0 if not. */
1433
1434 static int
1435 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1436 struct axs_value *value,
1437 char *field, int offset, struct type *type)
1438 {
1439 int i, rslt;
1440 int nbases = TYPE_N_BASECLASSES (type);
1441
1442 CHECK_TYPEDEF (type);
1443
1444 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1445 {
1446 const char *this_name = TYPE_FIELD_NAME (type, i);
1447
1448 if (this_name)
1449 {
1450 if (strcmp (field, this_name) == 0)
1451 {
1452 /* Note that bytecodes for the struct's base (aka
1453 "this") will have been generated already, which will
1454 be unnecessary but not harmful if the static field is
1455 being handled as a global. */
1456 if (field_is_static (&TYPE_FIELD (type, i)))
1457 {
1458 gen_static_field (exp->gdbarch, ax, value, type, i);
1459 if (value->optimized_out)
1460 error (_("static field `%s' has been "
1461 "optimized out, cannot use"),
1462 field);
1463 return 1;
1464 }
1465
1466 gen_primitive_field (exp, ax, value, offset, i, type);
1467 return 1;
1468 }
1469 #if 0 /* is this right? */
1470 if (this_name[0] == '\0')
1471 internal_error (__FILE__, __LINE__,
1472 _("find_field: anonymous unions not supported"));
1473 #endif
1474 }
1475 }
1476
1477 /* Now scan through base classes recursively. */
1478 for (i = 0; i < nbases; i++)
1479 {
1480 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1481
1482 rslt = gen_struct_ref_recursive (exp, ax, value, field,
1483 offset + TYPE_BASECLASS_BITPOS (type, i)
1484 / TARGET_CHAR_BIT,
1485 basetype);
1486 if (rslt)
1487 return 1;
1488 }
1489
1490 /* Not found anywhere, flag so caller can complain. */
1491 return 0;
1492 }
1493
1494 /* Generate code to reference the member named FIELD of a structure or
1495 union. The top of the stack, as described by VALUE, should have
1496 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1497 the operator being compiled, and OPERAND_NAME is the kind of thing
1498 it operates on; we use them in error messages. */
1499 static void
1500 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1501 struct axs_value *value, char *field,
1502 char *operator_name, char *operand_name)
1503 {
1504 struct type *type;
1505 int found;
1506
1507 /* Follow pointers until we reach a non-pointer. These aren't the C
1508 semantics, but they're what the normal GDB evaluator does, so we
1509 should at least be consistent. */
1510 while (pointer_type (value->type))
1511 {
1512 require_rvalue (ax, value);
1513 gen_deref (ax, value);
1514 }
1515 type = check_typedef (value->type);
1516
1517 /* This must yield a structure or a union. */
1518 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1519 && TYPE_CODE (type) != TYPE_CODE_UNION)
1520 error (_("The left operand of `%s' is not a %s."),
1521 operator_name, operand_name);
1522
1523 /* And it must be in memory; we don't deal with structure rvalues,
1524 or structures living in registers. */
1525 if (value->kind != axs_lvalue_memory)
1526 error (_("Structure does not live in memory."));
1527
1528 /* Search through fields and base classes recursively. */
1529 found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1530
1531 if (!found)
1532 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1533 field, TYPE_TAG_NAME (type));
1534 }
1535
1536 static int
1537 gen_namespace_elt (struct expression *exp,
1538 struct agent_expr *ax, struct axs_value *value,
1539 const struct type *curtype, char *name);
1540 static int
1541 gen_maybe_namespace_elt (struct expression *exp,
1542 struct agent_expr *ax, struct axs_value *value,
1543 const struct type *curtype, char *name);
1544
1545 static void
1546 gen_static_field (struct gdbarch *gdbarch,
1547 struct agent_expr *ax, struct axs_value *value,
1548 struct type *type, int fieldno)
1549 {
1550 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1551 {
1552 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1553 value->kind = axs_lvalue_memory;
1554 value->type = TYPE_FIELD_TYPE (type, fieldno);
1555 value->optimized_out = 0;
1556 }
1557 else
1558 {
1559 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1560 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
1561
1562 if (sym)
1563 {
1564 gen_var_ref (gdbarch, ax, value, sym);
1565
1566 /* Don't error if the value was optimized out, we may be
1567 scanning all static fields and just want to pass over this
1568 and continue with the rest. */
1569 }
1570 else
1571 {
1572 /* Silently assume this was optimized out; class printing
1573 will let the user know why the data is missing. */
1574 value->optimized_out = 1;
1575 }
1576 }
1577 }
1578
1579 static int
1580 gen_struct_elt_for_reference (struct expression *exp,
1581 struct agent_expr *ax, struct axs_value *value,
1582 struct type *type, char *fieldname)
1583 {
1584 struct type *t = type;
1585 int i;
1586
1587 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1588 && TYPE_CODE (t) != TYPE_CODE_UNION)
1589 internal_error (__FILE__, __LINE__,
1590 _("non-aggregate type to gen_struct_elt_for_reference"));
1591
1592 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1593 {
1594 const char *t_field_name = TYPE_FIELD_NAME (t, i);
1595
1596 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1597 {
1598 if (field_is_static (&TYPE_FIELD (t, i)))
1599 {
1600 gen_static_field (exp->gdbarch, ax, value, t, i);
1601 if (value->optimized_out)
1602 error (_("static field `%s' has been "
1603 "optimized out, cannot use"),
1604 fieldname);
1605 return 1;
1606 }
1607 if (TYPE_FIELD_PACKED (t, i))
1608 error (_("pointers to bitfield members not allowed"));
1609
1610 /* FIXME we need a way to do "want_address" equivalent */
1611
1612 error (_("Cannot reference non-static field \"%s\""), fieldname);
1613 }
1614 }
1615
1616 /* FIXME add other scoped-reference cases here */
1617
1618 /* Do a last-ditch lookup. */
1619 return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1620 }
1621
1622 /* C++: Return the member NAME of the namespace given by the type
1623 CURTYPE. */
1624
1625 static int
1626 gen_namespace_elt (struct expression *exp,
1627 struct agent_expr *ax, struct axs_value *value,
1628 const struct type *curtype, char *name)
1629 {
1630 int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1631
1632 if (!found)
1633 error (_("No symbol \"%s\" in namespace \"%s\"."),
1634 name, TYPE_TAG_NAME (curtype));
1635
1636 return found;
1637 }
1638
1639 /* A helper function used by value_namespace_elt and
1640 value_struct_elt_for_reference. It looks up NAME inside the
1641 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1642 is a class and NAME refers to a type in CURTYPE itself (as opposed
1643 to, say, some base class of CURTYPE). */
1644
1645 static int
1646 gen_maybe_namespace_elt (struct expression *exp,
1647 struct agent_expr *ax, struct axs_value *value,
1648 const struct type *curtype, char *name)
1649 {
1650 const char *namespace_name = TYPE_TAG_NAME (curtype);
1651 struct symbol *sym;
1652
1653 sym = cp_lookup_symbol_namespace (namespace_name, name,
1654 block_for_pc (ax->scope),
1655 VAR_DOMAIN);
1656
1657 if (sym == NULL)
1658 return 0;
1659
1660 gen_var_ref (exp->gdbarch, ax, value, sym);
1661
1662 if (value->optimized_out)
1663 error (_("`%s' has been optimized out, cannot use"),
1664 SYMBOL_PRINT_NAME (sym));
1665
1666 return 1;
1667 }
1668
1669
1670 static int
1671 gen_aggregate_elt_ref (struct expression *exp,
1672 struct agent_expr *ax, struct axs_value *value,
1673 struct type *type, char *field,
1674 char *operator_name, char *operand_name)
1675 {
1676 switch (TYPE_CODE (type))
1677 {
1678 case TYPE_CODE_STRUCT:
1679 case TYPE_CODE_UNION:
1680 return gen_struct_elt_for_reference (exp, ax, value, type, field);
1681 break;
1682 case TYPE_CODE_NAMESPACE:
1683 return gen_namespace_elt (exp, ax, value, type, field);
1684 break;
1685 default:
1686 internal_error (__FILE__, __LINE__,
1687 _("non-aggregate type in gen_aggregate_elt_ref"));
1688 }
1689
1690 return 0;
1691 }
1692
1693 /* Generate code for GDB's magical `repeat' operator.
1694 LVALUE @ INT creates an array INT elements long, and whose elements
1695 have the same type as LVALUE, located in memory so that LVALUE is
1696 its first element. For example, argv[0]@argc gives you the array
1697 of command-line arguments.
1698
1699 Unfortunately, because we have to know the types before we actually
1700 have a value for the expression, we can't implement this perfectly
1701 without changing the type system, having values that occupy two
1702 stack slots, doing weird things with sizeof, etc. So we require
1703 the right operand to be a constant expression. */
1704 static void
1705 gen_repeat (struct expression *exp, union exp_element **pc,
1706 struct agent_expr *ax, struct axs_value *value)
1707 {
1708 struct axs_value value1;
1709
1710 /* We don't want to turn this into an rvalue, so no conversions
1711 here. */
1712 gen_expr (exp, pc, ax, &value1);
1713 if (value1.kind != axs_lvalue_memory)
1714 error (_("Left operand of `@' must be an object in memory."));
1715
1716 /* Evaluate the length; it had better be a constant. */
1717 {
1718 struct value *v = const_expr (pc);
1719 int length;
1720
1721 if (!v)
1722 error (_("Right operand of `@' must be a "
1723 "constant, in agent expressions."));
1724 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1725 error (_("Right operand of `@' must be an integer."));
1726 length = value_as_long (v);
1727 if (length <= 0)
1728 error (_("Right operand of `@' must be positive."));
1729
1730 /* The top of the stack is already the address of the object, so
1731 all we need to do is frob the type of the lvalue. */
1732 {
1733 /* FIXME-type-allocation: need a way to free this type when we are
1734 done with it. */
1735 struct type *array
1736 = lookup_array_range_type (value1.type, 0, length - 1);
1737
1738 value->kind = axs_lvalue_memory;
1739 value->type = array;
1740 }
1741 }
1742 }
1743
1744
1745 /* Emit code for the `sizeof' operator.
1746 *PC should point at the start of the operand expression; we advance it
1747 to the first instruction after the operand. */
1748 static void
1749 gen_sizeof (struct expression *exp, union exp_element **pc,
1750 struct agent_expr *ax, struct axs_value *value,
1751 struct type *size_type)
1752 {
1753 /* We don't care about the value of the operand expression; we only
1754 care about its type. However, in the current arrangement, the
1755 only way to find an expression's type is to generate code for it.
1756 So we generate code for the operand, and then throw it away,
1757 replacing it with code that simply pushes its size. */
1758 int start = ax->len;
1759
1760 gen_expr (exp, pc, ax, value);
1761
1762 /* Throw away the code we just generated. */
1763 ax->len = start;
1764
1765 ax_const_l (ax, TYPE_LENGTH (value->type));
1766 value->kind = axs_rvalue;
1767 value->type = size_type;
1768 }
1769 \f
1770
1771 /* Generating bytecode from GDB expressions: general recursive thingy */
1772
1773 /* XXX: i18n */
1774 /* A gen_expr function written by a Gen-X'er guy.
1775 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1776 void
1777 gen_expr (struct expression *exp, union exp_element **pc,
1778 struct agent_expr *ax, struct axs_value *value)
1779 {
1780 /* Used to hold the descriptions of operand expressions. */
1781 struct axs_value value1, value2, value3;
1782 enum exp_opcode op = (*pc)[0].opcode, op2;
1783 int if1, go1, if2, go2, end;
1784 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
1785
1786 /* If we're looking at a constant expression, just push its value. */
1787 {
1788 struct value *v = maybe_const_expr (pc);
1789
1790 if (v)
1791 {
1792 ax_const_l (ax, value_as_long (v));
1793 value->kind = axs_rvalue;
1794 value->type = check_typedef (value_type (v));
1795 return;
1796 }
1797 }
1798
1799 /* Otherwise, go ahead and generate code for it. */
1800 switch (op)
1801 {
1802 /* Binary arithmetic operators. */
1803 case BINOP_ADD:
1804 case BINOP_SUB:
1805 case BINOP_MUL:
1806 case BINOP_DIV:
1807 case BINOP_REM:
1808 case BINOP_LSH:
1809 case BINOP_RSH:
1810 case BINOP_SUBSCRIPT:
1811 case BINOP_BITWISE_AND:
1812 case BINOP_BITWISE_IOR:
1813 case BINOP_BITWISE_XOR:
1814 case BINOP_EQUAL:
1815 case BINOP_NOTEQUAL:
1816 case BINOP_LESS:
1817 case BINOP_GTR:
1818 case BINOP_LEQ:
1819 case BINOP_GEQ:
1820 (*pc)++;
1821 gen_expr (exp, pc, ax, &value1);
1822 gen_usual_unary (exp, ax, &value1);
1823 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1824 break;
1825
1826 case BINOP_LOGICAL_AND:
1827 (*pc)++;
1828 /* Generate the obvious sequence of tests and jumps. */
1829 gen_expr (exp, pc, ax, &value1);
1830 gen_usual_unary (exp, ax, &value1);
1831 if1 = ax_goto (ax, aop_if_goto);
1832 go1 = ax_goto (ax, aop_goto);
1833 ax_label (ax, if1, ax->len);
1834 gen_expr (exp, pc, ax, &value2);
1835 gen_usual_unary (exp, ax, &value2);
1836 if2 = ax_goto (ax, aop_if_goto);
1837 go2 = ax_goto (ax, aop_goto);
1838 ax_label (ax, if2, ax->len);
1839 ax_const_l (ax, 1);
1840 end = ax_goto (ax, aop_goto);
1841 ax_label (ax, go1, ax->len);
1842 ax_label (ax, go2, ax->len);
1843 ax_const_l (ax, 0);
1844 ax_label (ax, end, ax->len);
1845 value->kind = axs_rvalue;
1846 value->type = int_type;
1847 break;
1848
1849 case BINOP_LOGICAL_OR:
1850 (*pc)++;
1851 /* Generate the obvious sequence of tests and jumps. */
1852 gen_expr (exp, pc, ax, &value1);
1853 gen_usual_unary (exp, ax, &value1);
1854 if1 = ax_goto (ax, aop_if_goto);
1855 gen_expr (exp, pc, ax, &value2);
1856 gen_usual_unary (exp, ax, &value2);
1857 if2 = ax_goto (ax, aop_if_goto);
1858 ax_const_l (ax, 0);
1859 end = ax_goto (ax, aop_goto);
1860 ax_label (ax, if1, ax->len);
1861 ax_label (ax, if2, ax->len);
1862 ax_const_l (ax, 1);
1863 ax_label (ax, end, ax->len);
1864 value->kind = axs_rvalue;
1865 value->type = int_type;
1866 break;
1867
1868 case TERNOP_COND:
1869 (*pc)++;
1870 gen_expr (exp, pc, ax, &value1);
1871 gen_usual_unary (exp, ax, &value1);
1872 /* For (A ? B : C), it's easiest to generate subexpression
1873 bytecodes in order, but if_goto jumps on true, so we invert
1874 the sense of A. Then we can do B by dropping through, and
1875 jump to do C. */
1876 gen_logical_not (ax, &value1, int_type);
1877 if1 = ax_goto (ax, aop_if_goto);
1878 gen_expr (exp, pc, ax, &value2);
1879 gen_usual_unary (exp, ax, &value2);
1880 end = ax_goto (ax, aop_goto);
1881 ax_label (ax, if1, ax->len);
1882 gen_expr (exp, pc, ax, &value3);
1883 gen_usual_unary (exp, ax, &value3);
1884 ax_label (ax, end, ax->len);
1885 /* This is arbitary - what if B and C are incompatible types? */
1886 value->type = value2.type;
1887 value->kind = value2.kind;
1888 break;
1889
1890 case BINOP_ASSIGN:
1891 (*pc)++;
1892 if ((*pc)[0].opcode == OP_INTERNALVAR)
1893 {
1894 char *name = internalvar_name ((*pc)[1].internalvar);
1895 struct trace_state_variable *tsv;
1896
1897 (*pc) += 3;
1898 gen_expr (exp, pc, ax, value);
1899 tsv = find_trace_state_variable (name);
1900 if (tsv)
1901 {
1902 ax_tsv (ax, aop_setv, tsv->number);
1903 if (ax->tracing)
1904 ax_tsv (ax, aop_tracev, tsv->number);
1905 }
1906 else
1907 error (_("$%s is not a trace state variable, "
1908 "may not assign to it"), name);
1909 }
1910 else
1911 error (_("May only assign to trace state variables"));
1912 break;
1913
1914 case BINOP_ASSIGN_MODIFY:
1915 (*pc)++;
1916 op2 = (*pc)[0].opcode;
1917 (*pc)++;
1918 (*pc)++;
1919 if ((*pc)[0].opcode == OP_INTERNALVAR)
1920 {
1921 char *name = internalvar_name ((*pc)[1].internalvar);
1922 struct trace_state_variable *tsv;
1923
1924 (*pc) += 3;
1925 tsv = find_trace_state_variable (name);
1926 if (tsv)
1927 {
1928 /* The tsv will be the left half of the binary operation. */
1929 ax_tsv (ax, aop_getv, tsv->number);
1930 if (ax->tracing)
1931 ax_tsv (ax, aop_tracev, tsv->number);
1932 /* Trace state variables are always 64-bit integers. */
1933 value1.kind = axs_rvalue;
1934 value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1935 /* Now do right half of expression. */
1936 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1937 /* We have a result of the binary op, set the tsv. */
1938 ax_tsv (ax, aop_setv, tsv->number);
1939 if (ax->tracing)
1940 ax_tsv (ax, aop_tracev, tsv->number);
1941 }
1942 else
1943 error (_("$%s is not a trace state variable, "
1944 "may not assign to it"), name);
1945 }
1946 else
1947 error (_("May only assign to trace state variables"));
1948 break;
1949
1950 /* Note that we need to be a little subtle about generating code
1951 for comma. In C, we can do some optimizations here because
1952 we know the left operand is only being evaluated for effect.
1953 However, if the tracing kludge is in effect, then we always
1954 need to evaluate the left hand side fully, so that all the
1955 variables it mentions get traced. */
1956 case BINOP_COMMA:
1957 (*pc)++;
1958 gen_expr (exp, pc, ax, &value1);
1959 /* Don't just dispose of the left operand. We might be tracing,
1960 in which case we want to emit code to trace it if it's an
1961 lvalue. */
1962 gen_traced_pop (exp->gdbarch, ax, &value1);
1963 gen_expr (exp, pc, ax, value);
1964 /* It's the consumer's responsibility to trace the right operand. */
1965 break;
1966
1967 case OP_LONG: /* some integer constant */
1968 {
1969 struct type *type = (*pc)[1].type;
1970 LONGEST k = (*pc)[2].longconst;
1971
1972 (*pc) += 4;
1973 gen_int_literal (ax, value, k, type);
1974 }
1975 break;
1976
1977 case OP_VAR_VALUE:
1978 gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
1979
1980 if (value->optimized_out)
1981 error (_("`%s' has been optimized out, cannot use"),
1982 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
1983
1984 (*pc) += 4;
1985 break;
1986
1987 case OP_REGISTER:
1988 {
1989 const char *name = &(*pc)[2].string;
1990 int reg;
1991
1992 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1993 reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
1994 if (reg == -1)
1995 internal_error (__FILE__, __LINE__,
1996 _("Register $%s not available"), name);
1997 /* No support for tracing user registers yet. */
1998 if (reg >= gdbarch_num_regs (exp->gdbarch)
1999 + gdbarch_num_pseudo_regs (exp->gdbarch))
2000 error (_("'%s' is a user-register; "
2001 "GDB cannot yet trace user-register contents."),
2002 name);
2003 value->kind = axs_lvalue_register;
2004 value->u.reg = reg;
2005 value->type = register_type (exp->gdbarch, reg);
2006 }
2007 break;
2008
2009 case OP_INTERNALVAR:
2010 {
2011 struct internalvar *var = (*pc)[1].internalvar;
2012 const char *name = internalvar_name (var);
2013 struct trace_state_variable *tsv;
2014
2015 (*pc) += 3;
2016 tsv = find_trace_state_variable (name);
2017 if (tsv)
2018 {
2019 ax_tsv (ax, aop_getv, tsv->number);
2020 if (ax->tracing)
2021 ax_tsv (ax, aop_tracev, tsv->number);
2022 /* Trace state variables are always 64-bit integers. */
2023 value->kind = axs_rvalue;
2024 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2025 }
2026 else if (! compile_internalvar_to_ax (var, ax, value))
2027 error (_("$%s is not a trace state variable; GDB agent "
2028 "expressions cannot use convenience variables."), name);
2029 }
2030 break;
2031
2032 /* Weirdo operator: see comments for gen_repeat for details. */
2033 case BINOP_REPEAT:
2034 /* Note that gen_repeat handles its own argument evaluation. */
2035 (*pc)++;
2036 gen_repeat (exp, pc, ax, value);
2037 break;
2038
2039 case UNOP_CAST:
2040 {
2041 struct type *type = (*pc)[1].type;
2042
2043 (*pc) += 3;
2044 gen_expr (exp, pc, ax, value);
2045 gen_cast (ax, value, type);
2046 }
2047 break;
2048
2049 case UNOP_CAST_TYPE:
2050 {
2051 int offset;
2052 struct value *val;
2053 struct type *type;
2054
2055 ++*pc;
2056 offset = *pc - exp->elts;
2057 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2058 type = value_type (val);
2059 *pc = &exp->elts[offset];
2060
2061 gen_expr (exp, pc, ax, value);
2062 gen_cast (ax, value, type);
2063 }
2064 break;
2065
2066 case UNOP_MEMVAL:
2067 {
2068 struct type *type = check_typedef ((*pc)[1].type);
2069
2070 (*pc) += 3;
2071 gen_expr (exp, pc, ax, value);
2072
2073 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2074 already have the right value on the stack. For
2075 axs_lvalue_register, we must convert. */
2076 if (value->kind == axs_lvalue_register)
2077 require_rvalue (ax, value);
2078
2079 value->type = type;
2080 value->kind = axs_lvalue_memory;
2081 }
2082 break;
2083
2084 case UNOP_MEMVAL_TYPE:
2085 {
2086 int offset;
2087 struct value *val;
2088 struct type *type;
2089
2090 ++*pc;
2091 offset = *pc - exp->elts;
2092 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2093 type = value_type (val);
2094 *pc = &exp->elts[offset];
2095
2096 gen_expr (exp, pc, ax, value);
2097
2098 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2099 already have the right value on the stack. For
2100 axs_lvalue_register, we must convert. */
2101 if (value->kind == axs_lvalue_register)
2102 require_rvalue (ax, value);
2103
2104 value->type = type;
2105 value->kind = axs_lvalue_memory;
2106 }
2107 break;
2108
2109 case UNOP_PLUS:
2110 (*pc)++;
2111 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2112 gen_expr (exp, pc, ax, value);
2113 gen_usual_unary (exp, ax, value);
2114 break;
2115
2116 case UNOP_NEG:
2117 (*pc)++;
2118 /* -FOO is equivalent to 0 - FOO. */
2119 gen_int_literal (ax, &value1, 0,
2120 builtin_type (exp->gdbarch)->builtin_int);
2121 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2122 gen_expr (exp, pc, ax, &value2);
2123 gen_usual_unary (exp, ax, &value2);
2124 gen_usual_arithmetic (exp, ax, &value1, &value2);
2125 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2126 break;
2127
2128 case UNOP_LOGICAL_NOT:
2129 (*pc)++;
2130 gen_expr (exp, pc, ax, value);
2131 gen_usual_unary (exp, ax, value);
2132 gen_logical_not (ax, value, int_type);
2133 break;
2134
2135 case UNOP_COMPLEMENT:
2136 (*pc)++;
2137 gen_expr (exp, pc, ax, value);
2138 gen_usual_unary (exp, ax, value);
2139 gen_integral_promotions (exp, ax, value);
2140 gen_complement (ax, value);
2141 break;
2142
2143 case UNOP_IND:
2144 (*pc)++;
2145 gen_expr (exp, pc, ax, value);
2146 gen_usual_unary (exp, ax, value);
2147 if (!pointer_type (value->type))
2148 error (_("Argument of unary `*' is not a pointer."));
2149 gen_deref (ax, value);
2150 break;
2151
2152 case UNOP_ADDR:
2153 (*pc)++;
2154 gen_expr (exp, pc, ax, value);
2155 gen_address_of (ax, value);
2156 break;
2157
2158 case UNOP_SIZEOF:
2159 (*pc)++;
2160 /* Notice that gen_sizeof handles its own operand, unlike most
2161 of the other unary operator functions. This is because we
2162 have to throw away the code we generate. */
2163 gen_sizeof (exp, pc, ax, value,
2164 builtin_type (exp->gdbarch)->builtin_int);
2165 break;
2166
2167 case STRUCTOP_STRUCT:
2168 case STRUCTOP_PTR:
2169 {
2170 int length = (*pc)[1].longconst;
2171 char *name = &(*pc)[2].string;
2172
2173 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2174 gen_expr (exp, pc, ax, value);
2175 if (op == STRUCTOP_STRUCT)
2176 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2177 else if (op == STRUCTOP_PTR)
2178 gen_struct_ref (exp, ax, value, name, "->",
2179 "pointer to a structure or union");
2180 else
2181 /* If this `if' chain doesn't handle it, then the case list
2182 shouldn't mention it, and we shouldn't be here. */
2183 internal_error (__FILE__, __LINE__,
2184 _("gen_expr: unhandled struct case"));
2185 }
2186 break;
2187
2188 case OP_THIS:
2189 {
2190 struct symbol *sym, *func;
2191 struct block *b;
2192 const struct language_defn *lang;
2193
2194 b = block_for_pc (ax->scope);
2195 func = block_linkage_function (b);
2196 lang = language_def (SYMBOL_LANGUAGE (func));
2197
2198 sym = lookup_language_this (lang, b);
2199 if (!sym)
2200 error (_("no `%s' found"), lang->la_name_of_this);
2201
2202 gen_var_ref (exp->gdbarch, ax, value, sym);
2203
2204 if (value->optimized_out)
2205 error (_("`%s' has been optimized out, cannot use"),
2206 SYMBOL_PRINT_NAME (sym));
2207
2208 (*pc) += 2;
2209 }
2210 break;
2211
2212 case OP_SCOPE:
2213 {
2214 struct type *type = (*pc)[1].type;
2215 int length = longest_to_int ((*pc)[2].longconst);
2216 char *name = &(*pc)[3].string;
2217 int found;
2218
2219 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2220 "?", "??");
2221 if (!found)
2222 error (_("There is no field named %s"), name);
2223 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2224 }
2225 break;
2226
2227 case OP_TYPE:
2228 case OP_TYPEOF:
2229 case OP_DECLTYPE:
2230 error (_("Attempt to use a type name as an expression."));
2231
2232 default:
2233 error (_("Unsupported operator %s (%d) in expression."),
2234 op_name (exp, op), op);
2235 }
2236 }
2237
2238 /* This handles the middle-to-right-side of code generation for binary
2239 expressions, which is shared between regular binary operations and
2240 assign-modify (+= and friends) expressions. */
2241
2242 static void
2243 gen_expr_binop_rest (struct expression *exp,
2244 enum exp_opcode op, union exp_element **pc,
2245 struct agent_expr *ax, struct axs_value *value,
2246 struct axs_value *value1, struct axs_value *value2)
2247 {
2248 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2249
2250 gen_expr (exp, pc, ax, value2);
2251 gen_usual_unary (exp, ax, value2);
2252 gen_usual_arithmetic (exp, ax, value1, value2);
2253 switch (op)
2254 {
2255 case BINOP_ADD:
2256 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2257 && pointer_type (value2->type))
2258 {
2259 /* Swap the values and proceed normally. */
2260 ax_simple (ax, aop_swap);
2261 gen_ptradd (ax, value, value2, value1);
2262 }
2263 else if (pointer_type (value1->type)
2264 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2265 gen_ptradd (ax, value, value1, value2);
2266 else
2267 gen_binop (ax, value, value1, value2,
2268 aop_add, aop_add, 1, "addition");
2269 break;
2270 case BINOP_SUB:
2271 if (pointer_type (value1->type)
2272 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2273 gen_ptrsub (ax,value, value1, value2);
2274 else if (pointer_type (value1->type)
2275 && pointer_type (value2->type))
2276 /* FIXME --- result type should be ptrdiff_t */
2277 gen_ptrdiff (ax, value, value1, value2,
2278 builtin_type (exp->gdbarch)->builtin_long);
2279 else
2280 gen_binop (ax, value, value1, value2,
2281 aop_sub, aop_sub, 1, "subtraction");
2282 break;
2283 case BINOP_MUL:
2284 gen_binop (ax, value, value1, value2,
2285 aop_mul, aop_mul, 1, "multiplication");
2286 break;
2287 case BINOP_DIV:
2288 gen_binop (ax, value, value1, value2,
2289 aop_div_signed, aop_div_unsigned, 1, "division");
2290 break;
2291 case BINOP_REM:
2292 gen_binop (ax, value, value1, value2,
2293 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2294 break;
2295 case BINOP_LSH:
2296 gen_binop (ax, value, value1, value2,
2297 aop_lsh, aop_lsh, 1, "left shift");
2298 break;
2299 case BINOP_RSH:
2300 gen_binop (ax, value, value1, value2,
2301 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2302 break;
2303 case BINOP_SUBSCRIPT:
2304 {
2305 struct type *type;
2306
2307 if (binop_types_user_defined_p (op, value1->type, value2->type))
2308 {
2309 error (_("cannot subscript requested type: "
2310 "cannot call user defined functions"));
2311 }
2312 else
2313 {
2314 /* If the user attempts to subscript something that is not
2315 an array or pointer type (like a plain int variable for
2316 example), then report this as an error. */
2317 type = check_typedef (value1->type);
2318 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2319 && TYPE_CODE (type) != TYPE_CODE_PTR)
2320 {
2321 if (TYPE_NAME (type))
2322 error (_("cannot subscript something of type `%s'"),
2323 TYPE_NAME (type));
2324 else
2325 error (_("cannot subscript requested type"));
2326 }
2327 }
2328
2329 if (!is_integral_type (value2->type))
2330 error (_("Argument to arithmetic operation "
2331 "not a number or boolean."));
2332
2333 gen_ptradd (ax, value, value1, value2);
2334 gen_deref (ax, value);
2335 break;
2336 }
2337 case BINOP_BITWISE_AND:
2338 gen_binop (ax, value, value1, value2,
2339 aop_bit_and, aop_bit_and, 0, "bitwise and");
2340 break;
2341
2342 case BINOP_BITWISE_IOR:
2343 gen_binop (ax, value, value1, value2,
2344 aop_bit_or, aop_bit_or, 0, "bitwise or");
2345 break;
2346
2347 case BINOP_BITWISE_XOR:
2348 gen_binop (ax, value, value1, value2,
2349 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2350 break;
2351
2352 case BINOP_EQUAL:
2353 gen_equal (ax, value, value1, value2, int_type);
2354 break;
2355
2356 case BINOP_NOTEQUAL:
2357 gen_equal (ax, value, value1, value2, int_type);
2358 gen_logical_not (ax, value, int_type);
2359 break;
2360
2361 case BINOP_LESS:
2362 gen_less (ax, value, value1, value2, int_type);
2363 break;
2364
2365 case BINOP_GTR:
2366 ax_simple (ax, aop_swap);
2367 gen_less (ax, value, value1, value2, int_type);
2368 break;
2369
2370 case BINOP_LEQ:
2371 ax_simple (ax, aop_swap);
2372 gen_less (ax, value, value1, value2, int_type);
2373 gen_logical_not (ax, value, int_type);
2374 break;
2375
2376 case BINOP_GEQ:
2377 gen_less (ax, value, value1, value2, int_type);
2378 gen_logical_not (ax, value, int_type);
2379 break;
2380
2381 default:
2382 /* We should only list operators in the outer case statement
2383 that we actually handle in the inner case statement. */
2384 internal_error (__FILE__, __LINE__,
2385 _("gen_expr: op case sets don't match"));
2386 }
2387 }
2388 \f
2389
2390 /* Given a single variable and a scope, generate bytecodes to trace
2391 its value. This is for use in situations where we have only a
2392 variable's name, and no parsed expression; for instance, when the
2393 name comes from a list of local variables of a function. */
2394
2395 struct agent_expr *
2396 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2397 struct symbol *var, int trace_string)
2398 {
2399 struct cleanup *old_chain = 0;
2400 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2401 struct axs_value value;
2402
2403 old_chain = make_cleanup_free_agent_expr (ax);
2404
2405 ax->tracing = 1;
2406 ax->trace_string = trace_string;
2407 gen_var_ref (gdbarch, ax, &value, var);
2408
2409 /* If there is no actual variable to trace, flag it by returning
2410 an empty agent expression. */
2411 if (value.optimized_out)
2412 {
2413 do_cleanups (old_chain);
2414 return NULL;
2415 }
2416
2417 /* Make sure we record the final object, and get rid of it. */
2418 gen_traced_pop (gdbarch, ax, &value);
2419
2420 /* Oh, and terminate. */
2421 ax_simple (ax, aop_end);
2422
2423 /* We have successfully built the agent expr, so cancel the cleanup
2424 request. If we add more cleanups that we always want done, this
2425 will have to get more complicated. */
2426 discard_cleanups (old_chain);
2427 return ax;
2428 }
2429
2430 /* Generating bytecode from GDB expressions: driver */
2431
2432 /* Given a GDB expression EXPR, return bytecode to trace its value.
2433 The result will use the `trace' and `trace_quick' bytecodes to
2434 record the value of all memory touched by the expression. The
2435 caller can then use the ax_reqs function to discover which
2436 registers it relies upon. */
2437 struct agent_expr *
2438 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
2439 int trace_string)
2440 {
2441 struct cleanup *old_chain = 0;
2442 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2443 union exp_element *pc;
2444 struct axs_value value;
2445
2446 old_chain = make_cleanup_free_agent_expr (ax);
2447
2448 pc = expr->elts;
2449 ax->tracing = 1;
2450 ax->trace_string = trace_string;
2451 value.optimized_out = 0;
2452 gen_expr (expr, &pc, ax, &value);
2453
2454 /* Make sure we record the final object, and get rid of it. */
2455 gen_traced_pop (expr->gdbarch, ax, &value);
2456
2457 /* Oh, and terminate. */
2458 ax_simple (ax, aop_end);
2459
2460 /* We have successfully built the agent expr, so cancel the cleanup
2461 request. If we add more cleanups that we always want done, this
2462 will have to get more complicated. */
2463 discard_cleanups (old_chain);
2464 return ax;
2465 }
2466
2467 /* Given a GDB expression EXPR, return a bytecode sequence that will
2468 evaluate and return a result. The bytecodes will do a direct
2469 evaluation, using the current data on the target, rather than
2470 recording blocks of memory and registers for later use, as
2471 gen_trace_for_expr does. The generated bytecode sequence leaves
2472 the result of expression evaluation on the top of the stack. */
2473
2474 struct agent_expr *
2475 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2476 {
2477 struct cleanup *old_chain = 0;
2478 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2479 union exp_element *pc;
2480 struct axs_value value;
2481
2482 old_chain = make_cleanup_free_agent_expr (ax);
2483
2484 pc = expr->elts;
2485 ax->tracing = 0;
2486 value.optimized_out = 0;
2487 gen_expr (expr, &pc, ax, &value);
2488
2489 require_rvalue (ax, &value);
2490
2491 /* Oh, and terminate. */
2492 ax_simple (ax, aop_end);
2493
2494 /* We have successfully built the agent expr, so cancel the cleanup
2495 request. If we add more cleanups that we always want done, this
2496 will have to get more complicated. */
2497 discard_cleanups (old_chain);
2498 return ax;
2499 }
2500
2501 struct agent_expr *
2502 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
2503 int trace_string)
2504 {
2505 struct cleanup *old_chain = 0;
2506 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2507 struct axs_value value;
2508
2509 old_chain = make_cleanup_free_agent_expr (ax);
2510
2511 ax->tracing = 1;
2512 ax->trace_string = trace_string;
2513
2514 gdbarch_gen_return_address (gdbarch, ax, &value, scope);
2515
2516 /* Make sure we record the final object, and get rid of it. */
2517 gen_traced_pop (gdbarch, ax, &value);
2518
2519 /* Oh, and terminate. */
2520 ax_simple (ax, aop_end);
2521
2522 /* We have successfully built the agent expr, so cancel the cleanup
2523 request. If we add more cleanups that we always want done, this
2524 will have to get more complicated. */
2525 discard_cleanups (old_chain);
2526 return ax;
2527 }
2528
2529 /* Given a collection of printf-style arguments, generate code to
2530 evaluate the arguments and pass everything to a special
2531 bytecode. */
2532
2533 struct agent_expr *
2534 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2535 CORE_ADDR function, LONGEST channel,
2536 const char *format, int fmtlen,
2537 struct format_piece *frags,
2538 int nargs, struct expression **exprs)
2539 {
2540 struct cleanup *old_chain = 0;
2541 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2542 union exp_element *pc;
2543 struct axs_value value;
2544 int tem;
2545
2546 old_chain = make_cleanup_free_agent_expr (ax);
2547
2548 /* We're computing values, not doing side effects. */
2549 ax->tracing = 0;
2550
2551 /* Evaluate and push the args on the stack in reverse order,
2552 for simplicity of collecting them on the target side. */
2553 for (tem = nargs - 1; tem >= 0; --tem)
2554 {
2555 pc = exprs[tem]->elts;
2556 value.optimized_out = 0;
2557 gen_expr (exprs[tem], &pc, ax, &value);
2558 require_rvalue (ax, &value);
2559 }
2560
2561 /* Push function and channel. */
2562 ax_const_l (ax, channel);
2563 ax_const_l (ax, function);
2564
2565 /* Issue the printf bytecode proper. */
2566 ax_simple (ax, aop_printf);
2567 ax_simple (ax, nargs);
2568 ax_string (ax, format, fmtlen);
2569
2570 /* And terminate. */
2571 ax_simple (ax, aop_end);
2572
2573 /* We have successfully built the agent expr, so cancel the cleanup
2574 request. If we add more cleanups that we always want done, this
2575 will have to get more complicated. */
2576 discard_cleanups (old_chain);
2577
2578 return ax;
2579 }
2580
2581 static void
2582 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
2583 {
2584 struct cleanup *old_chain = 0;
2585 struct expression *expr;
2586 struct agent_expr *agent;
2587 const char *arg;
2588 int trace_string = 0;
2589
2590 if (!eval)
2591 {
2592 if (*exp == '/')
2593 exp = decode_agent_options (exp, &trace_string);
2594 }
2595
2596 arg = exp;
2597 if (!eval && strcmp (arg, "$_ret") == 0)
2598 {
2599 agent = gen_trace_for_return_address (pc, get_current_arch (),
2600 trace_string);
2601 old_chain = make_cleanup_free_agent_expr (agent);
2602 }
2603 else
2604 {
2605 expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
2606 old_chain = make_cleanup (free_current_contents, &expr);
2607 if (eval)
2608 {
2609 gdb_assert (trace_string == 0);
2610 agent = gen_eval_for_expr (pc, expr);
2611 }
2612 else
2613 agent = gen_trace_for_expr (pc, expr, trace_string);
2614 make_cleanup_free_agent_expr (agent);
2615 }
2616
2617 ax_reqs (agent);
2618 ax_print (gdb_stdout, agent);
2619
2620 /* It would be nice to call ax_reqs here to gather some general info
2621 about the expression, and then print out the result. */
2622
2623 do_cleanups (old_chain);
2624 dont_repeat ();
2625 }
2626
2627 static void
2628 agent_command_1 (char *exp, int eval)
2629 {
2630 /* We don't deal with overlay debugging at the moment. We need to
2631 think more carefully about this. If you copy this code into
2632 another command, change the error message; the user shouldn't
2633 have to know anything about agent expressions. */
2634 if (overlay_debugging)
2635 error (_("GDB can't do agent expression translation with overlays."));
2636
2637 if (exp == 0)
2638 error_no_arg (_("expression to translate"));
2639
2640 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2641 {
2642 struct linespec_result canonical;
2643 int ix;
2644 struct linespec_sals *iter;
2645 struct cleanup *old_chain;
2646
2647 exp = skip_spaces (exp);
2648 init_linespec_result (&canonical);
2649 decode_line_full (&exp, DECODE_LINE_FUNFIRSTLINE,
2650 (struct symtab *) NULL, 0, &canonical,
2651 NULL, NULL);
2652 old_chain = make_cleanup_destroy_linespec_result (&canonical);
2653 exp = skip_spaces (exp);
2654 if (exp[0] == ',')
2655 {
2656 exp++;
2657 exp = skip_spaces (exp);
2658 }
2659 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
2660 {
2661 int i;
2662
2663 for (i = 0; i < iter->sals.nelts; i++)
2664 agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
2665 }
2666 do_cleanups (old_chain);
2667 }
2668 else
2669 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
2670
2671 dont_repeat ();
2672 }
2673
2674 static void
2675 agent_command (char *exp, int from_tty)
2676 {
2677 agent_command_1 (exp, 0);
2678 }
2679
2680 /* Parse the given expression, compile it into an agent expression
2681 that does direct evaluation, and display the resulting
2682 expression. */
2683
2684 static void
2685 agent_eval_command (char *exp, int from_tty)
2686 {
2687 agent_command_1 (exp, 1);
2688 }
2689
2690 /* Parse the given expression, compile it into an agent expression
2691 that does a printf, and display the resulting expression. */
2692
2693 static void
2694 maint_agent_printf_command (char *exp, int from_tty)
2695 {
2696 struct cleanup *old_chain = 0;
2697 struct expression *expr;
2698 struct expression *argvec[100];
2699 struct agent_expr *agent;
2700 struct frame_info *fi = get_current_frame (); /* need current scope */
2701 const char *cmdrest;
2702 const char *format_start, *format_end;
2703 struct format_piece *fpieces;
2704 int nargs;
2705
2706 /* We don't deal with overlay debugging at the moment. We need to
2707 think more carefully about this. If you copy this code into
2708 another command, change the error message; the user shouldn't
2709 have to know anything about agent expressions. */
2710 if (overlay_debugging)
2711 error (_("GDB can't do agent expression translation with overlays."));
2712
2713 if (exp == 0)
2714 error_no_arg (_("expression to translate"));
2715
2716 cmdrest = exp;
2717
2718 cmdrest = skip_spaces_const (cmdrest);
2719
2720 if (*cmdrest++ != '"')
2721 error (_("Must start with a format string."));
2722
2723 format_start = cmdrest;
2724
2725 fpieces = parse_format_string (&cmdrest);
2726
2727 old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2728
2729 format_end = cmdrest;
2730
2731 if (*cmdrest++ != '"')
2732 error (_("Bad format string, non-terminated '\"'."));
2733
2734 cmdrest = skip_spaces_const (cmdrest);
2735
2736 if (*cmdrest != ',' && *cmdrest != 0)
2737 error (_("Invalid argument syntax"));
2738
2739 if (*cmdrest == ',')
2740 cmdrest++;
2741 cmdrest = skip_spaces_const (cmdrest);
2742
2743 nargs = 0;
2744 while (*cmdrest != '\0')
2745 {
2746 const char *cmd1;
2747
2748 cmd1 = cmdrest;
2749 expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2750 argvec[nargs] = expr;
2751 ++nargs;
2752 cmdrest = cmd1;
2753 if (*cmdrest == ',')
2754 ++cmdrest;
2755 /* else complain? */
2756 }
2757
2758
2759 agent = gen_printf (get_frame_pc (fi), get_current_arch (), 0, 0,
2760 format_start, format_end - format_start,
2761 fpieces, nargs, argvec);
2762 make_cleanup_free_agent_expr (agent);
2763 ax_reqs (agent);
2764 ax_print (gdb_stdout, agent);
2765
2766 /* It would be nice to call ax_reqs here to gather some general info
2767 about the expression, and then print out the result. */
2768
2769 do_cleanups (old_chain);
2770 dont_repeat ();
2771 }
2772 \f
2773
2774 /* Initialization code. */
2775
2776 void _initialize_ax_gdb (void);
2777 void
2778 _initialize_ax_gdb (void)
2779 {
2780 add_cmd ("agent", class_maintenance, agent_command,
2781 _("\
2782 Translate an expression into remote agent bytecode for tracing.\n\
2783 Usage: maint agent [-at location,] EXPRESSION\n\
2784 If -at is given, generate remote agent bytecode for this location.\n\
2785 If not, generate remote agent bytecode for current frame pc address."),
2786 &maintenancelist);
2787
2788 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2789 _("\
2790 Translate an expression into remote agent bytecode for evaluation.\n\
2791 Usage: maint agent-eval [-at location,] EXPRESSION\n\
2792 If -at is given, generate remote agent bytecode for this location.\n\
2793 If not, generate remote agent bytecode for current frame pc address."),
2794 &maintenancelist);
2795
2796 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2797 _("Translate an expression into remote "
2798 "agent bytecode for evaluation and display the bytecodes."),
2799 &maintenancelist);
2800 }