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