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