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