]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ax-gdb.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / ax-gdb.c
CommitLineData
1bac305b
AC
1/* GDB-specific functions for operating on agent expressions.
2
1d506c26 3 Copyright (C) 1998-2024 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
4de283e4
TT
20#include "symtab.h"
21#include "symfile.h"
22#include "gdbtypes.h"
23#include "language.h"
24#include "value.h"
25#include "expression.h"
26#include "command.h"
5b9707eb 27#include "cli/cli-cmds.h"
4de283e4
TT
28#include "frame.h"
29#include "target.h"
d55e5aa6 30#include "ax.h"
4de283e4 31#include "ax-gdb.h"
fe898f56 32#include "block.h"
4de283e4
TT
33#include "regcache.h"
34#include "user-regs.h"
35#include "dictionary.h"
00bf0b85 36#include "breakpoint.h"
4de283e4 37#include "tracepoint.h"
d55e5aa6 38#include "cp-support.h"
4de283e4
TT
39#include "arch-utils.h"
40#include "cli/cli-utils.h"
34b536a8 41#include "linespec.h"
f00aae0f 42#include "location.h"
77e371c0 43#include "objfiles.h"
46a4882b 44#include "typeprint.h"
3065dfb6 45#include "valprint.h"
4de283e4 46#include "c-lang.h"
75f9892d 47#include "expop.h"
4de283e4 48
268a13a5 49#include "gdbsupport/format.h"
d3ce09f5 50
6426a772
JM
51/* To make sense of this file, you should read doc/agentexpr.texi.
52 Then look at the types and enums in ax-gdb.h. For the code itself,
53 look at gen_expr, towards the bottom; that's the main function that
54 looks at the GDB expressions and calls everything else to generate
55 code.
c906108c
SS
56
57 I'm beginning to wonder whether it wouldn't be nicer to internally
58 generate trees, with types, and then spit out the bytecode in
59 linear form afterwards; we could generate fewer `swap', `ext', and
60 `zero_ext' bytecodes that way; it would make good constant folding
61 easier, too. But at the moment, I think we should be willing to
62 pay for the simplicity of this code with less-than-optimal bytecode
63 strings.
64
c5aa993b
JM
65 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
66\f
c906108c
SS
67
68
0e2de366 69/* Prototypes for local functions. */
c906108c
SS
70
71/* There's a standard order to the arguments of these functions:
c906108c
SS
72 struct agent_expr * --- agent expression buffer to generate code into
73 struct axs_value * --- describes value left on top of stack */
c5aa993b 74
40f4af28 75static void gen_traced_pop (struct agent_expr *, struct axs_value *);
a14ed312
KB
76
77static void gen_sign_extend (struct agent_expr *, struct type *);
78static void gen_extend (struct agent_expr *, struct type *);
79static void gen_fetch (struct agent_expr *, struct type *);
80static void gen_left_shift (struct agent_expr *, int);
81
82
40f4af28
SM
83static void gen_frame_args_address (struct agent_expr *);
84static void gen_frame_locals_address (struct agent_expr *);
a14ed312
KB
85static void gen_offset (struct agent_expr *ax, int offset);
86static void gen_sym_offset (struct agent_expr *, struct symbol *);
40f4af28
SM
87static void gen_var_ref (struct agent_expr *ax, struct axs_value *value,
88 struct symbol *var);
a14ed312
KB
89
90
91static void gen_int_literal (struct agent_expr *ax,
92 struct axs_value *value,
93 LONGEST k, struct type *type);
94
6661ad48 95static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
a14ed312
KB
96static int type_wider_than (struct type *type1, struct type *type2);
97static struct type *max_type (struct type *type1, struct type *type2);
98static void gen_conversion (struct agent_expr *ax,
99 struct type *from, struct type *to);
100static int is_nontrivial_conversion (struct type *from, struct type *to);
6661ad48 101static void gen_usual_arithmetic (struct agent_expr *ax,
a14ed312
KB
102 struct axs_value *value1,
103 struct axs_value *value2);
6661ad48 104static void gen_integral_promotions (struct agent_expr *ax,
a14ed312
KB
105 struct axs_value *value);
106static void gen_cast (struct agent_expr *ax,
107 struct axs_value *value, struct type *type);
108static void gen_scale (struct agent_expr *ax,
109 enum agent_op op, struct type *type);
f7c79c41
UW
110static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
111 struct axs_value *value1, struct axs_value *value2);
112static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
113 struct axs_value *value1, struct axs_value *value2);
114static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
115 struct axs_value *value1, struct axs_value *value2,
116 struct type *result_type);
a14ed312
KB
117static void gen_binop (struct agent_expr *ax,
118 struct axs_value *value,
119 struct axs_value *value1,
120 struct axs_value *value2,
121 enum agent_op op,
a121b7c1
PA
122 enum agent_op op_unsigned, int may_carry,
123 const char *name);
f7c79c41
UW
124static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
125 struct type *result_type);
a14ed312 126static void gen_complement (struct agent_expr *ax, struct axs_value *value);
053f8057
SM
127static void gen_deref (struct axs_value *);
128static void gen_address_of (struct axs_value *);
6661ad48 129static void gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
a14ed312 130 struct type *type, int start, int end);
6661ad48 131static void gen_primitive_field (struct agent_expr *ax,
b6e7192f
SS
132 struct axs_value *value,
133 int offset, int fieldno, struct type *type);
6661ad48 134static int gen_struct_ref_recursive (struct agent_expr *ax,
b6e7192f 135 struct axs_value *value,
a121b7c1 136 const char *field, int offset,
b6e7192f 137 struct type *type);
6661ad48 138static void gen_struct_ref (struct agent_expr *ax,
a14ed312 139 struct axs_value *value,
a121b7c1
PA
140 const char *field,
141 const char *operator_name,
142 const char *operand_name);
40f4af28 143static void gen_static_field (struct agent_expr *ax, struct axs_value *value,
b6e7192f 144 struct type *type, int fieldno);
75f9892d
TT
145static void gen_expr_binop_rest (struct expression *exp,
146 enum exp_opcode op,
147 struct agent_expr *ax,
148 struct axs_value *value,
149 struct axs_value *value1,
150 struct axs_value *value2);
c906108c 151
c906108c 152\f
c5aa993b 153
c906108c
SS
154/* Generating bytecode from GDB expressions: general assumptions */
155
156/* Here are a few general assumptions made throughout the code; if you
157 want to make a change that contradicts one of these, then you'd
158 better scan things pretty thoroughly.
159
160 - We assume that all values occupy one stack element. For example,
c5aa993b
JM
161 sometimes we'll swap to get at the left argument to a binary
162 operator. If we decide that void values should occupy no stack
163 elements, or that synthetic arrays (whose size is determined at
164 run time, created by the `@' operator) should occupy two stack
165 elements (address and length), then this will cause trouble.
c906108c
SS
166
167 - We assume the stack elements are infinitely wide, and that we
c5aa993b
JM
168 don't have to worry what happens if the user requests an
169 operation that is wider than the actual interpreter's stack.
170 That is, it's up to the interpreter to handle directly all the
171 integer widths the user has access to. (Woe betide the language
172 with bignums!)
c906108c
SS
173
174 - We don't support side effects. Thus, we don't have to worry about
c5aa993b 175 GCC's generalized lvalues, function calls, etc.
c906108c
SS
176
177 - We don't support floating point. Many places where we switch on
c5aa993b
JM
178 some type don't bother to include cases for floating point; there
179 may be even more subtle ways this assumption exists. For
180 example, the arguments to % must be integers.
c906108c
SS
181
182 - We assume all subexpressions have a static, unchanging type. If
c5aa993b
JM
183 we tried to support convenience variables, this would be a
184 problem.
c906108c
SS
185
186 - All values on the stack should always be fully zero- or
c5aa993b
JM
187 sign-extended.
188
189 (I wasn't sure whether to choose this or its opposite --- that
190 only addresses are assumed extended --- but it turns out that
191 neither convention completely eliminates spurious extend
192 operations (if everything is always extended, then you have to
193 extend after add, because it could overflow; if nothing is
194 extended, then you end up producing extends whenever you change
195 sizes), and this is simpler.) */
c906108c 196\f
c5aa993b 197
400c6af0
SS
198/* Scan for all static fields in the given class, including any base
199 classes, and generate tracing bytecodes for each. */
200
201static void
40f4af28 202gen_trace_static_fields (struct agent_expr *ax,
400c6af0
SS
203 struct type *type)
204{
205 int i, nbases = TYPE_N_BASECLASSES (type);
206 struct axs_value value;
207
f168693b 208 type = check_typedef (type);
400c6af0 209
1f704f76 210 for (i = type->num_fields () - 1; i >= nbases; i--)
400c6af0 211 {
c819a338 212 if (type->field (i).is_static ())
400c6af0 213 {
40f4af28 214 gen_static_field (ax, &value, type, i);
400c6af0
SS
215 if (value.optimized_out)
216 continue;
217 switch (value.kind)
218 {
219 case axs_lvalue_memory:
220 {
dda83cd7
SM
221 /* Initialize the TYPE_LENGTH if it is a typedef. */
222 check_typedef (value.type);
df86565b 223 ax_const_l (ax, value.type->length ());
400c6af0
SS
224 ax_simple (ax, aop_trace);
225 }
226 break;
227
228 case axs_lvalue_register:
35c9c7ba
SS
229 /* We don't actually need the register's value to be pushed,
230 just note that we need it to be collected. */
231 ax_reg_mask (ax, value.u.reg);
400c6af0
SS
232
233 default:
234 break;
235 }
236 }
237 }
238
239 /* Now scan through base classes recursively. */
240 for (i = 0; i < nbases; i++)
241 {
242 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
243
40f4af28 244 gen_trace_static_fields (ax, basetype);
400c6af0
SS
245 }
246}
247
c906108c
SS
248/* Trace the lvalue on the stack, if it needs it. In either case, pop
249 the value. Useful on the left side of a comma, and at the end of
250 an expression being used for tracing. */
251static void
40f4af28 252gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
c906108c 253{
3065dfb6 254 int string_trace = 0;
92bc6a20 255 if (ax->trace_string
78134374 256 && value->type->code () == TYPE_CODE_PTR
27710edb 257 && c_textual_element_type (check_typedef (value->type->target_type ()),
3065dfb6
SS
258 's'))
259 string_trace = 1;
260
92bc6a20 261 if (ax->tracing)
c906108c
SS
262 switch (value->kind)
263 {
264 case axs_rvalue:
3065dfb6
SS
265 if (string_trace)
266 {
92bc6a20 267 ax_const_l (ax, ax->trace_string);
3065dfb6
SS
268 ax_simple (ax, aop_tracenz);
269 }
270 else
271 /* We don't trace rvalues, just the lvalues necessary to
272 produce them. So just dispose of this value. */
273 ax_simple (ax, aop_pop);
c906108c
SS
274 break;
275
276 case axs_lvalue_memory:
277 {
744a8059
SP
278 /* Initialize the TYPE_LENGTH if it is a typedef. */
279 check_typedef (value->type);
280
3065dfb6
SS
281 if (string_trace)
282 {
f906b857 283 gen_fetch (ax, value->type);
92bc6a20 284 ax_const_l (ax, ax->trace_string);
3065dfb6
SS
285 ax_simple (ax, aop_tracenz);
286 }
f906b857
MK
287 else
288 {
289 /* There's no point in trying to use a trace_quick bytecode
dda83cd7
SM
290 here, since "trace_quick SIZE pop" is three bytes, whereas
291 "const8 SIZE trace" is also three bytes, does the same
292 thing, and the simplest code which generates that will also
293 work correctly for objects with large sizes. */
df86565b 294 ax_const_l (ax, value->type->length ());
f906b857
MK
295 ax_simple (ax, aop_trace);
296 }
c906108c 297 }
c5aa993b 298 break;
c906108c
SS
299
300 case axs_lvalue_register:
35c9c7ba
SS
301 /* We don't actually need the register's value to be on the
302 stack, and the target will get heartburn if the register is
303 larger than will fit in a stack, so just mark it for
304 collection and be done with it. */
305 ax_reg_mask (ax, value->u.reg);
3065dfb6
SS
306
307 /* But if the register points to a string, assume the value
308 will fit on the stack and push it anyway. */
309 if (string_trace)
310 {
311 ax_reg (ax, value->u.reg);
92bc6a20 312 ax_const_l (ax, ax->trace_string);
3065dfb6
SS
313 ax_simple (ax, aop_tracenz);
314 }
c906108c
SS
315 break;
316 }
317 else
318 /* If we're not tracing, just pop the value. */
319 ax_simple (ax, aop_pop);
400c6af0
SS
320
321 /* To trace C++ classes with static fields stored elsewhere. */
92bc6a20 322 if (ax->tracing
78134374
SM
323 && (value->type->code () == TYPE_CODE_STRUCT
324 || value->type->code () == TYPE_CODE_UNION))
40f4af28 325 gen_trace_static_fields (ax, value->type);
c906108c 326}
c5aa993b 327\f
c906108c
SS
328
329
c906108c
SS
330/* Generating bytecode from GDB expressions: helper functions */
331
332/* Assume that the lower bits of the top of the stack is a value of
333 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
334static void
fba45db2 335gen_sign_extend (struct agent_expr *ax, struct type *type)
c906108c
SS
336{
337 /* Do we need to sign-extend this? */
c6d940a9 338 if (!type->is_unsigned ())
df86565b 339 ax_ext (ax, type->length () * TARGET_CHAR_BIT);
c906108c
SS
340}
341
342
343/* Assume the lower bits of the top of the stack hold a value of type
344 TYPE, and the upper bits are garbage. Sign-extend or truncate as
345 needed. */
346static void
fba45db2 347gen_extend (struct agent_expr *ax, struct type *type)
c906108c 348{
df86565b 349 int bits = type->length () * TARGET_CHAR_BIT;
5b4ee69b 350
c906108c 351 /* I just had to. */
c6d940a9 352 ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, bits));
c906108c
SS
353}
354
935d4856
TT
355/* A helper that returns the target type if TYPE is a range type, or
356 otherwise just returns TYPE. */
357
358static struct type *
359strip_range_type (struct type *type)
360{
361 if (type->code () == TYPE_CODE_RANGE)
362 return type->target_type ();
363 return type;
364}
c906108c
SS
365
366/* Assume that the top of the stack contains a value of type "pointer
367 to TYPE"; generate code to fetch its value. Note that TYPE is the
368 target type, not the pointer type. */
369static void
fba45db2 370gen_fetch (struct agent_expr *ax, struct type *type)
c906108c 371{
92bc6a20 372 if (ax->tracing)
c906108c
SS
373 {
374 /* Record the area of memory we're about to fetch. */
df86565b 375 ax_trace_quick (ax, type->length ());
c906108c
SS
376 }
377
935d4856 378 type = strip_range_type (type);
af381b8c 379
78134374 380 switch (type->code ())
c906108c
SS
381 {
382 case TYPE_CODE_PTR:
b97aedf3 383 case TYPE_CODE_REF:
aa006118 384 case TYPE_CODE_RVALUE_REF:
c906108c
SS
385 case TYPE_CODE_ENUM:
386 case TYPE_CODE_INT:
387 case TYPE_CODE_CHAR:
3b11a015 388 case TYPE_CODE_BOOL:
c906108c 389 /* It's a scalar value, so we know how to dereference it. How
dda83cd7 390 many bytes long is it? */
df86565b 391 switch (type->length ())
c906108c 392 {
c5aa993b
JM
393 case 8 / TARGET_CHAR_BIT:
394 ax_simple (ax, aop_ref8);
395 break;
396 case 16 / TARGET_CHAR_BIT:
397 ax_simple (ax, aop_ref16);
398 break;
399 case 32 / TARGET_CHAR_BIT:
400 ax_simple (ax, aop_ref32);
401 break;
402 case 64 / TARGET_CHAR_BIT:
403 ax_simple (ax, aop_ref64);
404 break;
c906108c
SS
405
406 /* Either our caller shouldn't have asked us to dereference
407 that pointer (other code's fault), or we're not
408 implementing something we should be (this code's fault).
409 In any case, it's a bug the user shouldn't see. */
410 default:
f34652de 411 internal_error (_("gen_fetch: strange size"));
c906108c
SS
412 }
413
414 gen_sign_extend (ax, type);
415 break;
416
417 default:
52323be9
LM
418 /* Our caller requested us to dereference a pointer from an unsupported
419 type. Error out and give callers a chance to handle the failure
420 gracefully. */
421 error (_("gen_fetch: Unsupported type code `%s'."),
7d93a1e0 422 type->name ());
c906108c
SS
423 }
424}
425
426
427/* Generate code to left shift the top of the stack by DISTANCE bits, or
428 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
429 unsigned (logical) right shifts. */
430static void
fba45db2 431gen_left_shift (struct agent_expr *ax, int distance)
c906108c
SS
432{
433 if (distance > 0)
434 {
435 ax_const_l (ax, distance);
436 ax_simple (ax, aop_lsh);
437 }
438 else if (distance < 0)
439 {
440 ax_const_l (ax, -distance);
441 ax_simple (ax, aop_rsh_unsigned);
442 }
443}
c5aa993b 444\f
c906108c
SS
445
446
c906108c
SS
447/* Generating bytecode from GDB expressions: symbol references */
448
449/* Generate code to push the base address of the argument portion of
450 the top stack frame. */
451static void
40f4af28 452gen_frame_args_address (struct agent_expr *ax)
c906108c 453{
39d4ef09
AC
454 int frame_reg;
455 LONGEST frame_offset;
c906108c 456
40f4af28 457 gdbarch_virtual_frame_pointer (ax->gdbarch,
c7bb205c 458 ax->scope, &frame_reg, &frame_offset);
c5aa993b 459 ax_reg (ax, frame_reg);
c906108c
SS
460 gen_offset (ax, frame_offset);
461}
462
463
464/* Generate code to push the base address of the locals portion of the
465 top stack frame. */
466static void
40f4af28 467gen_frame_locals_address (struct agent_expr *ax)
c906108c 468{
39d4ef09
AC
469 int frame_reg;
470 LONGEST frame_offset;
c906108c 471
40f4af28 472 gdbarch_virtual_frame_pointer (ax->gdbarch,
c7bb205c 473 ax->scope, &frame_reg, &frame_offset);
c5aa993b 474 ax_reg (ax, frame_reg);
c906108c
SS
475 gen_offset (ax, frame_offset);
476}
477
478
479/* Generate code to add OFFSET to the top of the stack. Try to
480 generate short and readable code. We use this for getting to
481 variables on the stack, and structure members. If we were
482 programming in ML, it would be clearer why these are the same
483 thing. */
484static void
fba45db2 485gen_offset (struct agent_expr *ax, int offset)
c906108c
SS
486{
487 /* It would suffice to simply push the offset and add it, but this
488 makes it easier to read positive and negative offsets in the
489 bytecode. */
490 if (offset > 0)
491 {
492 ax_const_l (ax, offset);
493 ax_simple (ax, aop_add);
494 }
495 else if (offset < 0)
496 {
497 ax_const_l (ax, -offset);
498 ax_simple (ax, aop_sub);
499 }
500}
501
502
503/* In many cases, a symbol's value is the offset from some other
504 address (stack frame, base register, etc.) Generate code to add
505 VAR's value to the top of the stack. */
506static void
fba45db2 507gen_sym_offset (struct agent_expr *ax, struct symbol *var)
c906108c 508{
4aeddc50 509 gen_offset (ax, var->value_longest ());
c906108c
SS
510}
511
512
513/* Generate code for a variable reference to AX. The variable is the
514 symbol VAR. Set VALUE to describe the result. */
515
516static void
40f4af28 517gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
c906108c 518{
0e2de366 519 /* Dereference any typedefs. */
5f9c5a63 520 value->type = check_typedef (var->type ());
400c6af0 521 value->optimized_out = 0;
c906108c 522
7ae24327
SM
523 if (const symbol_computed_ops *computed_ops = var->computed_ops ();
524 computed_ops != nullptr)
525 return computed_ops->tracepoint_var_ref (var, ax, value);
24d6c2a0 526
c906108c 527 /* I'm imitating the code in read_var_value. */
66d7f48f 528 switch (var->aclass ())
c906108c
SS
529 {
530 case LOC_CONST: /* A constant, like an enum value. */
4aeddc50 531 ax_const_l (ax, (LONGEST) var->value_longest ());
c906108c
SS
532 value->kind = axs_rvalue;
533 break;
534
535 case LOC_LABEL: /* A goto label, being used as a value. */
4aeddc50 536 ax_const_l (ax, (LONGEST) var->value_address ());
c906108c
SS
537 value->kind = axs_rvalue;
538 break;
539
540 case LOC_CONST_BYTES:
f34652de 541 internal_error (_("gen_var_ref: LOC_CONST_BYTES "
3e43a32a 542 "symbols are not supported"));
c906108c
SS
543
544 /* Variable at a fixed location in memory. Easy. */
545 case LOC_STATIC:
546 /* Push the address of the variable. */
4aeddc50 547 ax_const_l (ax, var->value_address ());
c906108c
SS
548 value->kind = axs_lvalue_memory;
549 break;
550
551 case LOC_ARG: /* var lives in argument area of frame */
40f4af28 552 gen_frame_args_address (ax);
c906108c
SS
553 gen_sym_offset (ax, var);
554 value->kind = axs_lvalue_memory;
555 break;
556
557 case LOC_REF_ARG: /* As above, but the frame slot really
558 holds the address of the variable. */
40f4af28 559 gen_frame_args_address (ax);
c906108c
SS
560 gen_sym_offset (ax, var);
561 /* Don't assume any particular pointer size. */
40f4af28 562 gen_fetch (ax, builtin_type (ax->gdbarch)->builtin_data_ptr);
c906108c
SS
563 value->kind = axs_lvalue_memory;
564 break;
565
566 case LOC_LOCAL: /* var lives in locals area of frame */
40f4af28 567 gen_frame_locals_address (ax);
c906108c
SS
568 gen_sym_offset (ax, var);
569 value->kind = axs_lvalue_memory;
570 break;
571
c906108c 572 case LOC_TYPEDEF:
3d263c1d 573 error (_("Cannot compute value of typedef `%s'."),
987012b8 574 var->print_name ());
c906108c
SS
575 break;
576
577 case LOC_BLOCK:
6395b628 578 ax_const_l (ax, var->value_block ()->entry_pc ());
c906108c
SS
579 value->kind = axs_rvalue;
580 break;
581
582 case LOC_REGISTER:
c906108c 583 /* Don't generate any code at all; in the process of treating
dda83cd7
SM
584 this as an lvalue or rvalue, the caller will generate the
585 right code. */
c906108c 586 value->kind = axs_lvalue_register;
7ae24327 587 value->u.reg = var->register_ops ()->register_number (var, ax->gdbarch);
c906108c
SS
588 break;
589
590 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
dda83cd7
SM
591 register, not on the stack. Simpler than LOC_REGISTER
592 because it's just like any other case where the thing
2a2d4dc3 593 has a real address. */
c906108c 594 case LOC_REGPARM_ADDR:
7ae24327 595 ax_reg (ax, var->register_ops ()->register_number (var, ax->gdbarch));
c906108c
SS
596 value->kind = axs_lvalue_memory;
597 break;
598
599 case LOC_UNRESOLVED:
600 {
3b7344d5 601 struct bound_minimal_symbol msym
987012b8 602 = lookup_minimal_symbol (var->linkage_name (), NULL, NULL);
5b4ee69b 603
3b7344d5 604 if (!msym.minsym)
987012b8 605 error (_("Couldn't resolve symbol `%s'."), var->print_name ());
c5aa993b 606
c906108c 607 /* Push the address of the variable. */
4aeddc50 608 ax_const_l (ax, msym.value_address ());
c906108c
SS
609 value->kind = axs_lvalue_memory;
610 }
c5aa993b 611 break;
c906108c 612
a55cc764 613 case LOC_COMPUTED:
557b4d76 614 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
a55cc764 615
c906108c 616 case LOC_OPTIMIZED_OUT:
400c6af0
SS
617 /* Flag this, but don't say anything; leave it up to callers to
618 warn the user. */
619 value->optimized_out = 1;
c906108c
SS
620 break;
621
622 default:
3d263c1d 623 error (_("Cannot find value of botched symbol `%s'."),
987012b8 624 var->print_name ());
c906108c
SS
625 break;
626 }
627}
74ea4be4
PA
628
629/* Generate code for a minimal symbol variable reference to AX. The
630 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
631 the result. */
632
633static void
634gen_msym_var_ref (agent_expr *ax, axs_value *value,
635 minimal_symbol *msymbol, objfile *objf)
636{
637 CORE_ADDR address;
638 type *t = find_minsym_type_and_address (msymbol, objf, &address);
639 value->type = t;
640 value->optimized_out = false;
641 ax_const_l (ax, address);
642 value->kind = axs_lvalue_memory;
643}
644
c5aa993b 645\f
c906108c
SS
646
647
c906108c
SS
648/* Generating bytecode from GDB expressions: literals */
649
650static void
fba45db2
KB
651gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
652 struct type *type)
c906108c
SS
653{
654 ax_const_l (ax, k);
655 value->kind = axs_rvalue;
648027cc 656 value->type = check_typedef (type);
c906108c 657}
c5aa993b 658\f
c906108c
SS
659
660
c906108c
SS
661/* Generating bytecode from GDB expressions: unary conversions, casts */
662
663/* Take what's on the top of the stack (as described by VALUE), and
664 try to make an rvalue out of it. Signal an error if we can't do
665 that. */
55aa24fb 666void
fba45db2 667require_rvalue (struct agent_expr *ax, struct axs_value *value)
c906108c 668{
3a96536b
SS
669 /* Only deal with scalars, structs and such may be too large
670 to fit in a stack entry. */
671 value->type = check_typedef (value->type);
78134374
SM
672 if (value->type->code () == TYPE_CODE_ARRAY
673 || value->type->code () == TYPE_CODE_STRUCT
674 || value->type->code () == TYPE_CODE_UNION
675 || value->type->code () == TYPE_CODE_FUNC)
1c40aa62 676 error (_("Value not scalar: cannot be an rvalue."));
3a96536b 677
c906108c
SS
678 switch (value->kind)
679 {
680 case axs_rvalue:
681 /* It's already an rvalue. */
682 break;
683
684 case axs_lvalue_memory:
685 /* The top of stack is the address of the object. Dereference. */
686 gen_fetch (ax, value->type);
687 break;
688
689 case axs_lvalue_register:
690 /* There's nothing on the stack, but value->u.reg is the
dda83cd7 691 register number containing the value.
c906108c 692
dda83cd7
SM
693 When we add floating-point support, this is going to have to
694 change. What about SPARC register pairs, for example? */
c906108c
SS
695 ax_reg (ax, value->u.reg);
696 gen_extend (ax, value->type);
697 break;
698 }
699
700 value->kind = axs_rvalue;
701}
702
703
704/* Assume the top of the stack is described by VALUE, and perform the
705 usual unary conversions. This is motivated by ANSI 6.2.2, but of
706 course GDB expressions are not ANSI; they're the mishmash union of
707 a bunch of languages. Rah.
708
709 NOTE! This function promises to produce an rvalue only when the
710 incoming value is of an appropriate type. In other words, the
711 consumer of the value this function produces may assume the value
712 is an rvalue only after checking its type.
713
714 The immediate issue is that if the user tries to use a structure or
715 union as an operand of, say, the `+' operator, we don't want to try
716 to convert that structure to an rvalue; require_rvalue will bomb on
717 structs and unions. Rather, we want to simply pass the struct
718 lvalue through unchanged, and let `+' raise an error. */
719
720static void
6661ad48 721gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
722{
723 /* We don't have to generate any code for the usual integral
724 conversions, since values are always represented as full-width on
725 the stack. Should we tweak the type? */
726
727 /* Some types require special handling. */
78134374 728 switch (value->type->code ())
c906108c
SS
729 {
730 /* Functions get converted to a pointer to the function. */
731 case TYPE_CODE_FUNC:
732 value->type = lookup_pointer_type (value->type);
733 value->kind = axs_rvalue; /* Should always be true, but just in case. */
734 break;
735
736 /* Arrays get converted to a pointer to their first element, and
dda83cd7 737 are no longer an lvalue. */
c906108c
SS
738 case TYPE_CODE_ARRAY:
739 {
27710edb 740 struct type *elements = value->type->target_type ();
5b4ee69b 741
c906108c
SS
742 value->type = lookup_pointer_type (elements);
743 value->kind = axs_rvalue;
744 /* We don't need to generate any code; the address of the array
745 is also the address of its first element. */
746 }
c5aa993b 747 break;
c906108c 748
c5aa993b 749 /* Don't try to convert structures and unions to rvalues. Let the
dda83cd7 750 consumer signal an error. */
c906108c
SS
751 case TYPE_CODE_STRUCT:
752 case TYPE_CODE_UNION:
753 return;
c906108c
SS
754 }
755
756 /* If the value is an lvalue, dereference it. */
757 require_rvalue (ax, value);
758}
759
760
761/* Return non-zero iff the type TYPE1 is considered "wider" than the
762 type TYPE2, according to the rules described in gen_usual_arithmetic. */
763static int
fba45db2 764type_wider_than (struct type *type1, struct type *type2)
c906108c 765{
df86565b
SM
766 return (type1->length () > type2->length ()
767 || (type1->length () == type2->length ()
c6d940a9
SM
768 && type1->is_unsigned ()
769 && !type2->is_unsigned ()));
c906108c
SS
770}
771
772
773/* Return the "wider" of the two types TYPE1 and TYPE2. */
774static struct type *
fba45db2 775max_type (struct type *type1, struct type *type2)
c906108c
SS
776{
777 return type_wider_than (type1, type2) ? type1 : type2;
778}
779
780
781/* Generate code to convert a scalar value of type FROM to type TO. */
782static void
fba45db2 783gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
c906108c
SS
784{
785 /* Perhaps there is a more graceful way to state these rules. */
786
787 /* If we're converting to a narrower type, then we need to clear out
788 the upper bits. */
df86565b 789 if (to->length () < from->length ())
bcf5c1d9 790 gen_extend (ax, to);
c906108c
SS
791
792 /* If the two values have equal width, but different signednesses,
793 then we need to extend. */
df86565b 794 else if (to->length () == from->length ())
c906108c 795 {
c6d940a9 796 if (from->is_unsigned () != to->is_unsigned ())
c906108c
SS
797 gen_extend (ax, to);
798 }
799
800 /* If we're converting to a wider type, and becoming unsigned, then
801 we need to zero out any possible sign bits. */
df86565b 802 else if (to->length () > from->length ())
c906108c 803 {
c6d940a9 804 if (to->is_unsigned ())
c906108c
SS
805 gen_extend (ax, to);
806 }
807}
808
809
810/* Return non-zero iff the type FROM will require any bytecodes to be
811 emitted to be converted to the type TO. */
812static int
fba45db2 813is_nontrivial_conversion (struct type *from, struct type *to)
c906108c 814{
833177a4 815 agent_expr_up ax (new agent_expr (NULL, 0));
c906108c
SS
816
817 /* Actually generate the code, and see if anything came out. At the
818 moment, it would be trivial to replicate the code in
819 gen_conversion here, but in the future, when we're supporting
820 floating point and the like, it may not be. Doing things this
821 way allows this function to be independent of the logic in
822 gen_conversion. */
833177a4 823 gen_conversion (ax.get (), from, to);
6f96f485 824 return !ax->buf.empty ();
c906108c
SS
825}
826
827
828/* Generate code to perform the "usual arithmetic conversions" (ANSI C
829 6.2.1.5) for the two operands of an arithmetic operator. This
830 effectively finds a "least upper bound" type for the two arguments,
831 and promotes each argument to that type. *VALUE1 and *VALUE2
832 describe the values as they are passed in, and as they are left. */
833static void
6661ad48
SM
834gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
835 struct axs_value *value2)
c906108c 836{
935d4856
TT
837 struct type *type1 = strip_range_type (value1->type);
838 struct type *type2 = strip_range_type (value2->type);
839
c906108c 840 /* Do the usual binary conversions. */
935d4856
TT
841 if (type1->code () == TYPE_CODE_INT
842 && type2->code () == TYPE_CODE_INT)
c906108c
SS
843 {
844 /* The ANSI integral promotions seem to work this way: Order the
dda83cd7
SM
845 integer types by size, and then by signedness: an n-bit
846 unsigned type is considered "wider" than an n-bit signed
847 type. Promote to the "wider" of the two types, and always
848 promote at least to int. */
6661ad48 849 struct type *target = max_type (builtin_type (ax->gdbarch)->builtin_int,
935d4856 850 max_type (type1, type2));
c906108c
SS
851
852 /* Deal with value2, on the top of the stack. */
935d4856 853 gen_conversion (ax, type2, target);
c906108c
SS
854
855 /* Deal with value1, not on the top of the stack. Don't
dda83cd7
SM
856 generate the `swap' instructions if we're not actually going
857 to do anything. */
935d4856 858 if (is_nontrivial_conversion (type1, target))
c906108c
SS
859 {
860 ax_simple (ax, aop_swap);
935d4856 861 gen_conversion (ax, type1, target);
c906108c
SS
862 ax_simple (ax, aop_swap);
863 }
864
648027cc 865 value1->type = value2->type = check_typedef (target);
c906108c
SS
866 }
867}
868
869
870/* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
871 the value on the top of the stack, as described by VALUE. Assume
872 the value has integral type. */
873static void
6661ad48 874gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
c906108c 875{
6661ad48 876 const struct builtin_type *builtin = builtin_type (ax->gdbarch);
f7c79c41
UW
877
878 if (!type_wider_than (value->type, builtin->builtin_int))
c906108c 879 {
f7c79c41
UW
880 gen_conversion (ax, value->type, builtin->builtin_int);
881 value->type = builtin->builtin_int;
c906108c 882 }
f7c79c41 883 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
c906108c 884 {
f7c79c41
UW
885 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
886 value->type = builtin->builtin_unsigned_int;
c906108c
SS
887 }
888}
889
890
891/* Generate code for a cast to TYPE. */
892static void
fba45db2 893gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
c906108c
SS
894{
895 /* GCC does allow casts to yield lvalues, so this should be fixed
896 before merging these changes into the trunk. */
897 require_rvalue (ax, value);
0e2de366 898 /* Dereference typedefs. */
c906108c 899 type = check_typedef (type);
935d4856 900 type = strip_range_type (type);
c906108c 901
78134374 902 switch (type->code ())
c906108c
SS
903 {
904 case TYPE_CODE_PTR:
b97aedf3 905 case TYPE_CODE_REF:
aa006118 906 case TYPE_CODE_RVALUE_REF:
c906108c 907 /* It's implementation-defined, and I'll bet this is what GCC
dda83cd7 908 does. */
c906108c
SS
909 break;
910
911 case TYPE_CODE_ARRAY:
912 case TYPE_CODE_STRUCT:
913 case TYPE_CODE_UNION:
914 case TYPE_CODE_FUNC:
3d263c1d 915 error (_("Invalid type cast: intended type must be scalar."));
c906108c
SS
916
917 case TYPE_CODE_ENUM:
3b11a015 918 case TYPE_CODE_BOOL:
c906108c 919 /* We don't have to worry about the size of the value, because
dda83cd7
SM
920 all our integral values are fully sign-extended, and when
921 casting pointers we can do anything we like. Is there any
922 way for us to know what GCC actually does with a cast like
923 this? */
c906108c 924 break;
c5aa993b 925
c906108c
SS
926 case TYPE_CODE_INT:
927 gen_conversion (ax, value->type, type);
928 break;
929
930 case TYPE_CODE_VOID:
931 /* We could pop the value, and rely on everyone else to check
dda83cd7
SM
932 the type and notice that this value doesn't occupy a stack
933 slot. But for now, leave the value on the stack, and
934 preserve the "value == stack element" assumption. */
c906108c
SS
935 break;
936
937 default:
3d263c1d 938 error (_("Casts to requested type are not yet implemented."));
c906108c
SS
939 }
940
941 value->type = type;
942}
c5aa993b 943\f
c906108c
SS
944
945
c906108c
SS
946/* Generating bytecode from GDB expressions: arithmetic */
947
948/* Scale the integer on the top of the stack by the size of the target
949 of the pointer type TYPE. */
950static void
fba45db2 951gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
c906108c 952{
27710edb 953 struct type *element = type->target_type ();
c906108c 954
df86565b 955 if (element->length () != 1)
c906108c 956 {
df86565b 957 ax_const_l (ax, element->length ());
c906108c
SS
958 ax_simple (ax, op);
959 }
960}
961
962
f7c79c41 963/* Generate code for pointer arithmetic PTR + INT. */
c906108c 964static void
f7c79c41
UW
965gen_ptradd (struct agent_expr *ax, struct axs_value *value,
966 struct axs_value *value1, struct axs_value *value2)
c906108c 967{
809f3be1 968 gdb_assert (value1->type->is_pointer_or_reference ());
935d4856 969 gdb_assert (strip_range_type (value2->type)->code () == TYPE_CODE_INT);
c906108c 970
f7c79c41
UW
971 gen_scale (ax, aop_mul, value1->type);
972 ax_simple (ax, aop_add);
973 gen_extend (ax, value1->type); /* Catch overflow. */
974 value->type = value1->type;
975 value->kind = axs_rvalue;
976}
c906108c 977
c906108c 978
f7c79c41
UW
979/* Generate code for pointer arithmetic PTR - INT. */
980static void
981gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
982 struct axs_value *value1, struct axs_value *value2)
983{
809f3be1 984 gdb_assert (value1->type->is_pointer_or_reference ());
935d4856 985 gdb_assert (strip_range_type (value2->type)->code () == TYPE_CODE_INT);
c906108c 986
f7c79c41
UW
987 gen_scale (ax, aop_mul, value1->type);
988 ax_simple (ax, aop_sub);
989 gen_extend (ax, value1->type); /* Catch overflow. */
990 value->type = value1->type;
c906108c
SS
991 value->kind = axs_rvalue;
992}
993
994
f7c79c41 995/* Generate code for pointer arithmetic PTR - PTR. */
c906108c 996static void
f7c79c41
UW
997gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
998 struct axs_value *value1, struct axs_value *value2,
999 struct type *result_type)
c906108c 1000{
809f3be1
TT
1001 gdb_assert (value1->type->is_pointer_or_reference ());
1002 gdb_assert (value2->type->is_pointer_or_reference ());
c906108c 1003
df86565b
SM
1004 if (value1->type->target_type ()->length ()
1005 != value2->type->target_type ()->length ())
ac74f770
MS
1006 error (_("\
1007First argument of `-' is a pointer, but second argument is neither\n\
1008an integer nor a pointer of the same type."));
c906108c 1009
f7c79c41
UW
1010 ax_simple (ax, aop_sub);
1011 gen_scale (ax, aop_div_unsigned, value1->type);
1012 value->type = result_type;
c906108c
SS
1013 value->kind = axs_rvalue;
1014}
1015
3b11a015
SS
1016static void
1017gen_equal (struct agent_expr *ax, struct axs_value *value,
1018 struct axs_value *value1, struct axs_value *value2,
1019 struct type *result_type)
1020{
809f3be1 1021 if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ())
3b11a015
SS
1022 ax_simple (ax, aop_equal);
1023 else
1024 gen_binop (ax, value, value1, value2,
1025 aop_equal, aop_equal, 0, "equal");
1026 value->type = result_type;
1027 value->kind = axs_rvalue;
1028}
1029
1030static void
1031gen_less (struct agent_expr *ax, struct axs_value *value,
1032 struct axs_value *value1, struct axs_value *value2,
1033 struct type *result_type)
1034{
809f3be1 1035 if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ())
3b11a015
SS
1036 ax_simple (ax, aop_less_unsigned);
1037 else
1038 gen_binop (ax, value, value1, value2,
1039 aop_less_signed, aop_less_unsigned, 0, "less than");
1040 value->type = result_type;
1041 value->kind = axs_rvalue;
1042}
f7c79c41 1043
c906108c
SS
1044/* Generate code for a binary operator that doesn't do pointer magic.
1045 We set VALUE to describe the result value; we assume VALUE1 and
1046 VALUE2 describe the two operands, and that they've undergone the
1047 usual binary conversions. MAY_CARRY should be non-zero iff the
1048 result needs to be extended. NAME is the English name of the
1049 operator, used in error messages */
1050static void
fba45db2 1051gen_binop (struct agent_expr *ax, struct axs_value *value,
3e43a32a
MS
1052 struct axs_value *value1, struct axs_value *value2,
1053 enum agent_op op, enum agent_op op_unsigned,
a121b7c1 1054 int may_carry, const char *name)
c906108c
SS
1055{
1056 /* We only handle INT op INT. */
935d4856
TT
1057 struct type *type1 = strip_range_type (value1->type);
1058 if ((type1->code () != TYPE_CODE_INT)
1059 || (strip_range_type (value2->type)->code () != TYPE_CODE_INT))
3d263c1d 1060 error (_("Invalid combination of types in %s."), name);
c5aa993b 1061
935d4856 1062 ax_simple (ax, type1->is_unsigned () ? op_unsigned : op);
c906108c 1063 if (may_carry)
935d4856
TT
1064 gen_extend (ax, type1); /* catch overflow */
1065 value->type = type1;
c906108c
SS
1066 value->kind = axs_rvalue;
1067}
1068
1069
1070static void
f7c79c41
UW
1071gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1072 struct type *result_type)
c906108c 1073{
935d4856
TT
1074 struct type *type = strip_range_type (value->type);
1075 if (type->code () != TYPE_CODE_INT
1076 && type->code () != TYPE_CODE_PTR)
3d263c1d 1077 error (_("Invalid type of operand to `!'."));
c906108c 1078
c906108c 1079 ax_simple (ax, aop_log_not);
f7c79c41 1080 value->type = result_type;
c906108c
SS
1081}
1082
1083
1084static void
fba45db2 1085gen_complement (struct agent_expr *ax, struct axs_value *value)
c906108c 1086{
935d4856
TT
1087 struct type *type = strip_range_type (value->type);
1088 if (type->code () != TYPE_CODE_INT)
3d263c1d 1089 error (_("Invalid type of operand to `~'."));
c906108c 1090
c906108c 1091 ax_simple (ax, aop_bit_not);
935d4856 1092 gen_extend (ax, type);
c906108c 1093}
c5aa993b 1094\f
c906108c
SS
1095
1096
c906108c
SS
1097/* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1098
1099/* Dereference the value on the top of the stack. */
1100static void
053f8057 1101gen_deref (struct axs_value *value)
c906108c
SS
1102{
1103 /* The caller should check the type, because several operators use
1104 this, and we don't know what error message to generate. */
809f3be1 1105 if (!value->type->is_pointer_or_reference ())
f34652de 1106 internal_error (_("gen_deref: expected a pointer"));
c906108c
SS
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. */
27710edb 1113 value->type = check_typedef (value->type->target_type ());
78134374 1114 if (value->type->code () == TYPE_CODE_VOID)
b1028c8e 1115 error (_("Attempt to dereference a generic pointer."));
78134374 1116 value->kind = ((value->type->code () == TYPE_CODE_FUNC)
c906108c
SS
1117 ? axs_rvalue : axs_lvalue_memory);
1118}
1119
1120
1121/* Produce the address of the lvalue on the top of the stack. */
1122static void
053f8057 1123gen_address_of (struct axs_value *value)
c906108c
SS
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. */
78134374 1128 if (value->type->code () == TYPE_CODE_FUNC)
c906108c
SS
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:
3d263c1d 1136 error (_("Operand of `&' is an rvalue, which has no address."));
c906108c
SS
1137
1138 case axs_lvalue_register:
3d263c1d 1139 error (_("Operand of `&' is in a register, and has no address."));
c906108c
SS
1140
1141 case axs_lvalue_memory:
1142 value->kind = axs_rvalue;
1143 value->type = lookup_pointer_type (value->type);
1144 break;
1145 }
1146}
1147
c906108c
SS
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. */
1152static void
6661ad48
SM
1153gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1154 struct type *type, int start, int end)
c906108c
SS
1155{
1156 /* Note that ops[i] fetches 8 << i bits. */
1157 static enum agent_op ops[]
5b4ee69b 1158 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
c906108c
SS
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:
c5aa993b 1169 addr
c906108c 1170 For the first fetch, we duplicate the address
c5aa993b 1171 addr addr
c906108c
SS
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:
c5aa993b 1175 addr frag1
c906108c 1176 then we swap, and repeat the process:
c5aa993b
JM
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
c906108c
SS
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;
c5aa993b
JM
1190 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1191 / TARGET_CHAR_BIT)
1192 * TARGET_CHAR_BIT);
c906108c
SS
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
0e2de366 1204 /* Dereference any typedefs. */
c906108c
SS
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))
f34652de 1209 internal_error (_("gen_bitfield_ref: bitfield too wide"));
c906108c
SS
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
dda83cd7 1221 more fragments, then the address. */
c5aa993b 1222
c906108c
SS
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
c5aa993b
JM
1229 if (!last_frag)
1230 ax_simple (ax, aop_dup); /* keep a copy of the address */
1231
c906108c
SS
1232 /* Add the offset. */
1233 gen_offset (ax, offset / TARGET_CHAR_BIT);
1234
92bc6a20 1235 if (ax->tracing)
c906108c
SS
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]);
c5aa993b
JM
1243
1244 /* Shift the bits we have to their proper position.
c906108c
SS
1245 gen_left_shift will generate right shifts when the operand
1246 is negative.
1247
c5aa993b
JM
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
c906108c
SS
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
c5aa993b
JM
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. */
6661ad48 1278 if (gdbarch_byte_order (ax->gdbarch) == BFD_ENDIAN_BIG)
c906108c 1279 gen_left_shift (ax, end - (offset + op_size));
c5aa993b 1280 else
c906108c
SS
1281 gen_left_shift (ax, offset - start);
1282
c5aa993b 1283 if (!last_frag)
c906108c
SS
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. */
c6d940a9 1298 ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, end - start));
c906108c
SS
1299
1300 /* This is *not* an lvalue. Ugh. */
1301 value->kind = axs_rvalue;
1302 value->type = type;
1303}
1304
b6e7192f
SS
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
1310static void
6661ad48 1311gen_primitive_field (struct agent_expr *ax, struct axs_value *value,
b6e7192f
SS
1312 int offset, int fieldno, struct type *type)
1313{
1314 /* Is this a bitfield? */
8c329d5c 1315 if (type->field (fieldno).is_packed ())
940da03e 1316 gen_bitfield_ref (ax, value, type->field (fieldno).type (),
b6e7192f 1317 (offset * TARGET_CHAR_BIT
b610c045 1318 + type->field (fieldno).loc_bitpos ()),
b6e7192f 1319 (offset * TARGET_CHAR_BIT
b610c045 1320 + type->field (fieldno).loc_bitpos ()
3757d2d4 1321 + type->field (fieldno).bitsize ()));
b6e7192f
SS
1322 else
1323 {
1324 gen_offset (ax, offset
b610c045 1325 + type->field (fieldno).loc_bitpos () / TARGET_CHAR_BIT);
b6e7192f 1326 value->kind = axs_lvalue_memory;
940da03e 1327 value->type = type->field (fieldno).type ();
b6e7192f
SS
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
1334static int
6661ad48 1335gen_struct_ref_recursive (struct agent_expr *ax, struct axs_value *value,
a121b7c1 1336 const char *field, int offset, struct type *type)
b6e7192f
SS
1337{
1338 int i, rslt;
1339 int nbases = TYPE_N_BASECLASSES (type);
1340
f168693b 1341 type = check_typedef (type);
b6e7192f 1342
1f704f76 1343 for (i = type->num_fields () - 1; i >= nbases; i--)
b6e7192f 1344 {
33d16dd9 1345 const char *this_name = type->field (i).name ();
b6e7192f
SS
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. */
c819a338 1355 if (type->field (i).is_static ())
b6e7192f 1356 {
40f4af28 1357 gen_static_field (ax, value, type, i);
400c6af0 1358 if (value->optimized_out)
3e43a32a
MS
1359 error (_("static field `%s' has been "
1360 "optimized out, cannot use"),
400c6af0 1361 field);
b6e7192f
SS
1362 return 1;
1363 }
1364
6661ad48 1365 gen_primitive_field (ax, value, offset, i, type);
b6e7192f
SS
1366 return 1;
1367 }
1368#if 0 /* is this right? */
1369 if (this_name[0] == '\0')
f34652de 1370 internal_error (_("find_field: anonymous unions not supported"));
b6e7192f
SS
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
6661ad48 1380 rslt = gen_struct_ref_recursive (ax, value, field,
3e43a32a
MS
1381 offset + TYPE_BASECLASS_BITPOS (type, i)
1382 / TARGET_CHAR_BIT,
b6e7192f
SS
1383 basetype);
1384 if (rslt)
1385 return 1;
1386 }
1387
1388 /* Not found anywhere, flag so caller can complain. */
1389 return 0;
1390}
c906108c
SS
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. */
1397static void
6661ad48
SM
1398gen_struct_ref (struct agent_expr *ax, struct axs_value *value,
1399 const char *field, const char *operator_name,
1400 const char *operand_name)
c906108c
SS
1401{
1402 struct type *type;
b6e7192f 1403 int found;
c906108c
SS
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. */
809f3be1 1408 while (value->type->is_pointer_or_reference ())
c906108c 1409 {
f7c79c41 1410 require_rvalue (ax, value);
053f8057 1411 gen_deref (value);
c906108c 1412 }
e8860ec2 1413 type = check_typedef (value->type);
c906108c
SS
1414
1415 /* This must yield a structure or a union. */
78134374
SM
1416 if (type->code () != TYPE_CODE_STRUCT
1417 && type->code () != TYPE_CODE_UNION)
3d263c1d 1418 error (_("The left operand of `%s' is not a %s."),
c906108c
SS
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)
3d263c1d 1424 error (_("Structure does not live in memory."));
c906108c 1425
b6e7192f 1426 /* Search through fields and base classes recursively. */
6661ad48 1427 found = gen_struct_ref_recursive (ax, value, field, 0, type);
b6e7192f
SS
1428
1429 if (!found)
1430 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
7d93a1e0 1431 field, type->name ());
b6e7192f 1432}
c5aa993b 1433
b6e7192f 1434static int
6661ad48 1435gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
caaece0e 1436 const struct type *curtype, const char *name);
b6e7192f 1437static int
6661ad48 1438gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
caaece0e 1439 const struct type *curtype, const char *name);
b6e7192f
SS
1440
1441static void
40f4af28 1442gen_static_field (struct agent_expr *ax, struct axs_value *value,
b6e7192f
SS
1443 struct type *type, int fieldno)
1444{
2ad53ea1 1445 if (type->field (fieldno).loc_kind () == FIELD_LOC_KIND_PHYSADDR)
c906108c 1446 {
e06c3e11 1447 ax_const_l (ax, type->field (fieldno).loc_physaddr ());
c906108c 1448 value->kind = axs_lvalue_memory;
940da03e 1449 value->type = type->field (fieldno).type ();
400c6af0 1450 value->optimized_out = 0;
b6e7192f
SS
1451 }
1452 else
1453 {
fcbbbd90 1454 const char *phys_name = type->field (fieldno).loc_physname ();
ccf41c24
TT
1455 struct symbol *sym = lookup_symbol (phys_name, 0,
1456 SEARCH_VAR_DOMAIN, 0).symbol;
b6e7192f 1457
400c6af0
SS
1458 if (sym)
1459 {
40f4af28 1460 gen_var_ref (ax, value, sym);
400c6af0
SS
1461
1462 /* Don't error if the value was optimized out, we may be
1463 scanning all static fields and just want to pass over this
1464 and continue with the rest. */
1465 }
1466 else
1467 {
1468 /* Silently assume this was optimized out; class printing
1469 will let the user know why the data is missing. */
1470 value->optimized_out = 1;
1471 }
b6e7192f
SS
1472 }
1473}
1474
1475static int
6661ad48 1476gen_struct_elt_for_reference (struct agent_expr *ax, struct axs_value *value,
caaece0e 1477 struct type *type, const char *fieldname)
b6e7192f
SS
1478{
1479 struct type *t = type;
1480 int i;
b6e7192f 1481
78134374
SM
1482 if (t->code () != TYPE_CODE_STRUCT
1483 && t->code () != TYPE_CODE_UNION)
f34652de 1484 internal_error (_("non-aggregate type to gen_struct_elt_for_reference"));
b6e7192f 1485
1f704f76 1486 for (i = t->num_fields () - 1; i >= TYPE_N_BASECLASSES (t); i--)
b6e7192f 1487 {
33d16dd9 1488 const char *t_field_name = t->field (i).name ();
b6e7192f
SS
1489
1490 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1491 {
c819a338 1492 if (t->field (i).is_static ())
b6e7192f 1493 {
40f4af28 1494 gen_static_field (ax, value, t, i);
400c6af0 1495 if (value->optimized_out)
3e43a32a
MS
1496 error (_("static field `%s' has been "
1497 "optimized out, cannot use"),
400c6af0 1498 fieldname);
b6e7192f
SS
1499 return 1;
1500 }
8c329d5c 1501 if (t->field (i).is_packed ())
b6e7192f
SS
1502 error (_("pointers to bitfield members not allowed"));
1503
1504 /* FIXME we need a way to do "want_address" equivalent */
1505
1506 error (_("Cannot reference non-static field \"%s\""), fieldname);
1507 }
c906108c 1508 }
b6e7192f
SS
1509
1510 /* FIXME add other scoped-reference cases here */
1511
1512 /* Do a last-ditch lookup. */
6661ad48 1513 return gen_maybe_namespace_elt (ax, value, type, fieldname);
c906108c
SS
1514}
1515
b6e7192f
SS
1516/* C++: Return the member NAME of the namespace given by the type
1517 CURTYPE. */
1518
1519static int
6661ad48 1520gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
caaece0e 1521 const struct type *curtype, const char *name)
b6e7192f 1522{
6661ad48 1523 int found = gen_maybe_namespace_elt (ax, value, curtype, name);
b6e7192f
SS
1524
1525 if (!found)
1526 error (_("No symbol \"%s\" in namespace \"%s\"."),
7d93a1e0 1527 name, curtype->name ());
b6e7192f
SS
1528
1529 return found;
1530}
1531
1532/* A helper function used by value_namespace_elt and
1533 value_struct_elt_for_reference. It looks up NAME inside the
1534 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1535 is a class and NAME refers to a type in CURTYPE itself (as opposed
1536 to, say, some base class of CURTYPE). */
1537
1538static int
6661ad48 1539gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
caaece0e 1540 const struct type *curtype, const char *name)
b6e7192f 1541{
7d93a1e0 1542 const char *namespace_name = curtype->name ();
d12307c1 1543 struct block_symbol sym;
b6e7192f
SS
1544
1545 sym = cp_lookup_symbol_namespace (namespace_name, name,
1546 block_for_pc (ax->scope),
ccf41c24 1547 SEARCH_VAR_DOMAIN);
b6e7192f 1548
d12307c1 1549 if (sym.symbol == NULL)
b6e7192f
SS
1550 return 0;
1551
40f4af28 1552 gen_var_ref (ax, value, sym.symbol);
b6e7192f 1553
400c6af0
SS
1554 if (value->optimized_out)
1555 error (_("`%s' has been optimized out, cannot use"),
987012b8 1556 sym.symbol->print_name ());
400c6af0 1557
b6e7192f
SS
1558 return 1;
1559}
1560
1561
1562static int
6661ad48 1563gen_aggregate_elt_ref (struct agent_expr *ax, struct axs_value *value,
caaece0e 1564 struct type *type, const char *field)
b6e7192f 1565{
78134374 1566 switch (type->code ())
b6e7192f
SS
1567 {
1568 case TYPE_CODE_STRUCT:
1569 case TYPE_CODE_UNION:
6661ad48 1570 return gen_struct_elt_for_reference (ax, value, type, field);
b6e7192f
SS
1571 break;
1572 case TYPE_CODE_NAMESPACE:
6661ad48 1573 return gen_namespace_elt (ax, value, type, field);
b6e7192f
SS
1574 break;
1575 default:
f34652de 1576 internal_error (_("non-aggregate type in gen_aggregate_elt_ref"));
b6e7192f
SS
1577 }
1578
1579 return 0;
1580}
c906108c 1581
c906108c 1582\f
c5aa993b 1583
e2803273
TT
1584namespace expr
1585{
1586
1587void
1588operation::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;
d0c97917 1598 value->type = check_typedef (v->type ());
e2803273
TT
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
d5ab122c
TT
1608void
1609scope_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
d336c29e
TT
1621void
1622long_const_operation::do_generate_ax (struct expression *exp,
1623 struct agent_expr *ax,
1624 struct axs_value *value,
1625 struct type *cast_type)
1626{
5309ce2f
TT
1627 LONGEST val = as_longest ();
1628 gen_int_literal (ax, value, val, std::get<0> (m_storage));
d336c29e
TT
1629}
1630
0c8effa3
TT
1631void
1632var_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{
9c79936b
TT
1637 const bound_minimal_symbol &b = std::get<0> (m_storage);
1638 gen_msym_var_ref (ax, value, b.minsym, b.objfile);
0c8effa3
TT
1639
1640 if (value->type->code () == TYPE_CODE_ERROR)
1641 {
1642 if (cast_type == nullptr)
9c79936b 1643 error_unknown_type (b.minsym->linkage_name ());
0c8effa3
TT
1644 value->type = cast_type;
1645 }
1646}
1647
55bdbff8
TT
1648void
1649register_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)
f34652de 1660 internal_error (_("Register $%s not available"), name);
55bdbff8
TT
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
e6e01e16
TT
1671void
1672internalvar_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
9186293f
TT
1696void
1697ternop_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);
6f96f485 1716 ax_label (ax, if1, ax->buf.size ());
9186293f
TT
1717 std::get<2> (m_storage)->generate_ax (exp, ax, &value3);
1718 gen_usual_unary (ax, &value3);
6f96f485 1719 ax_label (ax, end, ax->buf.size ());
9186293f
TT
1720 /* This is arbitrary - what if B and C are incompatible types? */
1721 value->type = value2.type;
1722 value->kind = value2.kind;
1723}
1724
d4eff4c1
TT
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. */
1736void
1737repeat_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);
d0c97917 1758 if (v->type ()->code () != TYPE_CODE_INT)
d4eff4c1
TT
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
ae64ba58
TT
1775void
1776comma_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
85d23bda
TT
1797void
1798unop_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. */
6f96f485 1808 int start = ax->buf.size ();
85d23bda
TT
1809
1810 std::get<0> (m_storage)->generate_ax (exp, ax, value);
1811
1812 /* Throw away the code we just generated. */
6f96f485 1813 ax->buf.resize (start);
85d23bda 1814
df86565b 1815 ax_const_l (ax, value->type->length ());
85d23bda
TT
1816 value->kind = axs_rvalue;
1817 value->type = builtin_type (ax->gdbarch)->builtin_int;
1818}
1819
cbc18219 1820void
165a813a
TT
1821unop_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
6f0dabd4
AB
1830void
1831unop_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
165a813a 1849void
cbc18219
TT
1850unop_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
1866void
1867unop_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);
d0c97917 1875 struct type *type = val->type ();
cbc18219
TT
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
f6b42326
TT
1889void
1890op_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);
3c9d0506 1900 func = b->linkage_function ();
f6b42326
TT
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
40786782
TT
1914void
1915assign_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
5f48d886 1925 = gdb::checked_static_cast<internalvar_operation *> (subop);
40786782
TT
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 }
e5946e16
TT
1938 else
1939 error (_("$%s is not a trace state variable, "
1940 "may not assign to it"), name);
1941}
1942
1943void
1944assign_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
5f48d886 1954 = gdb::checked_static_cast<internalvar_operation *> (subop);
e5946e16
TT
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 }
40786782
TT
1979 else
1980 error (_("$%s is not a trace state variable, "
1981 "may not assign to it"), name);
1982}
1983
292382f4
TT
1984void
1985unop_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);
d0c97917 1993 std::get<1> (m_storage)->generate_ax (exp, ax, value, val->type ());
292382f4
TT
1994}
1995
e82a5afc
TT
1996void
1997var_value_operation::do_generate_ax (struct expression *exp,
1998 struct agent_expr *ax,
1999 struct axs_value *value,
2000 struct type *cast_type)
2001{
9e5e03df 2002 gen_var_ref (ax, value, std::get<0> (m_storage).symbol);
e82a5afc
TT
2003
2004 if (value->optimized_out)
2005 error (_("`%s' has been optimized out, cannot use"),
9e5e03df 2006 std::get<0> (m_storage).symbol->print_name ());
e82a5afc
TT
2007
2008 if (value->type->code () == TYPE_CODE_ERROR)
2009 {
2010 if (cast_type == nullptr)
9e5e03df 2011 error_unknown_type (std::get<0> (m_storage).symbol->print_name ());
e82a5afc
TT
2012 value->type = cast_type;
2013 }
2014}
2015
5019124b
TT
2016void
2017logical_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);
6f96f485 2030 ax_label (ax, if1, ax->buf.size ());
5019124b
TT
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);
6f96f485 2035 ax_label (ax, if2, ax->buf.size ());
5019124b
TT
2036 ax_const_l (ax, 1);
2037 end = ax_goto (ax, aop_goto);
6f96f485
TT
2038 ax_label (ax, go1, ax->buf.size ());
2039 ax_label (ax, go2, ax->buf.size ());
5019124b 2040 ax_const_l (ax, 0);
6f96f485 2041 ax_label (ax, end, ax->buf.size ());
5019124b
TT
2042 value->kind = axs_rvalue;
2043 value->type = builtin_type (ax->gdbarch)->builtin_int;
2044}
2045
2046void
2047logical_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);
6f96f485
TT
2064 ax_label (ax, if1, ax->buf.size ());
2065 ax_label (ax, if2, ax->buf.size ());
5019124b 2066 ax_const_l (ax, 1);
6f96f485 2067 ax_label (ax, end, ax->buf.size ());
5019124b
TT
2068 value->kind = axs_rvalue;
2069 value->type = builtin_type (ax->gdbarch)->builtin_int;
2070}
2071
e2803273
TT
2072}
2073
f61e138d
SS
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
2078static void
2079gen_expr_binop_rest (struct expression *exp,
e18c58f2 2080 enum exp_opcode op,
f61e138d
SS
2081 struct agent_expr *ax, struct axs_value *value,
2082 struct axs_value *value1, struct axs_value *value2)
2083{
6661ad48 2084 struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
3b11a015 2085
6661ad48
SM
2086 gen_usual_unary (ax, value2);
2087 gen_usual_arithmetic (ax, value1, value2);
f61e138d
SS
2088 switch (op)
2089 {
2090 case BINOP_ADD:
935d4856 2091 if (strip_range_type (value1->type)->code () == TYPE_CODE_INT
809f3be1 2092 && value2->type->is_pointer_or_reference ())
f61e138d
SS
2093 {
2094 /* Swap the values and proceed normally. */
2095 ax_simple (ax, aop_swap);
2096 gen_ptradd (ax, value, value2, value1);
2097 }
809f3be1 2098 else if (value1->type->is_pointer_or_reference ()
935d4856 2099 && strip_range_type (value2->type)->code () == TYPE_CODE_INT)
f61e138d
SS
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:
809f3be1 2106 if (value1->type->is_pointer_or_reference ()
935d4856 2107 && strip_range_type (value2->type)->code () == TYPE_CODE_INT)
f61e138d 2108 gen_ptrsub (ax,value, value1, value2);
809f3be1
TT
2109 else if (value1->type->is_pointer_or_reference ()
2110 && value2->type->is_pointer_or_reference ())
f61e138d
SS
2111 /* FIXME --- result type should be ptrdiff_t */
2112 gen_ptrdiff (ax, value, value1, value2,
6661ad48 2113 builtin_type (ax->gdbarch)->builtin_long);
f61e138d
SS
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;
948103cf
SS
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;
f61e138d 2138 case BINOP_SUBSCRIPT:
be636754
PA
2139 {
2140 struct type *type;
2141
2142 if (binop_types_user_defined_p (op, value1->type, value2->type))
2143 {
3e43a32a
MS
2144 error (_("cannot subscript requested type: "
2145 "cannot call user defined functions"));
be636754
PA
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);
78134374
SM
2153 if (type->code () != TYPE_CODE_ARRAY
2154 && type->code () != TYPE_CODE_PTR)
be636754 2155 {
7d93a1e0 2156 if (type->name ())
be636754 2157 error (_("cannot subscript something of type `%s'"),
7d93a1e0 2158 type->name ());
be636754
PA
2159 else
2160 error (_("cannot subscript requested type"));
2161 }
2162 }
2163
5d5b640e 2164 if (!is_integral_type (value2->type))
3e43a32a
MS
2165 error (_("Argument to arithmetic operation "
2166 "not a number or boolean."));
5d5b640e 2167
be636754 2168 gen_ptradd (ax, value, value1, value2);
053f8057 2169 gen_deref (value);
be636754
PA
2170 break;
2171 }
f61e138d
SS
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:
3b11a015 2188 gen_equal (ax, value, value1, value2, int_type);
f61e138d
SS
2189 break;
2190
2191 case BINOP_NOTEQUAL:
3b11a015
SS
2192 gen_equal (ax, value, value1, value2, int_type);
2193 gen_logical_not (ax, value, int_type);
f61e138d
SS
2194 break;
2195
2196 case BINOP_LESS:
3b11a015 2197 gen_less (ax, value, value1, value2, int_type);
f61e138d
SS
2198 break;
2199
2200 case BINOP_GTR:
2201 ax_simple (ax, aop_swap);
3b11a015 2202 gen_less (ax, value, value1, value2, int_type);
f61e138d
SS
2203 break;
2204
2205 case BINOP_LEQ:
2206 ax_simple (ax, aop_swap);
3b11a015
SS
2207 gen_less (ax, value, value1, value2, int_type);
2208 gen_logical_not (ax, value, int_type);
f61e138d
SS
2209 break;
2210
2211 case BINOP_GEQ:
3b11a015
SS
2212 gen_less (ax, value, value1, value2, int_type);
2213 gen_logical_not (ax, value, int_type);
f61e138d
SS
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. */
f34652de 2219 internal_error (_("gen_expr: op case sets don't match"));
f61e138d
SS
2220 }
2221}
e18c58f2 2222
75f9892d
TT
2223/* A helper function that emits a binop based on two operations. */
2224
2225void
2226gen_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
2242void
2243gen_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. */
f34652de 2258 internal_error (_("gen_expr: unhandled struct case"));
75f9892d 2259}
9307d17b
TT
2260
2261/* A helper function that emits a unary operation. */
2262
2263void
2264gen_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
876469ff
TT
2302 case UNOP_IND:
2303 lhs->generate_ax (exp, ax, value);
2304 gen_usual_unary (ax, value);
809f3be1 2305 if (!value->type->is_pointer_or_reference ())
876469ff
TT
2306 error (_("Argument of unary `*' is not a pointer."));
2307 gen_deref (value);
2308 break;
2309
14aff815
TT
2310 case UNOP_ADDR:
2311 lhs->generate_ax (exp, ax, value);
2312 gen_address_of (value);
2313 break;
2314
9307d17b
TT
2315 default:
2316 gdb_assert_not_reached ("invalid case in gen_expr_unop");
2317 }
2318}
2319
c906108c 2320\f
c5aa993b 2321
0936ad1d
SS
2322/* Given a single variable and a scope, generate bytecodes to trace
2323 its value. This is for use in situations where we have only a
2324 variable's name, and no parsed expression; for instance, when the
2325 name comes from a list of local variables of a function. */
2326
833177a4 2327agent_expr_up
400c6af0 2328gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
92bc6a20 2329 struct symbol *var, int trace_string)
0936ad1d 2330{
833177a4 2331 agent_expr_up ax (new agent_expr (gdbarch, scope));
0936ad1d
SS
2332 struct axs_value value;
2333
da25448d 2334 ax->tracing = true;
92bc6a20 2335 ax->trace_string = trace_string;
40f4af28 2336 gen_var_ref (ax.get (), &value, var);
400c6af0
SS
2337
2338 /* If there is no actual variable to trace, flag it by returning
2339 an empty agent expression. */
2340 if (value.optimized_out)
833177a4 2341 return agent_expr_up ();
0936ad1d
SS
2342
2343 /* Make sure we record the final object, and get rid of it. */
40f4af28 2344 gen_traced_pop (ax.get (), &value);
0936ad1d
SS
2345
2346 /* Oh, and terminate. */
833177a4 2347 ax_simple (ax.get (), aop_end);
0936ad1d 2348
0936ad1d
SS
2349 return ax;
2350}
c5aa993b 2351
c906108c
SS
2352/* Generating bytecode from GDB expressions: driver */
2353
c906108c
SS
2354/* Given a GDB expression EXPR, return bytecode to trace its value.
2355 The result will use the `trace' and `trace_quick' bytecodes to
2356 record the value of all memory touched by the expression. The
2357 caller can then use the ax_reqs function to discover which
2358 registers it relies upon. */
833177a4
PA
2359
2360agent_expr_up
92bc6a20
TT
2361gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
2362 int trace_string)
c906108c 2363{
833177a4 2364 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
c906108c
SS
2365 struct axs_value value;
2366
da25448d 2367 ax->tracing = true;
92bc6a20 2368 ax->trace_string = trace_string;
35c9c7ba 2369 value.optimized_out = 0;
1eaebe02 2370 expr->op->generate_ax (expr, ax.get (), &value);
c906108c
SS
2371
2372 /* Make sure we record the final object, and get rid of it. */
40f4af28 2373 gen_traced_pop (ax.get (), &value);
c906108c
SS
2374
2375 /* Oh, and terminate. */
833177a4 2376 ax_simple (ax.get (), aop_end);
c906108c 2377
c906108c
SS
2378 return ax;
2379}
c906108c 2380
782b2b07
SS
2381/* Given a GDB expression EXPR, return a bytecode sequence that will
2382 evaluate and return a result. The bytecodes will do a direct
2383 evaluation, using the current data on the target, rather than
2384 recording blocks of memory and registers for later use, as
2385 gen_trace_for_expr does. The generated bytecode sequence leaves
2386 the result of expression evaluation on the top of the stack. */
2387
833177a4 2388agent_expr_up
782b2b07
SS
2389gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2390{
833177a4 2391 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
782b2b07
SS
2392 struct axs_value value;
2393
da25448d 2394 ax->tracing = false;
35c9c7ba 2395 value.optimized_out = 0;
1eaebe02 2396 expr->op->generate_ax (expr, ax.get (), &value);
782b2b07 2397
833177a4 2398 require_rvalue (ax.get (), &value);
35c9c7ba 2399
782b2b07 2400 /* Oh, and terminate. */
833177a4 2401 ax_simple (ax.get (), aop_end);
782b2b07 2402
782b2b07
SS
2403 return ax;
2404}
2405
833177a4 2406agent_expr_up
92bc6a20
TT
2407gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
2408 int trace_string)
6710bf39 2409{
833177a4 2410 agent_expr_up ax (new agent_expr (gdbarch, scope));
6710bf39
SS
2411 struct axs_value value;
2412
da25448d 2413 ax->tracing = true;
92bc6a20 2414 ax->trace_string = trace_string;
6710bf39 2415
833177a4 2416 gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope);
6710bf39
SS
2417
2418 /* Make sure we record the final object, and get rid of it. */
40f4af28 2419 gen_traced_pop (ax.get (), &value);
6710bf39
SS
2420
2421 /* Oh, and terminate. */
833177a4 2422 ax_simple (ax.get (), aop_end);
6710bf39 2423
6710bf39
SS
2424 return ax;
2425}
2426
d3ce09f5
SS
2427/* Given a collection of printf-style arguments, generate code to
2428 evaluate the arguments and pass everything to a special
2429 bytecode. */
2430
833177a4 2431agent_expr_up
d3ce09f5
SS
2432gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2433 CORE_ADDR function, LONGEST channel,
741d92cf 2434 const char *format, int fmtlen,
d3ce09f5
SS
2435 int nargs, struct expression **exprs)
2436{
833177a4 2437 agent_expr_up ax (new agent_expr (gdbarch, scope));
d3ce09f5 2438 struct axs_value value;
0e43993a 2439 int tem;
d3ce09f5 2440
92bc6a20 2441 /* We're computing values, not doing side effects. */
da25448d 2442 ax->tracing = false;
92bc6a20 2443
d3ce09f5
SS
2444 /* Evaluate and push the args on the stack in reverse order,
2445 for simplicity of collecting them on the target side. */
2446 for (tem = nargs - 1; tem >= 0; --tem)
2447 {
d3ce09f5 2448 value.optimized_out = 0;
1eaebe02 2449 exprs[tem]->op->generate_ax (exprs[tem], ax.get (), &value);
833177a4 2450 require_rvalue (ax.get (), &value);
d3ce09f5
SS
2451 }
2452
2453 /* Push function and channel. */
833177a4
PA
2454 ax_const_l (ax.get (), channel);
2455 ax_const_l (ax.get (), function);
d3ce09f5
SS
2456
2457 /* Issue the printf bytecode proper. */
833177a4
PA
2458 ax_simple (ax.get (), aop_printf);
2459 ax_raw_byte (ax.get (), nargs);
2460 ax_string (ax.get (), format, fmtlen);
d3ce09f5
SS
2461
2462 /* And terminate. */
833177a4 2463 ax_simple (ax.get (), aop_end);
d3ce09f5
SS
2464
2465 return ax;
2466}
2467
c906108c 2468static void
6f937416 2469agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
c906108c 2470{
bbc13ae3 2471 const char *arg;
92bc6a20 2472 int trace_string = 0;
c906108c 2473
34b536a8
HZ
2474 if (!eval)
2475 {
34b536a8 2476 if (*exp == '/')
dda83cd7 2477 exp = decode_agent_options (exp, &trace_string);
34b536a8 2478 }
3065dfb6 2479
833177a4
PA
2480 agent_expr_up agent;
2481
bbc13ae3
KS
2482 arg = exp;
2483 if (!eval && strcmp (arg, "$_ret") == 0)
6710bf39 2484 {
036e657b
JB
2485 agent = gen_trace_for_return_address (pc, get_current_arch (),
2486 trace_string);
6710bf39
SS
2487 }
2488 else
2489 {
4d01a485 2490 expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
833177a4 2491
34b536a8 2492 if (eval)
92bc6a20
TT
2493 {
2494 gdb_assert (trace_string == 0);
036e657b 2495 agent = gen_eval_for_expr (pc, expr.get ());
92bc6a20 2496 }
34b536a8 2497 else
036e657b 2498 agent = gen_trace_for_expr (pc, expr.get (), trace_string);
6710bf39
SS
2499 }
2500
833177a4
PA
2501 ax_reqs (agent.get ());
2502 ax_print (gdb_stdout, agent.get ());
085dd6e6
JM
2503
2504 /* It would be nice to call ax_reqs here to gather some general info
2505 about the expression, and then print out the result. */
c906108c 2506
c906108c
SS
2507 dont_repeat ();
2508}
782b2b07 2509
782b2b07 2510static void
c32ce0dc 2511maint_agent_command_1 (const char *exp, int eval)
782b2b07 2512{
782b2b07
SS
2513 /* We don't deal with overlay debugging at the moment. We need to
2514 think more carefully about this. If you copy this code into
2515 another command, change the error message; the user shouldn't
2516 have to know anything about agent expressions. */
2517 if (overlay_debugging)
2518 error (_("GDB can't do agent expression translation with overlays."));
2519
2520 if (exp == 0)
2521 error_no_arg (_("expression to translate"));
2522
34b536a8
HZ
2523 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2524 {
2525 struct linespec_result canonical;
34b536a8 2526
264f9890
PA
2527 location_spec_up locspec
2528 = new_linespec_location_spec (&exp, symbol_name_match_type::WILD);
2529 decode_line_full (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
cafb3438 2530 NULL, 0, &canonical,
34b536a8 2531 NULL, NULL);
34b536a8
HZ
2532 exp = skip_spaces (exp);
2533 if (exp[0] == ',')
dda83cd7 2534 {
34b536a8
HZ
2535 exp++;
2536 exp = skip_spaces (exp);
2537 }
6c5b2ebe
PA
2538 for (const auto &lsal : canonical.lsals)
2539 for (const auto &sal : lsal.sals)
2540 agent_eval_command_one (exp, eval, sal.pc);
34b536a8
HZ
2541 }
2542 else
2543 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
782b2b07 2544
782b2b07
SS
2545 dont_repeat ();
2546}
34b536a8
HZ
2547
2548static void
c32ce0dc 2549maint_agent_command (const char *exp, int from_tty)
34b536a8 2550{
c32ce0dc 2551 maint_agent_command_1 (exp, 0);
34b536a8
HZ
2552}
2553
2554/* Parse the given expression, compile it into an agent expression
2555 that does direct evaluation, and display the resulting
2556 expression. */
2557
2558static void
c32ce0dc 2559maint_agent_eval_command (const char *exp, int from_tty)
34b536a8 2560{
c32ce0dc 2561 maint_agent_command_1 (exp, 1);
34b536a8
HZ
2562}
2563
d3ce09f5
SS
2564/* Parse the given expression, compile it into an agent expression
2565 that does a printf, and display the resulting expression. */
2566
2567static void
4fd41b24 2568maint_agent_printf_command (const char *cmdrest, int from_tty)
d3ce09f5 2569{
bd2b40ac 2570 frame_info_ptr fi = get_current_frame (); /* need current scope */
bbc13ae3 2571 const char *format_start, *format_end;
d3ce09f5
SS
2572
2573 /* We don't deal with overlay debugging at the moment. We need to
2574 think more carefully about this. If you copy this code into
2575 another command, change the error message; the user shouldn't
2576 have to know anything about agent expressions. */
2577 if (overlay_debugging)
2578 error (_("GDB can't do agent expression translation with overlays."));
2579
4fd41b24 2580 if (cmdrest == 0)
d3ce09f5
SS
2581 error_no_arg (_("expression to translate"));
2582
f1735a53 2583 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2584
2585 if (*cmdrest++ != '"')
2586 error (_("Must start with a format string."));
2587
2588 format_start = cmdrest;
2589
8e481c3b 2590 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2591
2592 format_end = cmdrest;
2593
2594 if (*cmdrest++ != '"')
2595 error (_("Bad format string, non-terminated '\"'."));
2596
f1735a53 2597 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2598
2599 if (*cmdrest != ',' && *cmdrest != 0)
2600 error (_("Invalid argument syntax"));
2601
2602 if (*cmdrest == ',')
2603 cmdrest++;
f1735a53 2604 cmdrest = skip_spaces (cmdrest);
d3ce09f5 2605
8e481c3b 2606 std::vector<struct expression *> argvec;
d3ce09f5
SS
2607 while (*cmdrest != '\0')
2608 {
bbc13ae3 2609 const char *cmd1;
d3ce09f5
SS
2610
2611 cmd1 = cmdrest;
b8c03634
TT
2612 expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0,
2613 PARSER_COMMA_TERMINATES);
8e481c3b 2614 argvec.push_back (expr.release ());
d3ce09f5
SS
2615 cmdrest = cmd1;
2616 if (*cmdrest == ',')
2617 ++cmdrest;
2618 /* else complain? */
2619 }
2620
2621
833177a4
PA
2622 agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (),
2623 0, 0,
2624 format_start, format_end - format_start,
8e481c3b 2625 argvec.size (), argvec.data ());
833177a4
PA
2626 ax_reqs (agent.get ());
2627 ax_print (gdb_stdout, agent.get ());
d3ce09f5
SS
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
d3ce09f5
SS
2632 dont_repeat ();
2633}
c5aa993b 2634
c906108c
SS
2635/* Initialization code. */
2636
6c265988 2637void _initialize_ax_gdb ();
c906108c 2638void
6c265988 2639_initialize_ax_gdb ()
c906108c 2640{
c32ce0dc 2641 add_cmd ("agent", class_maintenance, maint_agent_command,
34b536a8
HZ
2642 _("\
2643Translate an expression into remote agent bytecode for tracing.\n\
48c5e7e2 2644Usage: maint agent [-at LOCATION,] EXPRESSION\n\
34b536a8
HZ
2645If -at is given, generate remote agent bytecode for this location.\n\
2646If not, generate remote agent bytecode for current frame pc address."),
782b2b07
SS
2647 &maintenancelist);
2648
c32ce0dc 2649 add_cmd ("agent-eval", class_maintenance, maint_agent_eval_command,
34b536a8
HZ
2650 _("\
2651Translate an expression into remote agent bytecode for evaluation.\n\
48c5e7e2 2652Usage: maint agent-eval [-at LOCATION,] EXPRESSION\n\
34b536a8
HZ
2653If -at is given, generate remote agent bytecode for this location.\n\
2654If not, generate remote agent bytecode for current frame pc address."),
c906108c 2655 &maintenancelist);
d3ce09f5
SS
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);
c906108c 2661}