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