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