]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ax-gdb.c
Remove now-unused C evaluator code
[thirdparty/binutils-gdb.git] / gdb / ax-gdb.c
CommitLineData
1bac305b
AC
1/* GDB-specific functions for operating on agent expressions.
2
3666a048 3 Copyright (C) 1998-2021 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:
73 union exp_element ** --- pointer into expression
74 struct agent_expr * --- agent expression buffer to generate code into
75 struct axs_value * --- describes value left on top of stack */
c5aa993b 76
a14ed312
KB
77static struct value *const_var_ref (struct symbol *var);
78static struct value *const_expr (union exp_element **pc);
79static struct value *maybe_const_expr (union exp_element **pc);
80
40f4af28 81static void gen_traced_pop (struct agent_expr *, struct axs_value *);
a14ed312
KB
82
83static void gen_sign_extend (struct agent_expr *, struct type *);
84static void gen_extend (struct agent_expr *, struct type *);
85static void gen_fetch (struct agent_expr *, struct type *);
86static void gen_left_shift (struct agent_expr *, int);
87
88
40f4af28
SM
89static void gen_frame_args_address (struct agent_expr *);
90static void gen_frame_locals_address (struct agent_expr *);
a14ed312
KB
91static void gen_offset (struct agent_expr *ax, int offset);
92static void gen_sym_offset (struct agent_expr *, struct symbol *);
40f4af28
SM
93static void gen_var_ref (struct agent_expr *ax, struct axs_value *value,
94 struct symbol *var);
a14ed312
KB
95
96
97static void gen_int_literal (struct agent_expr *ax,
98 struct axs_value *value,
99 LONGEST k, struct type *type);
100
6661ad48 101static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
a14ed312
KB
102static int type_wider_than (struct type *type1, struct type *type2);
103static struct type *max_type (struct type *type1, struct type *type2);
104static void gen_conversion (struct agent_expr *ax,
105 struct type *from, struct type *to);
106static int is_nontrivial_conversion (struct type *from, struct type *to);
6661ad48 107static void gen_usual_arithmetic (struct agent_expr *ax,
a14ed312
KB
108 struct axs_value *value1,
109 struct axs_value *value2);
6661ad48 110static void gen_integral_promotions (struct agent_expr *ax,
a14ed312
KB
111 struct axs_value *value);
112static void gen_cast (struct agent_expr *ax,
113 struct axs_value *value, struct type *type);
114static void gen_scale (struct agent_expr *ax,
115 enum agent_op op, struct type *type);
f7c79c41
UW
116static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
117 struct axs_value *value1, struct axs_value *value2);
118static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
119 struct axs_value *value1, struct axs_value *value2);
120static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
121 struct axs_value *value1, struct axs_value *value2,
122 struct type *result_type);
a14ed312
KB
123static void gen_binop (struct agent_expr *ax,
124 struct axs_value *value,
125 struct axs_value *value1,
126 struct axs_value *value2,
127 enum agent_op op,
a121b7c1
PA
128 enum agent_op op_unsigned, int may_carry,
129 const char *name);
f7c79c41
UW
130static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
131 struct type *result_type);
a14ed312 132static void gen_complement (struct agent_expr *ax, struct axs_value *value);
053f8057
SM
133static void gen_deref (struct axs_value *);
134static void gen_address_of (struct axs_value *);
6661ad48 135static void gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
a14ed312 136 struct type *type, int start, int end);
6661ad48 137static void gen_primitive_field (struct agent_expr *ax,
b6e7192f
SS
138 struct axs_value *value,
139 int offset, int fieldno, struct type *type);
6661ad48 140static int gen_struct_ref_recursive (struct agent_expr *ax,
b6e7192f 141 struct axs_value *value,
a121b7c1 142 const char *field, int offset,
b6e7192f 143 struct type *type);
6661ad48 144static void gen_struct_ref (struct agent_expr *ax,
a14ed312 145 struct axs_value *value,
a121b7c1
PA
146 const char *field,
147 const char *operator_name,
148 const char *operand_name);
40f4af28 149static void gen_static_field (struct agent_expr *ax, struct axs_value *value,
b6e7192f 150 struct type *type, int fieldno);
f7c79c41 151static void gen_repeat (struct expression *exp, union exp_element **pc,
a14ed312 152 struct agent_expr *ax, struct axs_value *value);
f7c79c41
UW
153static void gen_sizeof (struct expression *exp, union exp_element **pc,
154 struct agent_expr *ax, struct axs_value *value,
155 struct type *size_type);
f61e138d
SS
156static void gen_expr_binop_rest (struct expression *exp,
157 enum exp_opcode op, union exp_element **pc,
158 struct agent_expr *ax,
159 struct axs_value *value,
160 struct axs_value *value1,
161 struct axs_value *value2);
75f9892d
TT
162static void gen_expr_binop_rest (struct expression *exp,
163 enum exp_opcode op,
164 struct agent_expr *ax,
165 struct axs_value *value,
166 struct axs_value *value1,
167 struct axs_value *value2);
c906108c 168\f
c5aa993b 169
c906108c
SS
170/* Detecting constant expressions. */
171
172/* If the variable reference at *PC is a constant, return its value.
173 Otherwise, return zero.
174
175 Hey, Wally! How can a variable reference be a constant?
176
177 Well, Beav, this function really handles the OP_VAR_VALUE operator,
178 not specifically variable references. GDB uses OP_VAR_VALUE to
179 refer to any kind of symbolic reference: function names, enum
180 elements, and goto labels are all handled through the OP_VAR_VALUE
181 operator, even though they're constants. It makes sense given the
182 situation.
183
184 Gee, Wally, don'cha wonder sometimes if data representations that
185 subvert commonly accepted definitions of terms in favor of heavily
186 context-specific interpretations are really just a tool of the
187 programming hegemony to preserve their power and exclude the
188 proletariat? */
189
190static struct value *
fba45db2 191const_var_ref (struct symbol *var)
c906108c
SS
192{
193 struct type *type = SYMBOL_TYPE (var);
194
195 switch (SYMBOL_CLASS (var))
196 {
197 case LOC_CONST:
198 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
199
200 case LOC_LABEL:
4478b372 201 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
c906108c
SS
202
203 default:
204 return 0;
205 }
206}
207
208
209/* If the expression starting at *PC has a constant value, return it.
210 Otherwise, return zero. If we return a value, then *PC will be
211 advanced to the end of it. If we return zero, *PC could be
212 anywhere. */
213static struct value *
fba45db2 214const_expr (union exp_element **pc)
c906108c
SS
215{
216 enum exp_opcode op = (*pc)->opcode;
217 struct value *v1;
218
219 switch (op)
220 {
221 case OP_LONG:
222 {
223 struct type *type = (*pc)[1].type;
224 LONGEST k = (*pc)[2].longconst;
5b4ee69b 225
c906108c
SS
226 (*pc) += 4;
227 return value_from_longest (type, k);
228 }
229
230 case OP_VAR_VALUE:
231 {
232 struct value *v = const_var_ref ((*pc)[2].symbol);
5b4ee69b 233
c906108c
SS
234 (*pc) += 4;
235 return v;
236 }
237
c5aa993b 238 /* We could add more operators in here. */
c906108c
SS
239
240 case UNOP_NEG:
241 (*pc)++;
242 v1 = const_expr (pc);
243 if (v1)
244 return value_neg (v1);
245 else
246 return 0;
247
248 default:
249 return 0;
250 }
251}
252
253
254/* Like const_expr, but guarantee also that *PC is undisturbed if the
255 expression is not constant. */
256static struct value *
fba45db2 257maybe_const_expr (union exp_element **pc)
c906108c
SS
258{
259 union exp_element *tentative_pc = *pc;
260 struct value *v = const_expr (&tentative_pc);
261
262 /* If we got a value, then update the real PC. */
263 if (v)
264 *pc = tentative_pc;
c5aa993b 265
c906108c
SS
266 return v;
267}
c906108c 268\f
c5aa993b 269
c906108c
SS
270/* Generating bytecode from GDB expressions: general assumptions */
271
272/* Here are a few general assumptions made throughout the code; if you
273 want to make a change that contradicts one of these, then you'd
274 better scan things pretty thoroughly.
275
276 - We assume that all values occupy one stack element. For example,
c5aa993b
JM
277 sometimes we'll swap to get at the left argument to a binary
278 operator. If we decide that void values should occupy no stack
279 elements, or that synthetic arrays (whose size is determined at
280 run time, created by the `@' operator) should occupy two stack
281 elements (address and length), then this will cause trouble.
c906108c
SS
282
283 - We assume the stack elements are infinitely wide, and that we
c5aa993b
JM
284 don't have to worry what happens if the user requests an
285 operation that is wider than the actual interpreter's stack.
286 That is, it's up to the interpreter to handle directly all the
287 integer widths the user has access to. (Woe betide the language
288 with bignums!)
c906108c
SS
289
290 - We don't support side effects. Thus, we don't have to worry about
c5aa993b 291 GCC's generalized lvalues, function calls, etc.
c906108c
SS
292
293 - We don't support floating point. Many places where we switch on
c5aa993b
JM
294 some type don't bother to include cases for floating point; there
295 may be even more subtle ways this assumption exists. For
296 example, the arguments to % must be integers.
c906108c
SS
297
298 - We assume all subexpressions have a static, unchanging type. If
c5aa993b
JM
299 we tried to support convenience variables, this would be a
300 problem.
c906108c
SS
301
302 - All values on the stack should always be fully zero- or
c5aa993b
JM
303 sign-extended.
304
305 (I wasn't sure whether to choose this or its opposite --- that
306 only addresses are assumed extended --- but it turns out that
307 neither convention completely eliminates spurious extend
308 operations (if everything is always extended, then you have to
309 extend after add, because it could overflow; if nothing is
310 extended, then you end up producing extends whenever you change
311 sizes), and this is simpler.) */
c906108c 312\f
c5aa993b 313
400c6af0
SS
314/* Scan for all static fields in the given class, including any base
315 classes, and generate tracing bytecodes for each. */
316
317static void
40f4af28 318gen_trace_static_fields (struct agent_expr *ax,
400c6af0
SS
319 struct type *type)
320{
321 int i, nbases = TYPE_N_BASECLASSES (type);
322 struct axs_value value;
323
f168693b 324 type = check_typedef (type);
400c6af0 325
1f704f76 326 for (i = type->num_fields () - 1; i >= nbases; i--)
400c6af0 327 {
ceacbf6e 328 if (field_is_static (&type->field (i)))
400c6af0 329 {
40f4af28 330 gen_static_field (ax, &value, type, i);
400c6af0
SS
331 if (value.optimized_out)
332 continue;
333 switch (value.kind)
334 {
335 case axs_lvalue_memory:
336 {
dda83cd7
SM
337 /* Initialize the TYPE_LENGTH if it is a typedef. */
338 check_typedef (value.type);
744a8059 339 ax_const_l (ax, TYPE_LENGTH (value.type));
400c6af0
SS
340 ax_simple (ax, aop_trace);
341 }
342 break;
343
344 case axs_lvalue_register:
35c9c7ba
SS
345 /* We don't actually need the register's value to be pushed,
346 just note that we need it to be collected. */
347 ax_reg_mask (ax, value.u.reg);
400c6af0
SS
348
349 default:
350 break;
351 }
352 }
353 }
354
355 /* Now scan through base classes recursively. */
356 for (i = 0; i < nbases; i++)
357 {
358 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
359
40f4af28 360 gen_trace_static_fields (ax, basetype);
400c6af0
SS
361 }
362}
363
c906108c
SS
364/* Trace the lvalue on the stack, if it needs it. In either case, pop
365 the value. Useful on the left side of a comma, and at the end of
366 an expression being used for tracing. */
367static void
40f4af28 368gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
c906108c 369{
3065dfb6 370 int string_trace = 0;
92bc6a20 371 if (ax->trace_string
78134374 372 && value->type->code () == TYPE_CODE_PTR
3065dfb6
SS
373 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
374 's'))
375 string_trace = 1;
376
92bc6a20 377 if (ax->tracing)
c906108c
SS
378 switch (value->kind)
379 {
380 case axs_rvalue:
3065dfb6
SS
381 if (string_trace)
382 {
92bc6a20 383 ax_const_l (ax, ax->trace_string);
3065dfb6
SS
384 ax_simple (ax, aop_tracenz);
385 }
386 else
387 /* We don't trace rvalues, just the lvalues necessary to
388 produce them. So just dispose of this value. */
389 ax_simple (ax, aop_pop);
c906108c
SS
390 break;
391
392 case axs_lvalue_memory:
393 {
744a8059
SP
394 /* Initialize the TYPE_LENGTH if it is a typedef. */
395 check_typedef (value->type);
396
3065dfb6
SS
397 if (string_trace)
398 {
f906b857 399 gen_fetch (ax, value->type);
92bc6a20 400 ax_const_l (ax, ax->trace_string);
3065dfb6
SS
401 ax_simple (ax, aop_tracenz);
402 }
f906b857
MK
403 else
404 {
405 /* There's no point in trying to use a trace_quick bytecode
dda83cd7
SM
406 here, since "trace_quick SIZE pop" is three bytes, whereas
407 "const8 SIZE trace" is also three bytes, does the same
408 thing, and the simplest code which generates that will also
409 work correctly for objects with large sizes. */
f906b857
MK
410 ax_const_l (ax, TYPE_LENGTH (value->type));
411 ax_simple (ax, aop_trace);
412 }
c906108c 413 }
c5aa993b 414 break;
c906108c
SS
415
416 case axs_lvalue_register:
35c9c7ba
SS
417 /* We don't actually need the register's value to be on the
418 stack, and the target will get heartburn if the register is
419 larger than will fit in a stack, so just mark it for
420 collection and be done with it. */
421 ax_reg_mask (ax, value->u.reg);
3065dfb6
SS
422
423 /* But if the register points to a string, assume the value
424 will fit on the stack and push it anyway. */
425 if (string_trace)
426 {
427 ax_reg (ax, value->u.reg);
92bc6a20 428 ax_const_l (ax, ax->trace_string);
3065dfb6
SS
429 ax_simple (ax, aop_tracenz);
430 }
c906108c
SS
431 break;
432 }
433 else
434 /* If we're not tracing, just pop the value. */
435 ax_simple (ax, aop_pop);
400c6af0
SS
436
437 /* To trace C++ classes with static fields stored elsewhere. */
92bc6a20 438 if (ax->tracing
78134374
SM
439 && (value->type->code () == TYPE_CODE_STRUCT
440 || value->type->code () == TYPE_CODE_UNION))
40f4af28 441 gen_trace_static_fields (ax, value->type);
c906108c 442}
c5aa993b 443\f
c906108c
SS
444
445
c906108c
SS
446/* Generating bytecode from GDB expressions: helper functions */
447
448/* Assume that the lower bits of the top of the stack is a value of
449 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
450static void
fba45db2 451gen_sign_extend (struct agent_expr *ax, struct type *type)
c906108c
SS
452{
453 /* Do we need to sign-extend this? */
c6d940a9 454 if (!type->is_unsigned ())
0004e5a2 455 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
c906108c
SS
456}
457
458
459/* Assume the lower bits of the top of the stack hold a value of type
460 TYPE, and the upper bits are garbage. Sign-extend or truncate as
461 needed. */
462static void
fba45db2 463gen_extend (struct agent_expr *ax, struct type *type)
c906108c 464{
0004e5a2 465 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
5b4ee69b 466
c906108c 467 /* I just had to. */
c6d940a9 468 ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, bits));
c906108c
SS
469}
470
471
472/* Assume that the top of the stack contains a value of type "pointer
473 to TYPE"; generate code to fetch its value. Note that TYPE is the
474 target type, not the pointer type. */
475static void
fba45db2 476gen_fetch (struct agent_expr *ax, struct type *type)
c906108c 477{
92bc6a20 478 if (ax->tracing)
c906108c
SS
479 {
480 /* Record the area of memory we're about to fetch. */
481 ax_trace_quick (ax, TYPE_LENGTH (type));
482 }
483
78134374 484 if (type->code () == TYPE_CODE_RANGE)
af381b8c
JB
485 type = TYPE_TARGET_TYPE (type);
486
78134374 487 switch (type->code ())
c906108c
SS
488 {
489 case TYPE_CODE_PTR:
b97aedf3 490 case TYPE_CODE_REF:
aa006118 491 case TYPE_CODE_RVALUE_REF:
c906108c
SS
492 case TYPE_CODE_ENUM:
493 case TYPE_CODE_INT:
494 case TYPE_CODE_CHAR:
3b11a015 495 case TYPE_CODE_BOOL:
c906108c 496 /* It's a scalar value, so we know how to dereference it. How
dda83cd7 497 many bytes long is it? */
0004e5a2 498 switch (TYPE_LENGTH (type))
c906108c 499 {
c5aa993b
JM
500 case 8 / TARGET_CHAR_BIT:
501 ax_simple (ax, aop_ref8);
502 break;
503 case 16 / TARGET_CHAR_BIT:
504 ax_simple (ax, aop_ref16);
505 break;
506 case 32 / TARGET_CHAR_BIT:
507 ax_simple (ax, aop_ref32);
508 break;
509 case 64 / TARGET_CHAR_BIT:
510 ax_simple (ax, aop_ref64);
511 break;
c906108c
SS
512
513 /* Either our caller shouldn't have asked us to dereference
514 that pointer (other code's fault), or we're not
515 implementing something we should be (this code's fault).
516 In any case, it's a bug the user shouldn't see. */
517 default:
8e65ff28 518 internal_error (__FILE__, __LINE__,
3d263c1d 519 _("gen_fetch: strange size"));
c906108c
SS
520 }
521
522 gen_sign_extend (ax, type);
523 break;
524
525 default:
52323be9
LM
526 /* Our caller requested us to dereference a pointer from an unsupported
527 type. Error out and give callers a chance to handle the failure
528 gracefully. */
529 error (_("gen_fetch: Unsupported type code `%s'."),
7d93a1e0 530 type->name ());
c906108c
SS
531 }
532}
533
534
535/* Generate code to left shift the top of the stack by DISTANCE bits, or
536 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
537 unsigned (logical) right shifts. */
538static void
fba45db2 539gen_left_shift (struct agent_expr *ax, int distance)
c906108c
SS
540{
541 if (distance > 0)
542 {
543 ax_const_l (ax, distance);
544 ax_simple (ax, aop_lsh);
545 }
546 else if (distance < 0)
547 {
548 ax_const_l (ax, -distance);
549 ax_simple (ax, aop_rsh_unsigned);
550 }
551}
c5aa993b 552\f
c906108c
SS
553
554
c906108c
SS
555/* Generating bytecode from GDB expressions: symbol references */
556
557/* Generate code to push the base address of the argument portion of
558 the top stack frame. */
559static void
40f4af28 560gen_frame_args_address (struct agent_expr *ax)
c906108c 561{
39d4ef09
AC
562 int frame_reg;
563 LONGEST frame_offset;
c906108c 564
40f4af28 565 gdbarch_virtual_frame_pointer (ax->gdbarch,
c7bb205c 566 ax->scope, &frame_reg, &frame_offset);
c5aa993b 567 ax_reg (ax, frame_reg);
c906108c
SS
568 gen_offset (ax, frame_offset);
569}
570
571
572/* Generate code to push the base address of the locals portion of the
573 top stack frame. */
574static void
40f4af28 575gen_frame_locals_address (struct agent_expr *ax)
c906108c 576{
39d4ef09
AC
577 int frame_reg;
578 LONGEST frame_offset;
c906108c 579
40f4af28 580 gdbarch_virtual_frame_pointer (ax->gdbarch,
c7bb205c 581 ax->scope, &frame_reg, &frame_offset);
c5aa993b 582 ax_reg (ax, frame_reg);
c906108c
SS
583 gen_offset (ax, frame_offset);
584}
585
586
587/* Generate code to add OFFSET to the top of the stack. Try to
588 generate short and readable code. We use this for getting to
589 variables on the stack, and structure members. If we were
590 programming in ML, it would be clearer why these are the same
591 thing. */
592static void
fba45db2 593gen_offset (struct agent_expr *ax, int offset)
c906108c
SS
594{
595 /* It would suffice to simply push the offset and add it, but this
596 makes it easier to read positive and negative offsets in the
597 bytecode. */
598 if (offset > 0)
599 {
600 ax_const_l (ax, offset);
601 ax_simple (ax, aop_add);
602 }
603 else if (offset < 0)
604 {
605 ax_const_l (ax, -offset);
606 ax_simple (ax, aop_sub);
607 }
608}
609
610
611/* In many cases, a symbol's value is the offset from some other
612 address (stack frame, base register, etc.) Generate code to add
613 VAR's value to the top of the stack. */
614static void
fba45db2 615gen_sym_offset (struct agent_expr *ax, struct symbol *var)
c906108c
SS
616{
617 gen_offset (ax, SYMBOL_VALUE (var));
618}
619
620
621/* Generate code for a variable reference to AX. The variable is the
622 symbol VAR. Set VALUE to describe the result. */
623
624static void
40f4af28 625gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
c906108c 626{
0e2de366 627 /* Dereference any typedefs. */
c906108c 628 value->type = check_typedef (SYMBOL_TYPE (var));
400c6af0 629 value->optimized_out = 0;
c906108c 630
24d6c2a0
TT
631 if (SYMBOL_COMPUTED_OPS (var) != NULL)
632 {
40f4af28 633 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, ax, value);
24d6c2a0
TT
634 return;
635 }
636
c906108c
SS
637 /* I'm imitating the code in read_var_value. */
638 switch (SYMBOL_CLASS (var))
639 {
640 case LOC_CONST: /* A constant, like an enum value. */
641 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
642 value->kind = axs_rvalue;
643 break;
644
645 case LOC_LABEL: /* A goto label, being used as a value. */
646 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
647 value->kind = axs_rvalue;
648 break;
649
650 case LOC_CONST_BYTES:
8e65ff28 651 internal_error (__FILE__, __LINE__,
3e43a32a
MS
652 _("gen_var_ref: LOC_CONST_BYTES "
653 "symbols are not supported"));
c906108c
SS
654
655 /* Variable at a fixed location in memory. Easy. */
656 case LOC_STATIC:
657 /* Push the address of the variable. */
658 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
659 value->kind = axs_lvalue_memory;
660 break;
661
662 case LOC_ARG: /* var lives in argument area of frame */
40f4af28 663 gen_frame_args_address (ax);
c906108c
SS
664 gen_sym_offset (ax, var);
665 value->kind = axs_lvalue_memory;
666 break;
667
668 case LOC_REF_ARG: /* As above, but the frame slot really
669 holds the address of the variable. */
40f4af28 670 gen_frame_args_address (ax);
c906108c
SS
671 gen_sym_offset (ax, var);
672 /* Don't assume any particular pointer size. */
40f4af28 673 gen_fetch (ax, builtin_type (ax->gdbarch)->builtin_data_ptr);
c906108c
SS
674 value->kind = axs_lvalue_memory;
675 break;
676
677 case LOC_LOCAL: /* var lives in locals area of frame */
40f4af28 678 gen_frame_locals_address (ax);
c906108c
SS
679 gen_sym_offset (ax, var);
680 value->kind = axs_lvalue_memory;
681 break;
682
c906108c 683 case LOC_TYPEDEF:
3d263c1d 684 error (_("Cannot compute value of typedef `%s'."),
987012b8 685 var->print_name ());
c906108c
SS
686 break;
687
688 case LOC_BLOCK:
2b1ffcfd 689 ax_const_l (ax, BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (var)));
c906108c
SS
690 value->kind = axs_rvalue;
691 break;
692
693 case LOC_REGISTER:
c906108c 694 /* Don't generate any code at all; in the process of treating
dda83cd7
SM
695 this as an lvalue or rvalue, the caller will generate the
696 right code. */
c906108c 697 value->kind = axs_lvalue_register;
40f4af28
SM
698 value->u.reg
699 = SYMBOL_REGISTER_OPS (var)->register_number (var, ax->gdbarch);
c906108c
SS
700 break;
701
702 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
dda83cd7
SM
703 register, not on the stack. Simpler than LOC_REGISTER
704 because it's just like any other case where the thing
2a2d4dc3 705 has a real address. */
c906108c 706 case LOC_REGPARM_ADDR:
40f4af28
SM
707 ax_reg (ax,
708 SYMBOL_REGISTER_OPS (var)->register_number (var, ax->gdbarch));
c906108c
SS
709 value->kind = axs_lvalue_memory;
710 break;
711
712 case LOC_UNRESOLVED:
713 {
3b7344d5 714 struct bound_minimal_symbol msym
987012b8 715 = lookup_minimal_symbol (var->linkage_name (), NULL, NULL);
5b4ee69b 716
3b7344d5 717 if (!msym.minsym)
987012b8 718 error (_("Couldn't resolve symbol `%s'."), var->print_name ());
c5aa993b 719
c906108c 720 /* Push the address of the variable. */
77e371c0 721 ax_const_l (ax, BMSYMBOL_VALUE_ADDRESS (msym));
c906108c
SS
722 value->kind = axs_lvalue_memory;
723 }
c5aa993b 724 break;
c906108c 725
a55cc764 726 case LOC_COMPUTED:
24d6c2a0 727 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
a55cc764 728
c906108c 729 case LOC_OPTIMIZED_OUT:
400c6af0
SS
730 /* Flag this, but don't say anything; leave it up to callers to
731 warn the user. */
732 value->optimized_out = 1;
c906108c
SS
733 break;
734
735 default:
3d263c1d 736 error (_("Cannot find value of botched symbol `%s'."),
987012b8 737 var->print_name ());
c906108c
SS
738 break;
739 }
740}
74ea4be4
PA
741
742/* Generate code for a minimal symbol variable reference to AX. The
743 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
744 the result. */
745
746static void
747gen_msym_var_ref (agent_expr *ax, axs_value *value,
748 minimal_symbol *msymbol, objfile *objf)
749{
750 CORE_ADDR address;
751 type *t = find_minsym_type_and_address (msymbol, objf, &address);
752 value->type = t;
753 value->optimized_out = false;
754 ax_const_l (ax, address);
755 value->kind = axs_lvalue_memory;
756}
757
c5aa993b 758\f
c906108c
SS
759
760
c906108c
SS
761/* Generating bytecode from GDB expressions: literals */
762
763static void
fba45db2
KB
764gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
765 struct type *type)
c906108c
SS
766{
767 ax_const_l (ax, k);
768 value->kind = axs_rvalue;
648027cc 769 value->type = check_typedef (type);
c906108c 770}
c5aa993b 771\f
c906108c
SS
772
773
c906108c
SS
774/* Generating bytecode from GDB expressions: unary conversions, casts */
775
776/* Take what's on the top of the stack (as described by VALUE), and
777 try to make an rvalue out of it. Signal an error if we can't do
778 that. */
55aa24fb 779void
fba45db2 780require_rvalue (struct agent_expr *ax, struct axs_value *value)
c906108c 781{
3a96536b
SS
782 /* Only deal with scalars, structs and such may be too large
783 to fit in a stack entry. */
784 value->type = check_typedef (value->type);
78134374
SM
785 if (value->type->code () == TYPE_CODE_ARRAY
786 || value->type->code () == TYPE_CODE_STRUCT
787 || value->type->code () == TYPE_CODE_UNION
788 || value->type->code () == TYPE_CODE_FUNC)
1c40aa62 789 error (_("Value not scalar: cannot be an rvalue."));
3a96536b 790
c906108c
SS
791 switch (value->kind)
792 {
793 case axs_rvalue:
794 /* It's already an rvalue. */
795 break;
796
797 case axs_lvalue_memory:
798 /* The top of stack is the address of the object. Dereference. */
799 gen_fetch (ax, value->type);
800 break;
801
802 case axs_lvalue_register:
803 /* There's nothing on the stack, but value->u.reg is the
dda83cd7 804 register number containing the value.
c906108c 805
dda83cd7
SM
806 When we add floating-point support, this is going to have to
807 change. What about SPARC register pairs, for example? */
c906108c
SS
808 ax_reg (ax, value->u.reg);
809 gen_extend (ax, value->type);
810 break;
811 }
812
813 value->kind = axs_rvalue;
814}
815
816
817/* Assume the top of the stack is described by VALUE, and perform the
818 usual unary conversions. This is motivated by ANSI 6.2.2, but of
819 course GDB expressions are not ANSI; they're the mishmash union of
820 a bunch of languages. Rah.
821
822 NOTE! This function promises to produce an rvalue only when the
823 incoming value is of an appropriate type. In other words, the
824 consumer of the value this function produces may assume the value
825 is an rvalue only after checking its type.
826
827 The immediate issue is that if the user tries to use a structure or
828 union as an operand of, say, the `+' operator, we don't want to try
829 to convert that structure to an rvalue; require_rvalue will bomb on
830 structs and unions. Rather, we want to simply pass the struct
831 lvalue through unchanged, and let `+' raise an error. */
832
833static void
6661ad48 834gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
835{
836 /* We don't have to generate any code for the usual integral
837 conversions, since values are always represented as full-width on
838 the stack. Should we tweak the type? */
839
840 /* Some types require special handling. */
78134374 841 switch (value->type->code ())
c906108c
SS
842 {
843 /* Functions get converted to a pointer to the function. */
844 case TYPE_CODE_FUNC:
845 value->type = lookup_pointer_type (value->type);
846 value->kind = axs_rvalue; /* Should always be true, but just in case. */
847 break;
848
849 /* Arrays get converted to a pointer to their first element, and
dda83cd7 850 are no longer an lvalue. */
c906108c
SS
851 case TYPE_CODE_ARRAY:
852 {
853 struct type *elements = TYPE_TARGET_TYPE (value->type);
5b4ee69b 854
c906108c
SS
855 value->type = lookup_pointer_type (elements);
856 value->kind = axs_rvalue;
857 /* We don't need to generate any code; the address of the array
858 is also the address of its first element. */
859 }
c5aa993b 860 break;
c906108c 861
c5aa993b 862 /* Don't try to convert structures and unions to rvalues. Let the
dda83cd7 863 consumer signal an error. */
c906108c
SS
864 case TYPE_CODE_STRUCT:
865 case TYPE_CODE_UNION:
866 return;
c906108c
SS
867 }
868
869 /* If the value is an lvalue, dereference it. */
870 require_rvalue (ax, value);
871}
872
873
874/* Return non-zero iff the type TYPE1 is considered "wider" than the
875 type TYPE2, according to the rules described in gen_usual_arithmetic. */
876static int
fba45db2 877type_wider_than (struct type *type1, struct type *type2)
c906108c
SS
878{
879 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
880 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
c6d940a9
SM
881 && type1->is_unsigned ()
882 && !type2->is_unsigned ()));
c906108c
SS
883}
884
885
886/* Return the "wider" of the two types TYPE1 and TYPE2. */
887static struct type *
fba45db2 888max_type (struct type *type1, struct type *type2)
c906108c
SS
889{
890 return type_wider_than (type1, type2) ? type1 : type2;
891}
892
893
894/* Generate code to convert a scalar value of type FROM to type TO. */
895static void
fba45db2 896gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
c906108c
SS
897{
898 /* Perhaps there is a more graceful way to state these rules. */
899
900 /* If we're converting to a narrower type, then we need to clear out
901 the upper bits. */
902 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
bcf5c1d9 903 gen_extend (ax, to);
c906108c
SS
904
905 /* If the two values have equal width, but different signednesses,
906 then we need to extend. */
907 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
908 {
c6d940a9 909 if (from->is_unsigned () != to->is_unsigned ())
c906108c
SS
910 gen_extend (ax, to);
911 }
912
913 /* If we're converting to a wider type, and becoming unsigned, then
914 we need to zero out any possible sign bits. */
915 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
916 {
c6d940a9 917 if (to->is_unsigned ())
c906108c
SS
918 gen_extend (ax, to);
919 }
920}
921
922
923/* Return non-zero iff the type FROM will require any bytecodes to be
924 emitted to be converted to the type TO. */
925static int
fba45db2 926is_nontrivial_conversion (struct type *from, struct type *to)
c906108c 927{
833177a4 928 agent_expr_up ax (new agent_expr (NULL, 0));
c906108c
SS
929 int nontrivial;
930
931 /* Actually generate the code, and see if anything came out. At the
932 moment, it would be trivial to replicate the code in
933 gen_conversion here, but in the future, when we're supporting
934 floating point and the like, it may not be. Doing things this
935 way allows this function to be independent of the logic in
936 gen_conversion. */
833177a4 937 gen_conversion (ax.get (), from, to);
c906108c 938 nontrivial = ax->len > 0;
c906108c
SS
939 return nontrivial;
940}
941
942
943/* Generate code to perform the "usual arithmetic conversions" (ANSI C
944 6.2.1.5) for the two operands of an arithmetic operator. This
945 effectively finds a "least upper bound" type for the two arguments,
946 and promotes each argument to that type. *VALUE1 and *VALUE2
947 describe the values as they are passed in, and as they are left. */
948static void
6661ad48
SM
949gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
950 struct axs_value *value2)
c906108c
SS
951{
952 /* Do the usual binary conversions. */
78134374
SM
953 if (value1->type->code () == TYPE_CODE_INT
954 && value2->type->code () == TYPE_CODE_INT)
c906108c
SS
955 {
956 /* The ANSI integral promotions seem to work this way: Order the
dda83cd7
SM
957 integer types by size, and then by signedness: an n-bit
958 unsigned type is considered "wider" than an n-bit signed
959 type. Promote to the "wider" of the two types, and always
960 promote at least to int. */
6661ad48 961 struct type *target = max_type (builtin_type (ax->gdbarch)->builtin_int,
c906108c
SS
962 max_type (value1->type, value2->type));
963
964 /* Deal with value2, on the top of the stack. */
965 gen_conversion (ax, value2->type, target);
966
967 /* Deal with value1, not on the top of the stack. Don't
dda83cd7
SM
968 generate the `swap' instructions if we're not actually going
969 to do anything. */
c906108c
SS
970 if (is_nontrivial_conversion (value1->type, target))
971 {
972 ax_simple (ax, aop_swap);
973 gen_conversion (ax, value1->type, target);
974 ax_simple (ax, aop_swap);
975 }
976
648027cc 977 value1->type = value2->type = check_typedef (target);
c906108c
SS
978 }
979}
980
981
982/* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
983 the value on the top of the stack, as described by VALUE. Assume
984 the value has integral type. */
985static void
6661ad48 986gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
c906108c 987{
6661ad48 988 const struct builtin_type *builtin = builtin_type (ax->gdbarch);
f7c79c41
UW
989
990 if (!type_wider_than (value->type, builtin->builtin_int))
c906108c 991 {
f7c79c41
UW
992 gen_conversion (ax, value->type, builtin->builtin_int);
993 value->type = builtin->builtin_int;
c906108c 994 }
f7c79c41 995 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
c906108c 996 {
f7c79c41
UW
997 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
998 value->type = builtin->builtin_unsigned_int;
c906108c
SS
999 }
1000}
1001
1002
1003/* Generate code for a cast to TYPE. */
1004static void
fba45db2 1005gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
c906108c
SS
1006{
1007 /* GCC does allow casts to yield lvalues, so this should be fixed
1008 before merging these changes into the trunk. */
1009 require_rvalue (ax, value);
0e2de366 1010 /* Dereference typedefs. */
c906108c
SS
1011 type = check_typedef (type);
1012
78134374 1013 switch (type->code ())
c906108c
SS
1014 {
1015 case TYPE_CODE_PTR:
b97aedf3 1016 case TYPE_CODE_REF:
aa006118 1017 case TYPE_CODE_RVALUE_REF:
c906108c 1018 /* It's implementation-defined, and I'll bet this is what GCC
dda83cd7 1019 does. */
c906108c
SS
1020 break;
1021
1022 case TYPE_CODE_ARRAY:
1023 case TYPE_CODE_STRUCT:
1024 case TYPE_CODE_UNION:
1025 case TYPE_CODE_FUNC:
3d263c1d 1026 error (_("Invalid type cast: intended type must be scalar."));
c906108c
SS
1027
1028 case TYPE_CODE_ENUM:
3b11a015 1029 case TYPE_CODE_BOOL:
c906108c 1030 /* We don't have to worry about the size of the value, because
dda83cd7
SM
1031 all our integral values are fully sign-extended, and when
1032 casting pointers we can do anything we like. Is there any
1033 way for us to know what GCC actually does with a cast like
1034 this? */
c906108c 1035 break;
c5aa993b 1036
c906108c
SS
1037 case TYPE_CODE_INT:
1038 gen_conversion (ax, value->type, type);
1039 break;
1040
1041 case TYPE_CODE_VOID:
1042 /* We could pop the value, and rely on everyone else to check
dda83cd7
SM
1043 the type and notice that this value doesn't occupy a stack
1044 slot. But for now, leave the value on the stack, and
1045 preserve the "value == stack element" assumption. */
c906108c
SS
1046 break;
1047
1048 default:
3d263c1d 1049 error (_("Casts to requested type are not yet implemented."));
c906108c
SS
1050 }
1051
1052 value->type = type;
1053}
c5aa993b 1054\f
c906108c
SS
1055
1056
c906108c
SS
1057/* Generating bytecode from GDB expressions: arithmetic */
1058
1059/* Scale the integer on the top of the stack by the size of the target
1060 of the pointer type TYPE. */
1061static void
fba45db2 1062gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
c906108c
SS
1063{
1064 struct type *element = TYPE_TARGET_TYPE (type);
1065
0004e5a2 1066 if (TYPE_LENGTH (element) != 1)
c906108c 1067 {
0004e5a2 1068 ax_const_l (ax, TYPE_LENGTH (element));
c906108c
SS
1069 ax_simple (ax, op);
1070 }
1071}
1072
1073
f7c79c41 1074/* Generate code for pointer arithmetic PTR + INT. */
c906108c 1075static void
f7c79c41
UW
1076gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1077 struct axs_value *value1, struct axs_value *value2)
c906108c 1078{
b97aedf3 1079 gdb_assert (pointer_type (value1->type));
78134374 1080 gdb_assert (value2->type->code () == TYPE_CODE_INT);
c906108c 1081
f7c79c41
UW
1082 gen_scale (ax, aop_mul, value1->type);
1083 ax_simple (ax, aop_add);
1084 gen_extend (ax, value1->type); /* Catch overflow. */
1085 value->type = value1->type;
1086 value->kind = axs_rvalue;
1087}
c906108c 1088
c906108c 1089
f7c79c41
UW
1090/* Generate code for pointer arithmetic PTR - INT. */
1091static void
1092gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1093 struct axs_value *value1, struct axs_value *value2)
1094{
b97aedf3 1095 gdb_assert (pointer_type (value1->type));
78134374 1096 gdb_assert (value2->type->code () == TYPE_CODE_INT);
c906108c 1097
f7c79c41
UW
1098 gen_scale (ax, aop_mul, value1->type);
1099 ax_simple (ax, aop_sub);
1100 gen_extend (ax, value1->type); /* Catch overflow. */
1101 value->type = value1->type;
c906108c
SS
1102 value->kind = axs_rvalue;
1103}
1104
1105
f7c79c41 1106/* Generate code for pointer arithmetic PTR - PTR. */
c906108c 1107static void
f7c79c41
UW
1108gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1109 struct axs_value *value1, struct axs_value *value2,
1110 struct type *result_type)
c906108c 1111{
b97aedf3
SS
1112 gdb_assert (pointer_type (value1->type));
1113 gdb_assert (pointer_type (value2->type));
c906108c 1114
f7c79c41
UW
1115 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1116 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
ac74f770
MS
1117 error (_("\
1118First argument of `-' is a pointer, but second argument is neither\n\
1119an integer nor a pointer of the same type."));
c906108c 1120
f7c79c41
UW
1121 ax_simple (ax, aop_sub);
1122 gen_scale (ax, aop_div_unsigned, value1->type);
1123 value->type = result_type;
c906108c
SS
1124 value->kind = axs_rvalue;
1125}
1126
3b11a015
SS
1127static void
1128gen_equal (struct agent_expr *ax, struct axs_value *value,
1129 struct axs_value *value1, struct axs_value *value2,
1130 struct type *result_type)
1131{
1132 if (pointer_type (value1->type) || pointer_type (value2->type))
1133 ax_simple (ax, aop_equal);
1134 else
1135 gen_binop (ax, value, value1, value2,
1136 aop_equal, aop_equal, 0, "equal");
1137 value->type = result_type;
1138 value->kind = axs_rvalue;
1139}
1140
1141static void
1142gen_less (struct agent_expr *ax, struct axs_value *value,
1143 struct axs_value *value1, struct axs_value *value2,
1144 struct type *result_type)
1145{
1146 if (pointer_type (value1->type) || pointer_type (value2->type))
1147 ax_simple (ax, aop_less_unsigned);
1148 else
1149 gen_binop (ax, value, value1, value2,
1150 aop_less_signed, aop_less_unsigned, 0, "less than");
1151 value->type = result_type;
1152 value->kind = axs_rvalue;
1153}
f7c79c41 1154
c906108c
SS
1155/* Generate code for a binary operator that doesn't do pointer magic.
1156 We set VALUE to describe the result value; we assume VALUE1 and
1157 VALUE2 describe the two operands, and that they've undergone the
1158 usual binary conversions. MAY_CARRY should be non-zero iff the
1159 result needs to be extended. NAME is the English name of the
1160 operator, used in error messages */
1161static void
fba45db2 1162gen_binop (struct agent_expr *ax, struct axs_value *value,
3e43a32a
MS
1163 struct axs_value *value1, struct axs_value *value2,
1164 enum agent_op op, enum agent_op op_unsigned,
a121b7c1 1165 int may_carry, const char *name)
c906108c
SS
1166{
1167 /* We only handle INT op INT. */
78134374
SM
1168 if ((value1->type->code () != TYPE_CODE_INT)
1169 || (value2->type->code () != TYPE_CODE_INT))
3d263c1d 1170 error (_("Invalid combination of types in %s."), name);
c5aa993b 1171
c6d940a9 1172 ax_simple (ax, value1->type->is_unsigned () ? op_unsigned : op);
c906108c 1173 if (may_carry)
c5aa993b 1174 gen_extend (ax, value1->type); /* catch overflow */
c906108c
SS
1175 value->type = value1->type;
1176 value->kind = axs_rvalue;
1177}
1178
1179
1180static void
f7c79c41
UW
1181gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1182 struct type *result_type)
c906108c 1183{
78134374
SM
1184 if (value->type->code () != TYPE_CODE_INT
1185 && value->type->code () != TYPE_CODE_PTR)
3d263c1d 1186 error (_("Invalid type of operand to `!'."));
c906108c 1187
c906108c 1188 ax_simple (ax, aop_log_not);
f7c79c41 1189 value->type = result_type;
c906108c
SS
1190}
1191
1192
1193static void
fba45db2 1194gen_complement (struct agent_expr *ax, struct axs_value *value)
c906108c 1195{
78134374 1196 if (value->type->code () != TYPE_CODE_INT)
3d263c1d 1197 error (_("Invalid type of operand to `~'."));
c906108c 1198
c906108c
SS
1199 ax_simple (ax, aop_bit_not);
1200 gen_extend (ax, value->type);
1201}
c5aa993b 1202\f
c906108c
SS
1203
1204
c906108c
SS
1205/* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1206
1207/* Dereference the value on the top of the stack. */
1208static void
053f8057 1209gen_deref (struct axs_value *value)
c906108c
SS
1210{
1211 /* The caller should check the type, because several operators use
1212 this, and we don't know what error message to generate. */
b97aedf3 1213 if (!pointer_type (value->type))
8e65ff28 1214 internal_error (__FILE__, __LINE__,
3d263c1d 1215 _("gen_deref: expected a pointer"));
c906108c
SS
1216
1217 /* We've got an rvalue now, which is a pointer. We want to yield an
1218 lvalue, whose address is exactly that pointer. So we don't
1219 actually emit any code; we just change the type from "Pointer to
1220 T" to "T", and mark the value as an lvalue in memory. Leave it
1221 to the consumer to actually dereference it. */
1222 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
78134374 1223 if (value->type->code () == TYPE_CODE_VOID)
b1028c8e 1224 error (_("Attempt to dereference a generic pointer."));
78134374 1225 value->kind = ((value->type->code () == TYPE_CODE_FUNC)
c906108c
SS
1226 ? axs_rvalue : axs_lvalue_memory);
1227}
1228
1229
1230/* Produce the address of the lvalue on the top of the stack. */
1231static void
053f8057 1232gen_address_of (struct axs_value *value)
c906108c
SS
1233{
1234 /* Special case for taking the address of a function. The ANSI
1235 standard describes this as a special case, too, so this
1236 arrangement is not without motivation. */
78134374 1237 if (value->type->code () == TYPE_CODE_FUNC)
c906108c
SS
1238 /* The value's already an rvalue on the stack, so we just need to
1239 change the type. */
1240 value->type = lookup_pointer_type (value->type);
1241 else
1242 switch (value->kind)
1243 {
1244 case axs_rvalue:
3d263c1d 1245 error (_("Operand of `&' is an rvalue, which has no address."));
c906108c
SS
1246
1247 case axs_lvalue_register:
3d263c1d 1248 error (_("Operand of `&' is in a register, and has no address."));
c906108c
SS
1249
1250 case axs_lvalue_memory:
1251 value->kind = axs_rvalue;
1252 value->type = lookup_pointer_type (value->type);
1253 break;
1254 }
1255}
1256
c906108c
SS
1257/* Generate code to push the value of a bitfield of a structure whose
1258 address is on the top of the stack. START and END give the
1259 starting and one-past-ending *bit* numbers of the field within the
1260 structure. */
1261static void
6661ad48
SM
1262gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1263 struct type *type, int start, int end)
c906108c
SS
1264{
1265 /* Note that ops[i] fetches 8 << i bits. */
1266 static enum agent_op ops[]
5b4ee69b 1267 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
c906108c
SS
1268 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1269
1270 /* We don't want to touch any byte that the bitfield doesn't
1271 actually occupy; we shouldn't make any accesses we're not
1272 explicitly permitted to. We rely here on the fact that the
1273 bytecode `ref' operators work on unaligned addresses.
1274
1275 It takes some fancy footwork to get the stack to work the way
1276 we'd like. Say we're retrieving a bitfield that requires three
1277 fetches. Initially, the stack just contains the address:
c5aa993b 1278 addr
c906108c 1279 For the first fetch, we duplicate the address
c5aa993b 1280 addr addr
c906108c
SS
1281 then add the byte offset, do the fetch, and shift and mask as
1282 needed, yielding a fragment of the value, properly aligned for
1283 the final bitwise or:
c5aa993b 1284 addr frag1
c906108c 1285 then we swap, and repeat the process:
c5aa993b
JM
1286 frag1 addr --- address on top
1287 frag1 addr addr --- duplicate it
1288 frag1 addr frag2 --- get second fragment
1289 frag1 frag2 addr --- swap again
1290 frag1 frag2 frag3 --- get third fragment
c906108c
SS
1291 Notice that, since the third fragment is the last one, we don't
1292 bother duplicating the address this time. Now we have all the
1293 fragments on the stack, and we can simply `or' them together,
1294 yielding the final value of the bitfield. */
1295
1296 /* The first and one-after-last bits in the field, but rounded down
1297 and up to byte boundaries. */
1298 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
c5aa993b
JM
1299 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1300 / TARGET_CHAR_BIT)
1301 * TARGET_CHAR_BIT);
c906108c
SS
1302
1303 /* current bit offset within the structure */
1304 int offset;
1305
1306 /* The index in ops of the opcode we're considering. */
1307 int op;
1308
1309 /* The number of fragments we generated in the process. Probably
1310 equal to the number of `one' bits in bytesize, but who cares? */
1311 int fragment_count;
1312
0e2de366 1313 /* Dereference any typedefs. */
c906108c
SS
1314 type = check_typedef (type);
1315
1316 /* Can we fetch the number of bits requested at all? */
1317 if ((end - start) > ((1 << num_ops) * 8))
8e65ff28 1318 internal_error (__FILE__, __LINE__,
3d263c1d 1319 _("gen_bitfield_ref: bitfield too wide"));
c906108c
SS
1320
1321 /* Note that we know here that we only need to try each opcode once.
1322 That may not be true on machines with weird byte sizes. */
1323 offset = bound_start;
1324 fragment_count = 0;
1325 for (op = num_ops - 1; op >= 0; op--)
1326 {
1327 /* number of bits that ops[op] would fetch */
1328 int op_size = 8 << op;
1329
1330 /* The stack at this point, from bottom to top, contains zero or
dda83cd7 1331 more fragments, then the address. */
c5aa993b 1332
c906108c
SS
1333 /* Does this fetch fit within the bitfield? */
1334 if (offset + op_size <= bound_end)
1335 {
1336 /* Is this the last fragment? */
1337 int last_frag = (offset + op_size == bound_end);
1338
c5aa993b
JM
1339 if (!last_frag)
1340 ax_simple (ax, aop_dup); /* keep a copy of the address */
1341
c906108c
SS
1342 /* Add the offset. */
1343 gen_offset (ax, offset / TARGET_CHAR_BIT);
1344
92bc6a20 1345 if (ax->tracing)
c906108c
SS
1346 {
1347 /* Record the area of memory we're about to fetch. */
1348 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1349 }
1350
1351 /* Perform the fetch. */
1352 ax_simple (ax, ops[op]);
c5aa993b
JM
1353
1354 /* Shift the bits we have to their proper position.
c906108c
SS
1355 gen_left_shift will generate right shifts when the operand
1356 is negative.
1357
c5aa993b
JM
1358 A big-endian field diagram to ponder:
1359 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1360 +------++------++------++------++------++------++------++------+
1361 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1362 ^ ^ ^ ^
1363 bit number 16 32 48 53
c906108c
SS
1364 These are bit numbers as supplied by GDB. Note that the
1365 bit numbers run from right to left once you've fetched the
1366 value!
1367
c5aa993b
JM
1368 A little-endian field diagram to ponder:
1369 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1370 +------++------++------++------++------++------++------++------+
1371 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1372 ^ ^ ^ ^ ^
1373 bit number 48 32 16 4 0
1374
1375 In both cases, the most significant end is on the left
1376 (i.e. normal numeric writing order), which means that you
1377 don't go crazy thinking about `left' and `right' shifts.
1378
1379 We don't have to worry about masking yet:
1380 - If they contain garbage off the least significant end, then we
1381 must be looking at the low end of the field, and the right
1382 shift will wipe them out.
1383 - If they contain garbage off the most significant end, then we
1384 must be looking at the most significant end of the word, and
1385 the sign/zero extension will wipe them out.
1386 - If we're in the interior of the word, then there is no garbage
1387 on either end, because the ref operators zero-extend. */
6661ad48 1388 if (gdbarch_byte_order (ax->gdbarch) == BFD_ENDIAN_BIG)
c906108c 1389 gen_left_shift (ax, end - (offset + op_size));
c5aa993b 1390 else
c906108c
SS
1391 gen_left_shift (ax, offset - start);
1392
c5aa993b 1393 if (!last_frag)
c906108c
SS
1394 /* Bring the copy of the address up to the top. */
1395 ax_simple (ax, aop_swap);
1396
1397 offset += op_size;
1398 fragment_count++;
1399 }
1400 }
1401
1402 /* Generate enough bitwise `or' operations to combine all the
1403 fragments we left on the stack. */
1404 while (fragment_count-- > 1)
1405 ax_simple (ax, aop_bit_or);
1406
1407 /* Sign- or zero-extend the value as appropriate. */
c6d940a9 1408 ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, end - start));
c906108c
SS
1409
1410 /* This is *not* an lvalue. Ugh. */
1411 value->kind = axs_rvalue;
1412 value->type = type;
1413}
1414
b6e7192f
SS
1415/* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1416 is an accumulated offset (in bytes), will be nonzero for objects
1417 embedded in other objects, like C++ base classes. Behavior should
1418 generally follow value_primitive_field. */
1419
1420static void
6661ad48 1421gen_primitive_field (struct agent_expr *ax, struct axs_value *value,
b6e7192f
SS
1422 int offset, int fieldno, struct type *type)
1423{
1424 /* Is this a bitfield? */
1425 if (TYPE_FIELD_PACKED (type, fieldno))
940da03e 1426 gen_bitfield_ref (ax, value, type->field (fieldno).type (),
b6e7192f
SS
1427 (offset * TARGET_CHAR_BIT
1428 + TYPE_FIELD_BITPOS (type, fieldno)),
1429 (offset * TARGET_CHAR_BIT
1430 + TYPE_FIELD_BITPOS (type, fieldno)
1431 + TYPE_FIELD_BITSIZE (type, fieldno)));
1432 else
1433 {
1434 gen_offset (ax, offset
1435 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1436 value->kind = axs_lvalue_memory;
940da03e 1437 value->type = type->field (fieldno).type ();
b6e7192f
SS
1438 }
1439}
1440
1441/* Search for the given field in either the given type or one of its
1442 base classes. Return 1 if found, 0 if not. */
1443
1444static int
6661ad48 1445gen_struct_ref_recursive (struct agent_expr *ax, struct axs_value *value,
a121b7c1 1446 const char *field, int offset, struct type *type)
b6e7192f
SS
1447{
1448 int i, rslt;
1449 int nbases = TYPE_N_BASECLASSES (type);
1450
f168693b 1451 type = check_typedef (type);
b6e7192f 1452
1f704f76 1453 for (i = type->num_fields () - 1; i >= nbases; i--)
b6e7192f 1454 {
0d5cff50 1455 const char *this_name = TYPE_FIELD_NAME (type, i);
b6e7192f
SS
1456
1457 if (this_name)
1458 {
1459 if (strcmp (field, this_name) == 0)
1460 {
1461 /* Note that bytecodes for the struct's base (aka
1462 "this") will have been generated already, which will
1463 be unnecessary but not harmful if the static field is
1464 being handled as a global. */
ceacbf6e 1465 if (field_is_static (&type->field (i)))
b6e7192f 1466 {
40f4af28 1467 gen_static_field (ax, value, type, i);
400c6af0 1468 if (value->optimized_out)
3e43a32a
MS
1469 error (_("static field `%s' has been "
1470 "optimized out, cannot use"),
400c6af0 1471 field);
b6e7192f
SS
1472 return 1;
1473 }
1474
6661ad48 1475 gen_primitive_field (ax, value, offset, i, type);
b6e7192f
SS
1476 return 1;
1477 }
1478#if 0 /* is this right? */
1479 if (this_name[0] == '\0')
1480 internal_error (__FILE__, __LINE__,
1481 _("find_field: anonymous unions not supported"));
1482#endif
1483 }
1484 }
1485
1486 /* Now scan through base classes recursively. */
1487 for (i = 0; i < nbases; i++)
1488 {
1489 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1490
6661ad48 1491 rslt = gen_struct_ref_recursive (ax, value, field,
3e43a32a
MS
1492 offset + TYPE_BASECLASS_BITPOS (type, i)
1493 / TARGET_CHAR_BIT,
b6e7192f
SS
1494 basetype);
1495 if (rslt)
1496 return 1;
1497 }
1498
1499 /* Not found anywhere, flag so caller can complain. */
1500 return 0;
1501}
c906108c
SS
1502
1503/* Generate code to reference the member named FIELD of a structure or
1504 union. The top of the stack, as described by VALUE, should have
1505 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1506 the operator being compiled, and OPERAND_NAME is the kind of thing
1507 it operates on; we use them in error messages. */
1508static void
6661ad48
SM
1509gen_struct_ref (struct agent_expr *ax, struct axs_value *value,
1510 const char *field, const char *operator_name,
1511 const char *operand_name)
c906108c
SS
1512{
1513 struct type *type;
b6e7192f 1514 int found;
c906108c
SS
1515
1516 /* Follow pointers until we reach a non-pointer. These aren't the C
1517 semantics, but they're what the normal GDB evaluator does, so we
1518 should at least be consistent. */
b97aedf3 1519 while (pointer_type (value->type))
c906108c 1520 {
f7c79c41 1521 require_rvalue (ax, value);
053f8057 1522 gen_deref (value);
c906108c 1523 }
e8860ec2 1524 type = check_typedef (value->type);
c906108c
SS
1525
1526 /* This must yield a structure or a union. */
78134374
SM
1527 if (type->code () != TYPE_CODE_STRUCT
1528 && type->code () != TYPE_CODE_UNION)
3d263c1d 1529 error (_("The left operand of `%s' is not a %s."),
c906108c
SS
1530 operator_name, operand_name);
1531
1532 /* And it must be in memory; we don't deal with structure rvalues,
1533 or structures living in registers. */
1534 if (value->kind != axs_lvalue_memory)
3d263c1d 1535 error (_("Structure does not live in memory."));
c906108c 1536
b6e7192f 1537 /* Search through fields and base classes recursively. */
6661ad48 1538 found = gen_struct_ref_recursive (ax, value, field, 0, type);
b6e7192f
SS
1539
1540 if (!found)
1541 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
7d93a1e0 1542 field, type->name ());
b6e7192f 1543}
c5aa993b 1544
b6e7192f 1545static int
6661ad48 1546gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
caaece0e 1547 const struct type *curtype, const char *name);
b6e7192f 1548static int
6661ad48 1549gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
caaece0e 1550 const struct type *curtype, const char *name);
b6e7192f
SS
1551
1552static void
40f4af28 1553gen_static_field (struct agent_expr *ax, struct axs_value *value,
b6e7192f
SS
1554 struct type *type, int fieldno)
1555{
1556 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
c906108c 1557 {
b6e7192f 1558 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
c906108c 1559 value->kind = axs_lvalue_memory;
940da03e 1560 value->type = type->field (fieldno).type ();
400c6af0 1561 value->optimized_out = 0;
b6e7192f
SS
1562 }
1563 else
1564 {
ff355380 1565 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
d12307c1 1566 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0).symbol;
b6e7192f 1567
400c6af0
SS
1568 if (sym)
1569 {
40f4af28 1570 gen_var_ref (ax, value, sym);
400c6af0
SS
1571
1572 /* Don't error if the value was optimized out, we may be
1573 scanning all static fields and just want to pass over this
1574 and continue with the rest. */
1575 }
1576 else
1577 {
1578 /* Silently assume this was optimized out; class printing
1579 will let the user know why the data is missing. */
1580 value->optimized_out = 1;
1581 }
b6e7192f
SS
1582 }
1583}
1584
1585static int
6661ad48 1586gen_struct_elt_for_reference (struct agent_expr *ax, struct axs_value *value,
caaece0e 1587 struct type *type, const char *fieldname)
b6e7192f
SS
1588{
1589 struct type *t = type;
1590 int i;
b6e7192f 1591
78134374
SM
1592 if (t->code () != TYPE_CODE_STRUCT
1593 && t->code () != TYPE_CODE_UNION)
b6e7192f
SS
1594 internal_error (__FILE__, __LINE__,
1595 _("non-aggregate type to gen_struct_elt_for_reference"));
1596
1f704f76 1597 for (i = t->num_fields () - 1; i >= TYPE_N_BASECLASSES (t); i--)
b6e7192f 1598 {
0d5cff50 1599 const char *t_field_name = TYPE_FIELD_NAME (t, i);
b6e7192f
SS
1600
1601 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1602 {
ceacbf6e 1603 if (field_is_static (&t->field (i)))
b6e7192f 1604 {
40f4af28 1605 gen_static_field (ax, value, t, i);
400c6af0 1606 if (value->optimized_out)
3e43a32a
MS
1607 error (_("static field `%s' has been "
1608 "optimized out, cannot use"),
400c6af0 1609 fieldname);
b6e7192f
SS
1610 return 1;
1611 }
1612 if (TYPE_FIELD_PACKED (t, i))
1613 error (_("pointers to bitfield members not allowed"));
1614
1615 /* FIXME we need a way to do "want_address" equivalent */
1616
1617 error (_("Cannot reference non-static field \"%s\""), fieldname);
1618 }
c906108c 1619 }
b6e7192f
SS
1620
1621 /* FIXME add other scoped-reference cases here */
1622
1623 /* Do a last-ditch lookup. */
6661ad48 1624 return gen_maybe_namespace_elt (ax, value, type, fieldname);
c906108c
SS
1625}
1626
b6e7192f
SS
1627/* C++: Return the member NAME of the namespace given by the type
1628 CURTYPE. */
1629
1630static int
6661ad48 1631gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
caaece0e 1632 const struct type *curtype, const char *name)
b6e7192f 1633{
6661ad48 1634 int found = gen_maybe_namespace_elt (ax, value, curtype, name);
b6e7192f
SS
1635
1636 if (!found)
1637 error (_("No symbol \"%s\" in namespace \"%s\"."),
7d93a1e0 1638 name, curtype->name ());
b6e7192f
SS
1639
1640 return found;
1641}
1642
1643/* A helper function used by value_namespace_elt and
1644 value_struct_elt_for_reference. It looks up NAME inside the
1645 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1646 is a class and NAME refers to a type in CURTYPE itself (as opposed
1647 to, say, some base class of CURTYPE). */
1648
1649static int
6661ad48 1650gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
caaece0e 1651 const struct type *curtype, const char *name)
b6e7192f 1652{
7d93a1e0 1653 const char *namespace_name = curtype->name ();
d12307c1 1654 struct block_symbol sym;
b6e7192f
SS
1655
1656 sym = cp_lookup_symbol_namespace (namespace_name, name,
1657 block_for_pc (ax->scope),
ac0cd78b 1658 VAR_DOMAIN);
b6e7192f 1659
d12307c1 1660 if (sym.symbol == NULL)
b6e7192f
SS
1661 return 0;
1662
40f4af28 1663 gen_var_ref (ax, value, sym.symbol);
b6e7192f 1664
400c6af0
SS
1665 if (value->optimized_out)
1666 error (_("`%s' has been optimized out, cannot use"),
987012b8 1667 sym.symbol->print_name ());
400c6af0 1668
b6e7192f
SS
1669 return 1;
1670}
1671
1672
1673static int
6661ad48 1674gen_aggregate_elt_ref (struct agent_expr *ax, struct axs_value *value,
caaece0e 1675 struct type *type, const char *field)
b6e7192f 1676{
78134374 1677 switch (type->code ())
b6e7192f
SS
1678 {
1679 case TYPE_CODE_STRUCT:
1680 case TYPE_CODE_UNION:
6661ad48 1681 return gen_struct_elt_for_reference (ax, value, type, field);
b6e7192f
SS
1682 break;
1683 case TYPE_CODE_NAMESPACE:
6661ad48 1684 return gen_namespace_elt (ax, value, type, field);
b6e7192f
SS
1685 break;
1686 default:
1687 internal_error (__FILE__, __LINE__,
1688 _("non-aggregate type in gen_aggregate_elt_ref"));
1689 }
1690
1691 return 0;
1692}
c906108c 1693
0e2de366 1694/* Generate code for GDB's magical `repeat' operator.
c906108c
SS
1695 LVALUE @ INT creates an array INT elements long, and whose elements
1696 have the same type as LVALUE, located in memory so that LVALUE is
1697 its first element. For example, argv[0]@argc gives you the array
1698 of command-line arguments.
1699
1700 Unfortunately, because we have to know the types before we actually
1701 have a value for the expression, we can't implement this perfectly
1702 without changing the type system, having values that occupy two
1703 stack slots, doing weird things with sizeof, etc. So we require
1704 the right operand to be a constant expression. */
1705static void
f7c79c41
UW
1706gen_repeat (struct expression *exp, union exp_element **pc,
1707 struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1708{
1709 struct axs_value value1;
5b4ee69b 1710
c906108c
SS
1711 /* We don't want to turn this into an rvalue, so no conversions
1712 here. */
f7c79c41 1713 gen_expr (exp, pc, ax, &value1);
c906108c 1714 if (value1.kind != axs_lvalue_memory)
3d263c1d 1715 error (_("Left operand of `@' must be an object in memory."));
c906108c
SS
1716
1717 /* Evaluate the length; it had better be a constant. */
1718 {
1719 struct value *v = const_expr (pc);
1720 int length;
1721
c5aa993b 1722 if (!v)
3e43a32a
MS
1723 error (_("Right operand of `@' must be a "
1724 "constant, in agent expressions."));
78134374 1725 if (value_type (v)->code () != TYPE_CODE_INT)
3d263c1d 1726 error (_("Right operand of `@' must be an integer."));
c906108c
SS
1727 length = value_as_long (v);
1728 if (length <= 0)
3d263c1d 1729 error (_("Right operand of `@' must be positive."));
c906108c
SS
1730
1731 /* The top of the stack is already the address of the object, so
1732 all we need to do is frob the type of the lvalue. */
1733 {
1734 /* FIXME-type-allocation: need a way to free this type when we are
dda83cd7 1735 done with it. */
e3506a9f
UW
1736 struct type *array
1737 = lookup_array_range_type (value1.type, 0, length - 1);
c906108c
SS
1738
1739 value->kind = axs_lvalue_memory;
1740 value->type = array;
1741 }
1742 }
1743}
1744
1745
1746/* Emit code for the `sizeof' operator.
1747 *PC should point at the start of the operand expression; we advance it
1748 to the first instruction after the operand. */
1749static void
f7c79c41
UW
1750gen_sizeof (struct expression *exp, union exp_element **pc,
1751 struct agent_expr *ax, struct axs_value *value,
1752 struct type *size_type)
c906108c
SS
1753{
1754 /* We don't care about the value of the operand expression; we only
1755 care about its type. However, in the current arrangement, the
1756 only way to find an expression's type is to generate code for it.
1757 So we generate code for the operand, and then throw it away,
1758 replacing it with code that simply pushes its size. */
1759 int start = ax->len;
5b4ee69b 1760
f7c79c41 1761 gen_expr (exp, pc, ax, value);
c906108c
SS
1762
1763 /* Throw away the code we just generated. */
1764 ax->len = start;
c5aa993b 1765
c906108c
SS
1766 ax_const_l (ax, TYPE_LENGTH (value->type));
1767 value->kind = axs_rvalue;
f7c79c41 1768 value->type = size_type;
c906108c 1769}
c906108c 1770\f
c5aa993b 1771
46a4882b
PA
1772/* Generate bytecode for a cast to TO_TYPE. Advance *PC over the
1773 subexpression. */
1774
1775static void
1776gen_expr_for_cast (struct expression *exp, union exp_element **pc,
1777 struct agent_expr *ax, struct axs_value *value,
1778 struct type *to_type)
1779{
1780 enum exp_opcode op = (*pc)[0].opcode;
1781
1782 /* Don't let symbols be handled with gen_expr because that throws an
1783 "unknown type" error for no-debug data symbols. Instead, we want
1784 the cast to reinterpret such symbols. */
1785 if (op == OP_VAR_MSYM_VALUE || op == OP_VAR_VALUE)
1786 {
1787 if (op == OP_VAR_VALUE)
1788 {
1789 gen_var_ref (ax, value, (*pc)[2].symbol);
1790
1791 if (value->optimized_out)
1792 error (_("`%s' has been optimized out, cannot use"),
987012b8 1793 (*pc)[2].symbol->print_name ());
46a4882b
PA
1794 }
1795 else
1796 gen_msym_var_ref (ax, value, (*pc)[2].msymbol, (*pc)[1].objfile);
78134374 1797 if (value->type->code () == TYPE_CODE_ERROR)
46a4882b
PA
1798 value->type = to_type;
1799 (*pc) += 4;
1800 }
1801 else
1802 gen_expr (exp, pc, ax, value);
1803 gen_cast (ax, value, to_type);
1804}
1805
c906108c
SS
1806/* Generating bytecode from GDB expressions: general recursive thingy */
1807
3d263c1d 1808/* XXX: i18n */
c906108c
SS
1809/* A gen_expr function written by a Gen-X'er guy.
1810 Append code for the subexpression of EXPR starting at *POS_P to AX. */
55aa24fb 1811void
f7c79c41
UW
1812gen_expr (struct expression *exp, union exp_element **pc,
1813 struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1814{
1815 /* Used to hold the descriptions of operand expressions. */
09d559e4 1816 struct axs_value value1, value2, value3;
f61e138d 1817 enum exp_opcode op = (*pc)[0].opcode, op2;
09d559e4 1818 int if1, go1, if2, go2, end;
6661ad48 1819 struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
c906108c
SS
1820
1821 /* If we're looking at a constant expression, just push its value. */
1822 {
1823 struct value *v = maybe_const_expr (pc);
c5aa993b 1824
c906108c
SS
1825 if (v)
1826 {
1827 ax_const_l (ax, value_as_long (v));
1828 value->kind = axs_rvalue;
df407dfe 1829 value->type = check_typedef (value_type (v));
c906108c
SS
1830 return;
1831 }
1832 }
1833
1834 /* Otherwise, go ahead and generate code for it. */
1835 switch (op)
1836 {
1837 /* Binary arithmetic operators. */
1838 case BINOP_ADD:
1839 case BINOP_SUB:
1840 case BINOP_MUL:
1841 case BINOP_DIV:
1842 case BINOP_REM:
948103cf
SS
1843 case BINOP_LSH:
1844 case BINOP_RSH:
c906108c
SS
1845 case BINOP_SUBSCRIPT:
1846 case BINOP_BITWISE_AND:
1847 case BINOP_BITWISE_IOR:
1848 case BINOP_BITWISE_XOR:
782b2b07
SS
1849 case BINOP_EQUAL:
1850 case BINOP_NOTEQUAL:
1851 case BINOP_LESS:
1852 case BINOP_GTR:
1853 case BINOP_LEQ:
1854 case BINOP_GEQ:
c906108c 1855 (*pc)++;
f7c79c41 1856 gen_expr (exp, pc, ax, &value1);
6661ad48 1857 gen_usual_unary (ax, &value1);
f61e138d
SS
1858 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1859 break;
1860
09d559e4
SS
1861 case BINOP_LOGICAL_AND:
1862 (*pc)++;
1863 /* Generate the obvious sequence of tests and jumps. */
1864 gen_expr (exp, pc, ax, &value1);
6661ad48 1865 gen_usual_unary (ax, &value1);
09d559e4
SS
1866 if1 = ax_goto (ax, aop_if_goto);
1867 go1 = ax_goto (ax, aop_goto);
1868 ax_label (ax, if1, ax->len);
1869 gen_expr (exp, pc, ax, &value2);
6661ad48 1870 gen_usual_unary (ax, &value2);
09d559e4
SS
1871 if2 = ax_goto (ax, aop_if_goto);
1872 go2 = ax_goto (ax, aop_goto);
1873 ax_label (ax, if2, ax->len);
1874 ax_const_l (ax, 1);
1875 end = ax_goto (ax, aop_goto);
1876 ax_label (ax, go1, ax->len);
1877 ax_label (ax, go2, ax->len);
1878 ax_const_l (ax, 0);
1879 ax_label (ax, end, ax->len);
1880 value->kind = axs_rvalue;
3b11a015 1881 value->type = int_type;
09d559e4
SS
1882 break;
1883
1884 case BINOP_LOGICAL_OR:
1885 (*pc)++;
1886 /* Generate the obvious sequence of tests and jumps. */
1887 gen_expr (exp, pc, ax, &value1);
6661ad48 1888 gen_usual_unary (ax, &value1);
09d559e4
SS
1889 if1 = ax_goto (ax, aop_if_goto);
1890 gen_expr (exp, pc, ax, &value2);
6661ad48 1891 gen_usual_unary (ax, &value2);
09d559e4
SS
1892 if2 = ax_goto (ax, aop_if_goto);
1893 ax_const_l (ax, 0);
1894 end = ax_goto (ax, aop_goto);
1895 ax_label (ax, if1, ax->len);
1896 ax_label (ax, if2, ax->len);
1897 ax_const_l (ax, 1);
1898 ax_label (ax, end, ax->len);
1899 value->kind = axs_rvalue;
3b11a015 1900 value->type = int_type;
09d559e4
SS
1901 break;
1902
1903 case TERNOP_COND:
1904 (*pc)++;
1905 gen_expr (exp, pc, ax, &value1);
6661ad48 1906 gen_usual_unary (ax, &value1);
09d559e4
SS
1907 /* For (A ? B : C), it's easiest to generate subexpression
1908 bytecodes in order, but if_goto jumps on true, so we invert
1909 the sense of A. Then we can do B by dropping through, and
1910 jump to do C. */
3b11a015 1911 gen_logical_not (ax, &value1, int_type);
09d559e4
SS
1912 if1 = ax_goto (ax, aop_if_goto);
1913 gen_expr (exp, pc, ax, &value2);
6661ad48 1914 gen_usual_unary (ax, &value2);
09d559e4
SS
1915 end = ax_goto (ax, aop_goto);
1916 ax_label (ax, if1, ax->len);
1917 gen_expr (exp, pc, ax, &value3);
6661ad48 1918 gen_usual_unary (ax, &value3);
09d559e4 1919 ax_label (ax, end, ax->len);
85102364 1920 /* This is arbitrary - what if B and C are incompatible types? */
09d559e4
SS
1921 value->type = value2.type;
1922 value->kind = value2.kind;
1923 break;
1924
f61e138d
SS
1925 case BINOP_ASSIGN:
1926 (*pc)++;
1927 if ((*pc)[0].opcode == OP_INTERNALVAR)
c906108c 1928 {
caaece0e 1929 const char *name = internalvar_name ((*pc)[1].internalvar);
f61e138d 1930 struct trace_state_variable *tsv;
5b4ee69b 1931
f61e138d
SS
1932 (*pc) += 3;
1933 gen_expr (exp, pc, ax, value);
1934 tsv = find_trace_state_variable (name);
1935 if (tsv)
f7c79c41 1936 {
f61e138d 1937 ax_tsv (ax, aop_setv, tsv->number);
92bc6a20 1938 if (ax->tracing)
f61e138d 1939 ax_tsv (ax, aop_tracev, tsv->number);
f7c79c41 1940 }
f7c79c41 1941 else
3e43a32a
MS
1942 error (_("$%s is not a trace state variable, "
1943 "may not assign to it"), name);
f61e138d
SS
1944 }
1945 else
1946 error (_("May only assign to trace state variables"));
1947 break;
782b2b07 1948
f61e138d
SS
1949 case BINOP_ASSIGN_MODIFY:
1950 (*pc)++;
1951 op2 = (*pc)[0].opcode;
1952 (*pc)++;
1953 (*pc)++;
1954 if ((*pc)[0].opcode == OP_INTERNALVAR)
1955 {
caaece0e 1956 const char *name = internalvar_name ((*pc)[1].internalvar);
f61e138d 1957 struct trace_state_variable *tsv;
5b4ee69b 1958
f61e138d
SS
1959 (*pc) += 3;
1960 tsv = find_trace_state_variable (name);
1961 if (tsv)
1962 {
1963 /* The tsv will be the left half of the binary operation. */
1964 ax_tsv (ax, aop_getv, tsv->number);
92bc6a20 1965 if (ax->tracing)
f61e138d
SS
1966 ax_tsv (ax, aop_tracev, tsv->number);
1967 /* Trace state variables are always 64-bit integers. */
1968 value1.kind = axs_rvalue;
6661ad48 1969 value1.type = builtin_type (ax->gdbarch)->builtin_long_long;
f61e138d
SS
1970 /* Now do right half of expression. */
1971 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1972 /* We have a result of the binary op, set the tsv. */
1973 ax_tsv (ax, aop_setv, tsv->number);
92bc6a20 1974 if (ax->tracing)
f61e138d
SS
1975 ax_tsv (ax, aop_tracev, tsv->number);
1976 }
1977 else
3e43a32a
MS
1978 error (_("$%s is not a trace state variable, "
1979 "may not assign to it"), name);
c906108c 1980 }
f61e138d
SS
1981 else
1982 error (_("May only assign to trace state variables"));
c906108c
SS
1983 break;
1984
1985 /* Note that we need to be a little subtle about generating code
dda83cd7
SM
1986 for comma. In C, we can do some optimizations here because
1987 we know the left operand is only being evaluated for effect.
1988 However, if the tracing kludge is in effect, then we always
1989 need to evaluate the left hand side fully, so that all the
1990 variables it mentions get traced. */
c906108c
SS
1991 case BINOP_COMMA:
1992 (*pc)++;
f7c79c41 1993 gen_expr (exp, pc, ax, &value1);
c906108c 1994 /* Don't just dispose of the left operand. We might be tracing,
dda83cd7
SM
1995 in which case we want to emit code to trace it if it's an
1996 lvalue. */
40f4af28 1997 gen_traced_pop (ax, &value1);
f7c79c41 1998 gen_expr (exp, pc, ax, value);
c906108c
SS
1999 /* It's the consumer's responsibility to trace the right operand. */
2000 break;
c5aa993b 2001
c906108c
SS
2002 case OP_LONG: /* some integer constant */
2003 {
2004 struct type *type = (*pc)[1].type;
2005 LONGEST k = (*pc)[2].longconst;
5b4ee69b 2006
c906108c
SS
2007 (*pc) += 4;
2008 gen_int_literal (ax, value, k, type);
2009 }
c5aa993b 2010 break;
c906108c
SS
2011
2012 case OP_VAR_VALUE:
40f4af28 2013 gen_var_ref (ax, value, (*pc)[2].symbol);
400c6af0
SS
2014
2015 if (value->optimized_out)
2016 error (_("`%s' has been optimized out, cannot use"),
987012b8 2017 (*pc)[2].symbol->print_name ());
400c6af0 2018
78134374 2019 if (value->type->code () == TYPE_CODE_ERROR)
987012b8 2020 error_unknown_type ((*pc)[2].symbol->print_name ());
46a4882b 2021
c906108c
SS
2022 (*pc) += 4;
2023 break;
2024
74ea4be4
PA
2025 case OP_VAR_MSYM_VALUE:
2026 gen_msym_var_ref (ax, value, (*pc)[2].msymbol, (*pc)[1].objfile);
46a4882b 2027
78134374 2028 if (value->type->code () == TYPE_CODE_ERROR)
c9d95fa3 2029 error_unknown_type ((*pc)[2].msymbol->linkage_name ());
46a4882b 2030
74ea4be4
PA
2031 (*pc) += 4;
2032 break;
2033
c906108c
SS
2034 case OP_REGISTER:
2035 {
67f3407f
DJ
2036 const char *name = &(*pc)[2].string;
2037 int reg;
5b4ee69b 2038
67f3407f 2039 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
6661ad48 2040 reg = user_reg_map_name_to_regnum (ax->gdbarch, name, strlen (name));
67f3407f
DJ
2041 if (reg == -1)
2042 internal_error (__FILE__, __LINE__,
2043 _("Register $%s not available"), name);
6ab12e0f 2044 /* No support for tracing user registers yet. */
f6efe3f8 2045 if (reg >= gdbarch_num_cooked_regs (ax->gdbarch))
abc1f4cd
HZ
2046 error (_("'%s' is a user-register; "
2047 "GDB cannot yet trace user-register contents."),
6ab12e0f 2048 name);
c906108c
SS
2049 value->kind = axs_lvalue_register;
2050 value->u.reg = reg;
6661ad48 2051 value->type = register_type (ax->gdbarch, reg);
c906108c 2052 }
c5aa993b 2053 break;
c906108c
SS
2054
2055 case OP_INTERNALVAR:
f61e138d 2056 {
22d2b532
SDJ
2057 struct internalvar *var = (*pc)[1].internalvar;
2058 const char *name = internalvar_name (var);
f61e138d 2059 struct trace_state_variable *tsv;
5b4ee69b 2060
f61e138d
SS
2061 (*pc) += 3;
2062 tsv = find_trace_state_variable (name);
2063 if (tsv)
2064 {
2065 ax_tsv (ax, aop_getv, tsv->number);
92bc6a20 2066 if (ax->tracing)
f61e138d
SS
2067 ax_tsv (ax, aop_tracev, tsv->number);
2068 /* Trace state variables are always 64-bit integers. */
2069 value->kind = axs_rvalue;
6661ad48 2070 value->type = builtin_type (ax->gdbarch)->builtin_long_long;
f61e138d 2071 }
22d2b532 2072 else if (! compile_internalvar_to_ax (var, ax, value))
3e43a32a
MS
2073 error (_("$%s is not a trace state variable; GDB agent "
2074 "expressions cannot use convenience variables."), name);
f61e138d
SS
2075 }
2076 break;
c906108c 2077
c5aa993b 2078 /* Weirdo operator: see comments for gen_repeat for details. */
c906108c
SS
2079 case BINOP_REPEAT:
2080 /* Note that gen_repeat handles its own argument evaluation. */
2081 (*pc)++;
f7c79c41 2082 gen_repeat (exp, pc, ax, value);
c906108c
SS
2083 break;
2084
2085 case UNOP_CAST:
2086 {
2087 struct type *type = (*pc)[1].type;
5b4ee69b 2088
c906108c 2089 (*pc) += 3;
46a4882b 2090 gen_expr_for_cast (exp, pc, ax, value, type);
c906108c 2091 }
c5aa993b 2092 break;
c906108c 2093
9eaf6705
TT
2094 case UNOP_CAST_TYPE:
2095 {
2096 int offset;
2097 struct value *val;
2098 struct type *type;
2099
2100 ++*pc;
2101 offset = *pc - exp->elts;
2102 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2103 type = value_type (val);
2104 *pc = &exp->elts[offset];
46a4882b 2105 gen_expr_for_cast (exp, pc, ax, value, type);
9eaf6705
TT
2106 }
2107 break;
2108
c906108c
SS
2109 case UNOP_MEMVAL:
2110 {
2111 struct type *type = check_typedef ((*pc)[1].type);
5b4ee69b 2112
c906108c 2113 (*pc) += 3;
f7c79c41 2114 gen_expr (exp, pc, ax, value);
a0c78a73
PA
2115
2116 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2117 already have the right value on the stack. For
2118 axs_lvalue_register, we must convert. */
2119 if (value->kind == axs_lvalue_register)
2120 require_rvalue (ax, value);
2121
c906108c
SS
2122 value->type = type;
2123 value->kind = axs_lvalue_memory;
2124 }
c5aa993b 2125 break;
c906108c 2126
9eaf6705
TT
2127 case UNOP_MEMVAL_TYPE:
2128 {
2129 int offset;
2130 struct value *val;
2131 struct type *type;
2132
2133 ++*pc;
2134 offset = *pc - exp->elts;
2135 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2136 type = value_type (val);
2137 *pc = &exp->elts[offset];
2138
2139 gen_expr (exp, pc, ax, value);
2140
2141 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2142 already have the right value on the stack. For
2143 axs_lvalue_register, we must convert. */
2144 if (value->kind == axs_lvalue_register)
2145 require_rvalue (ax, value);
2146
2147 value->type = type;
2148 value->kind = axs_lvalue_memory;
2149 }
2150 break;
2151
36e9969c
NS
2152 case UNOP_PLUS:
2153 (*pc)++;
0e2de366 2154 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
f7c79c41 2155 gen_expr (exp, pc, ax, value);
6661ad48 2156 gen_usual_unary (ax, value);
36e9969c
NS
2157 break;
2158
c906108c
SS
2159 case UNOP_NEG:
2160 (*pc)++;
2161 /* -FOO is equivalent to 0 - FOO. */
22601c15 2162 gen_int_literal (ax, &value1, 0,
6661ad48
SM
2163 builtin_type (ax->gdbarch)->builtin_int);
2164 gen_usual_unary (ax, &value1); /* shouldn't do much */
f7c79c41 2165 gen_expr (exp, pc, ax, &value2);
6661ad48
SM
2166 gen_usual_unary (ax, &value2);
2167 gen_usual_arithmetic (ax, &value1, &value2);
f7c79c41 2168 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
c906108c
SS
2169 break;
2170
2171 case UNOP_LOGICAL_NOT:
2172 (*pc)++;
f7c79c41 2173 gen_expr (exp, pc, ax, value);
6661ad48 2174 gen_usual_unary (ax, value);
3b11a015 2175 gen_logical_not (ax, value, int_type);
c906108c
SS
2176 break;
2177
2178 case UNOP_COMPLEMENT:
2179 (*pc)++;
f7c79c41 2180 gen_expr (exp, pc, ax, value);
6661ad48
SM
2181 gen_usual_unary (ax, value);
2182 gen_integral_promotions (ax, value);
c906108c
SS
2183 gen_complement (ax, value);
2184 break;
2185
2186 case UNOP_IND:
2187 (*pc)++;
f7c79c41 2188 gen_expr (exp, pc, ax, value);
6661ad48 2189 gen_usual_unary (ax, value);
b97aedf3 2190 if (!pointer_type (value->type))
3d263c1d 2191 error (_("Argument of unary `*' is not a pointer."));
053f8057 2192 gen_deref (value);
c906108c
SS
2193 break;
2194
2195 case UNOP_ADDR:
2196 (*pc)++;
f7c79c41 2197 gen_expr (exp, pc, ax, value);
053f8057 2198 gen_address_of (value);
c906108c
SS
2199 break;
2200
2201 case UNOP_SIZEOF:
2202 (*pc)++;
2203 /* Notice that gen_sizeof handles its own operand, unlike most
dda83cd7
SM
2204 of the other unary operator functions. This is because we
2205 have to throw away the code we generate. */
f7c79c41 2206 gen_sizeof (exp, pc, ax, value,
6661ad48 2207 builtin_type (ax->gdbarch)->builtin_int);
c906108c
SS
2208 break;
2209
2210 case STRUCTOP_STRUCT:
2211 case STRUCTOP_PTR:
2212 {
2213 int length = (*pc)[1].longconst;
caaece0e 2214 const char *name = &(*pc)[2].string;
c906108c
SS
2215
2216 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
f7c79c41 2217 gen_expr (exp, pc, ax, value);
c906108c 2218 if (op == STRUCTOP_STRUCT)
6661ad48 2219 gen_struct_ref (ax, value, name, ".", "structure or union");
c906108c 2220 else if (op == STRUCTOP_PTR)
6661ad48 2221 gen_struct_ref (ax, value, name, "->",
c906108c
SS
2222 "pointer to a structure or union");
2223 else
2224 /* If this `if' chain doesn't handle it, then the case list
c5aa993b 2225 shouldn't mention it, and we shouldn't be here. */
8e65ff28 2226 internal_error (__FILE__, __LINE__,
3d263c1d 2227 _("gen_expr: unhandled struct case"));
c906108c 2228 }
c5aa993b 2229 break;
c906108c 2230
6c228b9c
SS
2231 case OP_THIS:
2232 {
66a17cb6 2233 struct symbol *sym, *func;
3977b71f 2234 const struct block *b;
66a17cb6 2235 const struct language_defn *lang;
6c228b9c 2236
66a17cb6
TT
2237 b = block_for_pc (ax->scope);
2238 func = block_linkage_function (b);
c1b5c1eb 2239 lang = language_def (func->language ());
6c228b9c 2240
d12307c1 2241 sym = lookup_language_this (lang, b).symbol;
6c228b9c 2242 if (!sym)
5bae7c4e 2243 error (_("no `%s' found"), lang->name_of_this ());
6c228b9c 2244
40f4af28 2245 gen_var_ref (ax, value, sym);
400c6af0
SS
2246
2247 if (value->optimized_out)
2248 error (_("`%s' has been optimized out, cannot use"),
987012b8 2249 sym->print_name ());
400c6af0 2250
6c228b9c
SS
2251 (*pc) += 2;
2252 }
2253 break;
2254
b6e7192f
SS
2255 case OP_SCOPE:
2256 {
2257 struct type *type = (*pc)[1].type;
2258 int length = longest_to_int ((*pc)[2].longconst);
caaece0e 2259 const char *name = &(*pc)[3].string;
b6e7192f
SS
2260 int found;
2261
2347965c 2262 found = gen_aggregate_elt_ref (ax, value, type, name);
b6e7192f
SS
2263 if (!found)
2264 error (_("There is no field named %s"), name);
2265 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2266 }
2267 break;
2268
c906108c 2269 case OP_TYPE:
608b4967
TT
2270 case OP_TYPEOF:
2271 case OP_DECLTYPE:
3d263c1d 2272 error (_("Attempt to use a type name as an expression."));
c906108c
SS
2273
2274 default:
b6e7192f 2275 error (_("Unsupported operator %s (%d) in expression."),
88b91969 2276 op_name (op), op);
c906108c
SS
2277 }
2278}
f61e138d 2279
e2803273
TT
2280namespace expr
2281{
2282
2283void
2284operation::generate_ax (struct expression *exp,
2285 struct agent_expr *ax,
2286 struct axs_value *value,
2287 struct type *cast_type)
2288{
2289 if (constant_p ())
2290 {
2291 struct value *v = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
2292 ax_const_l (ax, value_as_long (v));
2293 value->kind = axs_rvalue;
2294 value->type = check_typedef (value_type (v));
2295 }
2296 else
2297 {
2298 do_generate_ax (exp, ax, value, cast_type);
2299 if (cast_type != nullptr)
2300 gen_cast (ax, value, cast_type);
2301 }
2302}
2303
d5ab122c
TT
2304void
2305scope_operation::do_generate_ax (struct expression *exp,
2306 struct agent_expr *ax,
2307 struct axs_value *value,
2308 struct type *cast_type)
2309{
2310 struct type *type = std::get<0> (m_storage);
2311 const std::string &name = std::get<1> (m_storage);
2312 int found = gen_aggregate_elt_ref (ax, value, type, name.c_str ());
2313 if (!found)
2314 error (_("There is no field named %s"), name.c_str ());
2315}
2316
d336c29e
TT
2317void
2318long_const_operation::do_generate_ax (struct expression *exp,
2319 struct agent_expr *ax,
2320 struct axs_value *value,
2321 struct type *cast_type)
2322{
2323 gen_int_literal (ax, value, std::get<1> (m_storage),
2324 std::get<0> (m_storage));
2325}
2326
0c8effa3
TT
2327void
2328var_msym_value_operation::do_generate_ax (struct expression *exp,
2329 struct agent_expr *ax,
2330 struct axs_value *value,
2331 struct type *cast_type)
2332{
2333 gen_msym_var_ref (ax, value, std::get<0> (m_storage),
2334 std::get<1> (m_storage));
2335
2336 if (value->type->code () == TYPE_CODE_ERROR)
2337 {
2338 if (cast_type == nullptr)
2339 error_unknown_type (std::get<0> (m_storage)->linkage_name ());
2340 value->type = cast_type;
2341 }
2342}
2343
55bdbff8
TT
2344void
2345register_operation::do_generate_ax (struct expression *exp,
2346 struct agent_expr *ax,
2347 struct axs_value *value,
2348 struct type *cast_type)
2349{
2350 const char *name = std::get<0> (m_storage).c_str ();
2351 int len = std::get<0> (m_storage).size ();
2352 int reg;
2353
2354 reg = user_reg_map_name_to_regnum (ax->gdbarch, name, len);
2355 if (reg == -1)
2356 internal_error (__FILE__, __LINE__,
2357 _("Register $%s not available"), name);
2358 /* No support for tracing user registers yet. */
2359 if (reg >= gdbarch_num_cooked_regs (ax->gdbarch))
2360 error (_("'%s' is a user-register; "
2361 "GDB cannot yet trace user-register contents."),
2362 name);
2363 value->kind = axs_lvalue_register;
2364 value->u.reg = reg;
2365 value->type = register_type (ax->gdbarch, reg);
2366}
2367
e6e01e16
TT
2368void
2369internalvar_operation::do_generate_ax (struct expression *exp,
2370 struct agent_expr *ax,
2371 struct axs_value *value,
2372 struct type *cast_type)
2373{
2374 struct internalvar *var = std::get<0> (m_storage);
2375 const char *name = internalvar_name (var);
2376 struct trace_state_variable *tsv;
2377
2378 tsv = find_trace_state_variable (name);
2379 if (tsv)
2380 {
2381 ax_tsv (ax, aop_getv, tsv->number);
2382 if (ax->tracing)
2383 ax_tsv (ax, aop_tracev, tsv->number);
2384 /* Trace state variables are always 64-bit integers. */
2385 value->kind = axs_rvalue;
2386 value->type = builtin_type (ax->gdbarch)->builtin_long_long;
2387 }
2388 else if (! compile_internalvar_to_ax (var, ax, value))
2389 error (_("$%s is not a trace state variable; GDB agent "
2390 "expressions cannot use convenience variables."), name);
2391}
2392
9186293f
TT
2393void
2394ternop_cond_operation::do_generate_ax (struct expression *exp,
2395 struct agent_expr *ax,
2396 struct axs_value *value,
2397 struct type *cast_type)
2398{
2399 struct axs_value value1, value2, value3;
2400 int if1, end;
2401
2402 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
2403 gen_usual_unary (ax, &value1);
2404 /* For (A ? B : C), it's easiest to generate subexpression
2405 bytecodes in order, but if_goto jumps on true, so we invert
2406 the sense of A. Then we can do B by dropping through, and
2407 jump to do C. */
2408 gen_logical_not (ax, &value1, builtin_type (ax->gdbarch)->builtin_int);
2409 if1 = ax_goto (ax, aop_if_goto);
2410 std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
2411 gen_usual_unary (ax, &value2);
2412 end = ax_goto (ax, aop_goto);
2413 ax_label (ax, if1, ax->len);
2414 std::get<2> (m_storage)->generate_ax (exp, ax, &value3);
2415 gen_usual_unary (ax, &value3);
2416 ax_label (ax, end, ax->len);
2417 /* This is arbitrary - what if B and C are incompatible types? */
2418 value->type = value2.type;
2419 value->kind = value2.kind;
2420}
2421
d4eff4c1
TT
2422/* Generate code for GDB's magical `repeat' operator.
2423 LVALUE @ INT creates an array INT elements long, and whose elements
2424 have the same type as LVALUE, located in memory so that LVALUE is
2425 its first element. For example, argv[0]@argc gives you the array
2426 of command-line arguments.
2427
2428 Unfortunately, because we have to know the types before we actually
2429 have a value for the expression, we can't implement this perfectly
2430 without changing the type system, having values that occupy two
2431 stack slots, doing weird things with sizeof, etc. So we require
2432 the right operand to be a constant expression. */
2433void
2434repeat_operation::do_generate_ax (struct expression *exp,
2435 struct agent_expr *ax,
2436 struct axs_value *value,
2437 struct type *cast_type)
2438{
2439 struct axs_value value1;
2440
2441 /* We don't want to turn this into an rvalue, so no conversions
2442 here. */
2443 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
2444 if (value1.kind != axs_lvalue_memory)
2445 error (_("Left operand of `@' must be an object in memory."));
2446
2447 /* Evaluate the length; it had better be a constant. */
2448 if (!std::get<1> (m_storage)->constant_p ())
2449 error (_("Right operand of `@' must be a "
2450 "constant, in agent expressions."));
2451
2452 struct value *v
2453 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2454 EVAL_AVOID_SIDE_EFFECTS);
2455 if (value_type (v)->code () != TYPE_CODE_INT)
2456 error (_("Right operand of `@' must be an integer."));
2457 int length = value_as_long (v);
2458 if (length <= 0)
2459 error (_("Right operand of `@' must be positive."));
2460
2461 /* The top of the stack is already the address of the object, so
2462 all we need to do is frob the type of the lvalue. */
2463 /* FIXME-type-allocation: need a way to free this type when we are
2464 done with it. */
2465 struct type *array
2466 = lookup_array_range_type (value1.type, 0, length - 1);
2467
2468 value->kind = axs_lvalue_memory;
2469 value->type = array;
2470}
2471
ae64ba58
TT
2472void
2473comma_operation::do_generate_ax (struct expression *exp,
2474 struct agent_expr *ax,
2475 struct axs_value *value,
2476 struct type *cast_type)
2477{
2478 /* Note that we need to be a little subtle about generating code
2479 for comma. In C, we can do some optimizations here because
2480 we know the left operand is only being evaluated for effect.
2481 However, if the tracing kludge is in effect, then we always
2482 need to evaluate the left hand side fully, so that all the
2483 variables it mentions get traced. */
2484 struct axs_value value1;
2485 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
2486 /* Don't just dispose of the left operand. We might be tracing,
2487 in which case we want to emit code to trace it if it's an
2488 lvalue. */
2489 gen_traced_pop (ax, &value1);
2490 std::get<1> (m_storage)->generate_ax (exp, ax, value);
2491 /* It's the consumer's responsibility to trace the right operand. */
2492}
2493
85d23bda
TT
2494void
2495unop_sizeof_operation::do_generate_ax (struct expression *exp,
2496 struct agent_expr *ax,
2497 struct axs_value *value,
2498 struct type *cast_type)
2499{
2500 /* We don't care about the value of the operand expression; we only
2501 care about its type. However, in the current arrangement, the
2502 only way to find an expression's type is to generate code for it.
2503 So we generate code for the operand, and then throw it away,
2504 replacing it with code that simply pushes its size. */
2505 int start = ax->len;
2506
2507 std::get<0> (m_storage)->generate_ax (exp, ax, value);
2508
2509 /* Throw away the code we just generated. */
2510 ax->len = start;
2511
2512 ax_const_l (ax, TYPE_LENGTH (value->type));
2513 value->kind = axs_rvalue;
2514 value->type = builtin_type (ax->gdbarch)->builtin_int;
2515}
2516
cbc18219 2517void
165a813a
TT
2518unop_cast_operation::do_generate_ax (struct expression *exp,
2519 struct agent_expr *ax,
2520 struct axs_value *value,
2521 struct type *cast_type)
2522{
2523 std::get<0> (m_storage)->generate_ax (exp, ax, value,
2524 std::get<1> (m_storage));
2525}
2526
2527void
cbc18219
TT
2528unop_memval_operation::do_generate_ax (struct expression *exp,
2529 struct agent_expr *ax,
2530 struct axs_value *value,
2531 struct type *cast_type)
2532{
2533 std::get<0> (m_storage)->generate_ax (exp, ax, value);
2534 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2535 already have the right value on the stack. For
2536 axs_lvalue_register, we must convert. */
2537 if (value->kind == axs_lvalue_register)
2538 require_rvalue (ax, value);
2539
2540 value->type = std::get<1> (m_storage);
2541 value->kind = axs_lvalue_memory;
2542}
2543
2544void
2545unop_memval_type_operation::do_generate_ax (struct expression *exp,
2546 struct agent_expr *ax,
2547 struct axs_value *value,
2548 struct type *cast_type)
2549{
2550 struct value *val
2551 = std::get<0> (m_storage)->evaluate (nullptr, exp,
2552 EVAL_AVOID_SIDE_EFFECTS);
2553 struct type *type = value_type (val);
2554
2555 std::get<1> (m_storage)->generate_ax (exp, ax, value);
2556
2557 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2558 already have the right value on the stack. For
2559 axs_lvalue_register, we must convert. */
2560 if (value->kind == axs_lvalue_register)
2561 require_rvalue (ax, value);
2562
2563 value->type = type;
2564 value->kind = axs_lvalue_memory;
2565}
2566
f6b42326
TT
2567void
2568op_this_operation::do_generate_ax (struct expression *exp,
2569 struct agent_expr *ax,
2570 struct axs_value *value,
2571 struct type *cast_type)
2572{
2573 struct symbol *sym, *func;
2574 const struct block *b;
2575 const struct language_defn *lang;
2576
2577 b = block_for_pc (ax->scope);
2578 func = block_linkage_function (b);
2579 lang = language_def (func->language ());
2580
2581 sym = lookup_language_this (lang, b).symbol;
2582 if (!sym)
2583 error (_("no `%s' found"), lang->name_of_this ());
2584
2585 gen_var_ref (ax, value, sym);
2586
2587 if (value->optimized_out)
2588 error (_("`%s' has been optimized out, cannot use"),
2589 sym->print_name ());
2590}
2591
40786782
TT
2592void
2593assign_operation::do_generate_ax (struct expression *exp,
2594 struct agent_expr *ax,
2595 struct axs_value *value,
2596 struct type *cast_type)
2597{
2598 operation *subop = std::get<0> (m_storage).get ();
2599 if (subop->opcode () != OP_INTERNALVAR)
2600 error (_("May only assign to trace state variables"));
2601
2602 internalvar_operation *ivarop
2603 = dynamic_cast<internalvar_operation *> (subop);
2604 gdb_assert (ivarop != nullptr);
2605
2606 const char *name = internalvar_name (ivarop->get_internalvar ());
2607 struct trace_state_variable *tsv;
2608
2609 std::get<1> (m_storage)->generate_ax (exp, ax, value);
2610 tsv = find_trace_state_variable (name);
2611 if (tsv)
2612 {
2613 ax_tsv (ax, aop_setv, tsv->number);
2614 if (ax->tracing)
2615 ax_tsv (ax, aop_tracev, tsv->number);
2616 }
e5946e16
TT
2617 else
2618 error (_("$%s is not a trace state variable, "
2619 "may not assign to it"), name);
2620}
2621
2622void
2623assign_modify_operation::do_generate_ax (struct expression *exp,
2624 struct agent_expr *ax,
2625 struct axs_value *value,
2626 struct type *cast_type)
2627{
2628 operation *subop = std::get<1> (m_storage).get ();
2629 if (subop->opcode () != OP_INTERNALVAR)
2630 error (_("May only assign to trace state variables"));
2631
2632 internalvar_operation *ivarop
2633 = dynamic_cast<internalvar_operation *> (subop);
2634 gdb_assert (ivarop != nullptr);
2635
2636 const char *name = internalvar_name (ivarop->get_internalvar ());
2637 struct trace_state_variable *tsv;
2638
2639 tsv = find_trace_state_variable (name);
2640 if (tsv)
2641 {
2642 /* The tsv will be the left half of the binary operation. */
2643 ax_tsv (ax, aop_getv, tsv->number);
2644 if (ax->tracing)
2645 ax_tsv (ax, aop_tracev, tsv->number);
2646 /* Trace state variables are always 64-bit integers. */
2647 struct axs_value value1, value2;
2648 value1.kind = axs_rvalue;
2649 value1.type = builtin_type (ax->gdbarch)->builtin_long_long;
2650 /* Now do right half of expression. */
2651 std::get<2> (m_storage)->generate_ax (exp, ax, &value2);
2652 gen_expr_binop_rest (exp, std::get<0> (m_storage), ax,
2653 value, &value1, &value2);
2654 /* We have a result of the binary op, set the tsv. */
2655 ax_tsv (ax, aop_setv, tsv->number);
2656 if (ax->tracing)
2657 ax_tsv (ax, aop_tracev, tsv->number);
2658 }
40786782
TT
2659 else
2660 error (_("$%s is not a trace state variable, "
2661 "may not assign to it"), name);
2662}
2663
292382f4
TT
2664void
2665unop_cast_type_operation::do_generate_ax (struct expression *exp,
2666 struct agent_expr *ax,
2667 struct axs_value *value,
2668 struct type *cast_type)
2669{
2670 struct value *val
2671 = std::get<0> (m_storage)->evaluate (nullptr, exp,
2672 EVAL_AVOID_SIDE_EFFECTS);
2673 std::get<1> (m_storage)->generate_ax (exp, ax, value, value_type (val));
2674}
2675
e82a5afc
TT
2676void
2677var_value_operation::do_generate_ax (struct expression *exp,
2678 struct agent_expr *ax,
2679 struct axs_value *value,
2680 struct type *cast_type)
2681{
2682 gen_var_ref (ax, value, std::get<0> (m_storage));
2683
2684 if (value->optimized_out)
2685 error (_("`%s' has been optimized out, cannot use"),
2686 std::get<0> (m_storage)->print_name ());
2687
2688 if (value->type->code () == TYPE_CODE_ERROR)
2689 {
2690 if (cast_type == nullptr)
2691 error_unknown_type (std::get<0> (m_storage)->print_name ());
2692 value->type = cast_type;
2693 }
2694}
2695
5019124b
TT
2696void
2697logical_and_operation::do_generate_ax (struct expression *exp,
2698 struct agent_expr *ax,
2699 struct axs_value *value,
2700 struct type *cast_type)
2701{
2702 struct axs_value value1, value2;
2703 int if1, go1, if2, go2, end;
2704
2705 /* Generate the obvious sequence of tests and jumps. */
2706 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
2707 gen_usual_unary (ax, &value1);
2708 if1 = ax_goto (ax, aop_if_goto);
2709 go1 = ax_goto (ax, aop_goto);
2710 ax_label (ax, if1, ax->len);
2711 std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
2712 gen_usual_unary (ax, &value2);
2713 if2 = ax_goto (ax, aop_if_goto);
2714 go2 = ax_goto (ax, aop_goto);
2715 ax_label (ax, if2, ax->len);
2716 ax_const_l (ax, 1);
2717 end = ax_goto (ax, aop_goto);
2718 ax_label (ax, go1, ax->len);
2719 ax_label (ax, go2, ax->len);
2720 ax_const_l (ax, 0);
2721 ax_label (ax, end, ax->len);
2722 value->kind = axs_rvalue;
2723 value->type = builtin_type (ax->gdbarch)->builtin_int;
2724}
2725
2726void
2727logical_or_operation::do_generate_ax (struct expression *exp,
2728 struct agent_expr *ax,
2729 struct axs_value *value,
2730 struct type *cast_type)
2731{
2732 struct axs_value value1, value2;
2733 int if1, if2, end;
2734
2735 /* Generate the obvious sequence of tests and jumps. */
2736 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
2737 gen_usual_unary (ax, &value1);
2738 if1 = ax_goto (ax, aop_if_goto);
2739 std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
2740 gen_usual_unary (ax, &value2);
2741 if2 = ax_goto (ax, aop_if_goto);
2742 ax_const_l (ax, 0);
2743 end = ax_goto (ax, aop_goto);
2744 ax_label (ax, if1, ax->len);
2745 ax_label (ax, if2, ax->len);
2746 ax_const_l (ax, 1);
2747 ax_label (ax, end, ax->len);
2748 value->kind = axs_rvalue;
2749 value->type = builtin_type (ax->gdbarch)->builtin_int;
2750}
2751
e2803273
TT
2752}
2753
f61e138d
SS
2754/* This handles the middle-to-right-side of code generation for binary
2755 expressions, which is shared between regular binary operations and
2756 assign-modify (+= and friends) expressions. */
2757
2758static void
2759gen_expr_binop_rest (struct expression *exp,
e18c58f2 2760 enum exp_opcode op,
f61e138d
SS
2761 struct agent_expr *ax, struct axs_value *value,
2762 struct axs_value *value1, struct axs_value *value2)
2763{
6661ad48 2764 struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
3b11a015 2765
6661ad48
SM
2766 gen_usual_unary (ax, value2);
2767 gen_usual_arithmetic (ax, value1, value2);
f61e138d
SS
2768 switch (op)
2769 {
2770 case BINOP_ADD:
78134374 2771 if (value1->type->code () == TYPE_CODE_INT
b97aedf3 2772 && pointer_type (value2->type))
f61e138d
SS
2773 {
2774 /* Swap the values and proceed normally. */
2775 ax_simple (ax, aop_swap);
2776 gen_ptradd (ax, value, value2, value1);
2777 }
b97aedf3 2778 else if (pointer_type (value1->type)
78134374 2779 && value2->type->code () == TYPE_CODE_INT)
f61e138d
SS
2780 gen_ptradd (ax, value, value1, value2);
2781 else
2782 gen_binop (ax, value, value1, value2,
2783 aop_add, aop_add, 1, "addition");
2784 break;
2785 case BINOP_SUB:
b97aedf3 2786 if (pointer_type (value1->type)
78134374 2787 && value2->type->code () == TYPE_CODE_INT)
f61e138d 2788 gen_ptrsub (ax,value, value1, value2);
b97aedf3
SS
2789 else if (pointer_type (value1->type)
2790 && pointer_type (value2->type))
f61e138d
SS
2791 /* FIXME --- result type should be ptrdiff_t */
2792 gen_ptrdiff (ax, value, value1, value2,
6661ad48 2793 builtin_type (ax->gdbarch)->builtin_long);
f61e138d
SS
2794 else
2795 gen_binop (ax, value, value1, value2,
2796 aop_sub, aop_sub, 1, "subtraction");
2797 break;
2798 case BINOP_MUL:
2799 gen_binop (ax, value, value1, value2,
2800 aop_mul, aop_mul, 1, "multiplication");
2801 break;
2802 case BINOP_DIV:
2803 gen_binop (ax, value, value1, value2,
2804 aop_div_signed, aop_div_unsigned, 1, "division");
2805 break;
2806 case BINOP_REM:
2807 gen_binop (ax, value, value1, value2,
2808 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2809 break;
948103cf
SS
2810 case BINOP_LSH:
2811 gen_binop (ax, value, value1, value2,
2812 aop_lsh, aop_lsh, 1, "left shift");
2813 break;
2814 case BINOP_RSH:
2815 gen_binop (ax, value, value1, value2,
2816 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2817 break;
f61e138d 2818 case BINOP_SUBSCRIPT:
be636754
PA
2819 {
2820 struct type *type;
2821
2822 if (binop_types_user_defined_p (op, value1->type, value2->type))
2823 {
3e43a32a
MS
2824 error (_("cannot subscript requested type: "
2825 "cannot call user defined functions"));
be636754
PA
2826 }
2827 else
2828 {
2829 /* If the user attempts to subscript something that is not
2830 an array or pointer type (like a plain int variable for
2831 example), then report this as an error. */
2832 type = check_typedef (value1->type);
78134374
SM
2833 if (type->code () != TYPE_CODE_ARRAY
2834 && type->code () != TYPE_CODE_PTR)
be636754 2835 {
7d93a1e0 2836 if (type->name ())
be636754 2837 error (_("cannot subscript something of type `%s'"),
7d93a1e0 2838 type->name ());
be636754
PA
2839 else
2840 error (_("cannot subscript requested type"));
2841 }
2842 }
2843
5d5b640e 2844 if (!is_integral_type (value2->type))
3e43a32a
MS
2845 error (_("Argument to arithmetic operation "
2846 "not a number or boolean."));
5d5b640e 2847
be636754 2848 gen_ptradd (ax, value, value1, value2);
053f8057 2849 gen_deref (value);
be636754
PA
2850 break;
2851 }
f61e138d
SS
2852 case BINOP_BITWISE_AND:
2853 gen_binop (ax, value, value1, value2,
2854 aop_bit_and, aop_bit_and, 0, "bitwise and");
2855 break;
2856
2857 case BINOP_BITWISE_IOR:
2858 gen_binop (ax, value, value1, value2,
2859 aop_bit_or, aop_bit_or, 0, "bitwise or");
2860 break;
2861
2862 case BINOP_BITWISE_XOR:
2863 gen_binop (ax, value, value1, value2,
2864 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2865 break;
2866
2867 case BINOP_EQUAL:
3b11a015 2868 gen_equal (ax, value, value1, value2, int_type);
f61e138d
SS
2869 break;
2870
2871 case BINOP_NOTEQUAL:
3b11a015
SS
2872 gen_equal (ax, value, value1, value2, int_type);
2873 gen_logical_not (ax, value, int_type);
f61e138d
SS
2874 break;
2875
2876 case BINOP_LESS:
3b11a015 2877 gen_less (ax, value, value1, value2, int_type);
f61e138d
SS
2878 break;
2879
2880 case BINOP_GTR:
2881 ax_simple (ax, aop_swap);
3b11a015 2882 gen_less (ax, value, value1, value2, int_type);
f61e138d
SS
2883 break;
2884
2885 case BINOP_LEQ:
2886 ax_simple (ax, aop_swap);
3b11a015
SS
2887 gen_less (ax, value, value1, value2, int_type);
2888 gen_logical_not (ax, value, int_type);
f61e138d
SS
2889 break;
2890
2891 case BINOP_GEQ:
3b11a015
SS
2892 gen_less (ax, value, value1, value2, int_type);
2893 gen_logical_not (ax, value, int_type);
f61e138d
SS
2894 break;
2895
2896 default:
2897 /* We should only list operators in the outer case statement
2898 that we actually handle in the inner case statement. */
2899 internal_error (__FILE__, __LINE__,
2900 _("gen_expr: op case sets don't match"));
2901 }
2902}
e18c58f2
TT
2903
2904/* Variant of gen_expr_binop_rest that first generates the
2905 right-hand-side. */
2906
2907static void
2908gen_expr_binop_rest (struct expression *exp,
2909 enum exp_opcode op, union exp_element **pc,
2910 struct agent_expr *ax, struct axs_value *value,
2911 struct axs_value *value1, struct axs_value *value2)
2912{
2913 gen_expr (exp, pc, ax, value2);
2914 gen_expr_binop_rest (exp, op, ax, value, value1, value2);
2915}
75f9892d
TT
2916
2917/* A helper function that emits a binop based on two operations. */
2918
2919void
2920gen_expr_binop (struct expression *exp,
2921 enum exp_opcode op,
2922 expr::operation *lhs, expr::operation *rhs,
2923 struct agent_expr *ax, struct axs_value *value)
2924{
2925 struct axs_value value1, value2;
2926
2927 lhs->generate_ax (exp, ax, &value1);
2928 gen_usual_unary (ax, &value1);
2929 rhs->generate_ax (exp, ax, &value2);
2930 gen_expr_binop_rest (exp, op, ax, value, &value1, &value2);
2931}
2932
2933/* A helper function that emits a structop based on an operation and a
2934 member name. */
2935
2936void
2937gen_expr_structop (struct expression *exp,
2938 enum exp_opcode op,
2939 expr::operation *lhs,
2940 const char *name,
2941 struct agent_expr *ax, struct axs_value *value)
2942{
2943 lhs->generate_ax (exp, ax, value);
2944 if (op == STRUCTOP_STRUCT)
2945 gen_struct_ref (ax, value, name, ".", "structure or union");
2946 else if (op == STRUCTOP_PTR)
2947 gen_struct_ref (ax, value, name, "->",
2948 "pointer to a structure or union");
2949 else
2950 /* If this `if' chain doesn't handle it, then the case list
2951 shouldn't mention it, and we shouldn't be here. */
2952 internal_error (__FILE__, __LINE__,
2953 _("gen_expr: unhandled struct case"));
2954}
9307d17b
TT
2955
2956/* A helper function that emits a unary operation. */
2957
2958void
2959gen_expr_unop (struct expression *exp,
2960 enum exp_opcode op,
2961 expr::operation *lhs,
2962 struct agent_expr *ax, struct axs_value *value)
2963{
2964 struct axs_value value1, value2;
2965
2966 switch (op)
2967 {
2968 case UNOP_NEG:
2969 gen_int_literal (ax, &value1, 0,
2970 builtin_type (ax->gdbarch)->builtin_int);
2971 gen_usual_unary (ax, &value1); /* shouldn't do much */
2972 lhs->generate_ax (exp, ax, &value2);
2973 gen_usual_unary (ax, &value2);
2974 gen_usual_arithmetic (ax, &value1, &value2);
2975 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2976 break;
2977
2978 case UNOP_PLUS:
2979 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2980 lhs->generate_ax (exp, ax, value);
2981 gen_usual_unary (ax, value);
2982 break;
2983
2984 case UNOP_LOGICAL_NOT:
2985 lhs->generate_ax (exp, ax, value);
2986 gen_usual_unary (ax, value);
2987 gen_logical_not (ax, value, builtin_type (ax->gdbarch)->builtin_int);
2988 break;
2989
2990 case UNOP_COMPLEMENT:
2991 lhs->generate_ax (exp, ax, value);
2992 gen_usual_unary (ax, value);
2993 gen_integral_promotions (ax, value);
2994 gen_complement (ax, value);
2995 break;
2996
876469ff
TT
2997 case UNOP_IND:
2998 lhs->generate_ax (exp, ax, value);
2999 gen_usual_unary (ax, value);
3000 if (!pointer_type (value->type))
3001 error (_("Argument of unary `*' is not a pointer."));
3002 gen_deref (value);
3003 break;
3004
14aff815
TT
3005 case UNOP_ADDR:
3006 lhs->generate_ax (exp, ax, value);
3007 gen_address_of (value);
3008 break;
3009
9307d17b
TT
3010 default:
3011 gdb_assert_not_reached ("invalid case in gen_expr_unop");
3012 }
3013}
3014
c906108c 3015\f
c5aa993b 3016
0936ad1d
SS
3017/* Given a single variable and a scope, generate bytecodes to trace
3018 its value. This is for use in situations where we have only a
3019 variable's name, and no parsed expression; for instance, when the
3020 name comes from a list of local variables of a function. */
3021
833177a4 3022agent_expr_up
400c6af0 3023gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
92bc6a20 3024 struct symbol *var, int trace_string)
0936ad1d 3025{
833177a4 3026 agent_expr_up ax (new agent_expr (gdbarch, scope));
0936ad1d
SS
3027 struct axs_value value;
3028
92bc6a20
TT
3029 ax->tracing = 1;
3030 ax->trace_string = trace_string;
40f4af28 3031 gen_var_ref (ax.get (), &value, var);
400c6af0
SS
3032
3033 /* If there is no actual variable to trace, flag it by returning
3034 an empty agent expression. */
3035 if (value.optimized_out)
833177a4 3036 return agent_expr_up ();
0936ad1d
SS
3037
3038 /* Make sure we record the final object, and get rid of it. */
40f4af28 3039 gen_traced_pop (ax.get (), &value);
0936ad1d
SS
3040
3041 /* Oh, and terminate. */
833177a4 3042 ax_simple (ax.get (), aop_end);
0936ad1d 3043
0936ad1d
SS
3044 return ax;
3045}
c5aa993b 3046
c906108c
SS
3047/* Generating bytecode from GDB expressions: driver */
3048
c906108c
SS
3049/* Given a GDB expression EXPR, return bytecode to trace its value.
3050 The result will use the `trace' and `trace_quick' bytecodes to
3051 record the value of all memory touched by the expression. The
3052 caller can then use the ax_reqs function to discover which
3053 registers it relies upon. */
833177a4
PA
3054
3055agent_expr_up
92bc6a20
TT
3056gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
3057 int trace_string)
c906108c 3058{
833177a4 3059 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
c906108c
SS
3060 union exp_element *pc;
3061 struct axs_value value;
3062
c906108c 3063 pc = expr->elts;
92bc6a20
TT
3064 ax->tracing = 1;
3065 ax->trace_string = trace_string;
35c9c7ba 3066 value.optimized_out = 0;
413403fc
TT
3067 if (expr->op != nullptr)
3068 expr->op->generate_ax (expr, ax.get (), &value);
3069 else
3070 gen_expr (expr, &pc, ax.get (), &value);
c906108c
SS
3071
3072 /* Make sure we record the final object, and get rid of it. */
40f4af28 3073 gen_traced_pop (ax.get (), &value);
c906108c
SS
3074
3075 /* Oh, and terminate. */
833177a4 3076 ax_simple (ax.get (), aop_end);
c906108c 3077
c906108c
SS
3078 return ax;
3079}
c906108c 3080
782b2b07
SS
3081/* Given a GDB expression EXPR, return a bytecode sequence that will
3082 evaluate and return a result. The bytecodes will do a direct
3083 evaluation, using the current data on the target, rather than
3084 recording blocks of memory and registers for later use, as
3085 gen_trace_for_expr does. The generated bytecode sequence leaves
3086 the result of expression evaluation on the top of the stack. */
3087
833177a4 3088agent_expr_up
782b2b07
SS
3089gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
3090{
833177a4 3091 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
782b2b07
SS
3092 union exp_element *pc;
3093 struct axs_value value;
3094
782b2b07 3095 pc = expr->elts;
92bc6a20 3096 ax->tracing = 0;
35c9c7ba 3097 value.optimized_out = 0;
413403fc
TT
3098 if (expr->op != nullptr)
3099 expr->op->generate_ax (expr, ax.get (), &value);
3100 else
3101 gen_expr (expr, &pc, ax.get (), &value);
782b2b07 3102
833177a4 3103 require_rvalue (ax.get (), &value);
35c9c7ba 3104
782b2b07 3105 /* Oh, and terminate. */
833177a4 3106 ax_simple (ax.get (), aop_end);
782b2b07 3107
782b2b07
SS
3108 return ax;
3109}
3110
833177a4 3111agent_expr_up
92bc6a20
TT
3112gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
3113 int trace_string)
6710bf39 3114{
833177a4 3115 agent_expr_up ax (new agent_expr (gdbarch, scope));
6710bf39
SS
3116 struct axs_value value;
3117
92bc6a20
TT
3118 ax->tracing = 1;
3119 ax->trace_string = trace_string;
6710bf39 3120
833177a4 3121 gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope);
6710bf39
SS
3122
3123 /* Make sure we record the final object, and get rid of it. */
40f4af28 3124 gen_traced_pop (ax.get (), &value);
6710bf39
SS
3125
3126 /* Oh, and terminate. */
833177a4 3127 ax_simple (ax.get (), aop_end);
6710bf39 3128
6710bf39
SS
3129 return ax;
3130}
3131
d3ce09f5
SS
3132/* Given a collection of printf-style arguments, generate code to
3133 evaluate the arguments and pass everything to a special
3134 bytecode. */
3135
833177a4 3136agent_expr_up
d3ce09f5
SS
3137gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
3138 CORE_ADDR function, LONGEST channel,
741d92cf 3139 const char *format, int fmtlen,
d3ce09f5
SS
3140 int nargs, struct expression **exprs)
3141{
833177a4 3142 agent_expr_up ax (new agent_expr (gdbarch, scope));
d3ce09f5
SS
3143 union exp_element *pc;
3144 struct axs_value value;
0e43993a 3145 int tem;
d3ce09f5 3146
92bc6a20
TT
3147 /* We're computing values, not doing side effects. */
3148 ax->tracing = 0;
3149
d3ce09f5
SS
3150 /* Evaluate and push the args on the stack in reverse order,
3151 for simplicity of collecting them on the target side. */
3152 for (tem = nargs - 1; tem >= 0; --tem)
3153 {
d3ce09f5 3154 value.optimized_out = 0;
413403fc
TT
3155 if (exprs[tem]->op != nullptr)
3156 exprs[tem]->op->generate_ax (exprs[tem], ax.get (), &value);
3157 else
3158 {
3159 pc = exprs[tem]->elts;
3160 gen_expr (exprs[tem], &pc, ax.get (), &value);
3161 }
833177a4 3162 require_rvalue (ax.get (), &value);
d3ce09f5
SS
3163 }
3164
3165 /* Push function and channel. */
833177a4
PA
3166 ax_const_l (ax.get (), channel);
3167 ax_const_l (ax.get (), function);
d3ce09f5
SS
3168
3169 /* Issue the printf bytecode proper. */
833177a4
PA
3170 ax_simple (ax.get (), aop_printf);
3171 ax_raw_byte (ax.get (), nargs);
3172 ax_string (ax.get (), format, fmtlen);
d3ce09f5
SS
3173
3174 /* And terminate. */
833177a4 3175 ax_simple (ax.get (), aop_end);
d3ce09f5
SS
3176
3177 return ax;
3178}
3179
c906108c 3180static void
6f937416 3181agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
c906108c 3182{
bbc13ae3 3183 const char *arg;
92bc6a20 3184 int trace_string = 0;
c906108c 3185
34b536a8
HZ
3186 if (!eval)
3187 {
34b536a8 3188 if (*exp == '/')
dda83cd7 3189 exp = decode_agent_options (exp, &trace_string);
34b536a8 3190 }
3065dfb6 3191
833177a4
PA
3192 agent_expr_up agent;
3193
bbc13ae3
KS
3194 arg = exp;
3195 if (!eval && strcmp (arg, "$_ret") == 0)
6710bf39 3196 {
036e657b
JB
3197 agent = gen_trace_for_return_address (pc, get_current_arch (),
3198 trace_string);
6710bf39
SS
3199 }
3200 else
3201 {
4d01a485 3202 expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
833177a4 3203
34b536a8 3204 if (eval)
92bc6a20
TT
3205 {
3206 gdb_assert (trace_string == 0);
036e657b 3207 agent = gen_eval_for_expr (pc, expr.get ());
92bc6a20 3208 }
34b536a8 3209 else
036e657b 3210 agent = gen_trace_for_expr (pc, expr.get (), trace_string);
6710bf39
SS
3211 }
3212
833177a4
PA
3213 ax_reqs (agent.get ());
3214 ax_print (gdb_stdout, agent.get ());
085dd6e6
JM
3215
3216 /* It would be nice to call ax_reqs here to gather some general info
3217 about the expression, and then print out the result. */
c906108c 3218
c906108c
SS
3219 dont_repeat ();
3220}
782b2b07 3221
782b2b07 3222static void
f2fc3015 3223agent_command_1 (const char *exp, int eval)
782b2b07 3224{
782b2b07
SS
3225 /* We don't deal with overlay debugging at the moment. We need to
3226 think more carefully about this. If you copy this code into
3227 another command, change the error message; the user shouldn't
3228 have to know anything about agent expressions. */
3229 if (overlay_debugging)
3230 error (_("GDB can't do agent expression translation with overlays."));
3231
3232 if (exp == 0)
3233 error_no_arg (_("expression to translate"));
3234
34b536a8
HZ
3235 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
3236 {
3237 struct linespec_result canonical;
34b536a8 3238
a20714ff
PA
3239 event_location_up location
3240 = new_linespec_location (&exp, symbol_name_match_type::WILD);
ffc2605c 3241 decode_line_full (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
cafb3438 3242 NULL, 0, &canonical,
34b536a8 3243 NULL, NULL);
34b536a8
HZ
3244 exp = skip_spaces (exp);
3245 if (exp[0] == ',')
dda83cd7 3246 {
34b536a8
HZ
3247 exp++;
3248 exp = skip_spaces (exp);
3249 }
6c5b2ebe
PA
3250 for (const auto &lsal : canonical.lsals)
3251 for (const auto &sal : lsal.sals)
3252 agent_eval_command_one (exp, eval, sal.pc);
34b536a8
HZ
3253 }
3254 else
3255 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
782b2b07 3256
782b2b07
SS
3257 dont_repeat ();
3258}
34b536a8
HZ
3259
3260static void
4fd41b24 3261agent_command (const char *exp, int from_tty)
34b536a8
HZ
3262{
3263 agent_command_1 (exp, 0);
3264}
3265
3266/* Parse the given expression, compile it into an agent expression
3267 that does direct evaluation, and display the resulting
3268 expression. */
3269
3270static void
4fd41b24 3271agent_eval_command (const char *exp, int from_tty)
34b536a8
HZ
3272{
3273 agent_command_1 (exp, 1);
3274}
3275
d3ce09f5
SS
3276/* Parse the given expression, compile it into an agent expression
3277 that does a printf, and display the resulting expression. */
3278
3279static void
4fd41b24 3280maint_agent_printf_command (const char *cmdrest, int from_tty)
d3ce09f5 3281{
d3ce09f5 3282 struct frame_info *fi = get_current_frame (); /* need current scope */
bbc13ae3 3283 const char *format_start, *format_end;
d3ce09f5
SS
3284
3285 /* We don't deal with overlay debugging at the moment. We need to
3286 think more carefully about this. If you copy this code into
3287 another command, change the error message; the user shouldn't
3288 have to know anything about agent expressions. */
3289 if (overlay_debugging)
3290 error (_("GDB can't do agent expression translation with overlays."));
3291
4fd41b24 3292 if (cmdrest == 0)
d3ce09f5
SS
3293 error_no_arg (_("expression to translate"));
3294
f1735a53 3295 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
3296
3297 if (*cmdrest++ != '"')
3298 error (_("Must start with a format string."));
3299
3300 format_start = cmdrest;
3301
8e481c3b 3302 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
3303
3304 format_end = cmdrest;
3305
3306 if (*cmdrest++ != '"')
3307 error (_("Bad format string, non-terminated '\"'."));
3308
f1735a53 3309 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
3310
3311 if (*cmdrest != ',' && *cmdrest != 0)
3312 error (_("Invalid argument syntax"));
3313
3314 if (*cmdrest == ',')
3315 cmdrest++;
f1735a53 3316 cmdrest = skip_spaces (cmdrest);
d3ce09f5 3317
8e481c3b 3318 std::vector<struct expression *> argvec;
d3ce09f5
SS
3319 while (*cmdrest != '\0')
3320 {
bbc13ae3 3321 const char *cmd1;
d3ce09f5
SS
3322
3323 cmd1 = cmdrest;
4d01a485 3324 expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
8e481c3b 3325 argvec.push_back (expr.release ());
d3ce09f5
SS
3326 cmdrest = cmd1;
3327 if (*cmdrest == ',')
3328 ++cmdrest;
3329 /* else complain? */
3330 }
3331
3332
833177a4
PA
3333 agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (),
3334 0, 0,
3335 format_start, format_end - format_start,
8e481c3b 3336 argvec.size (), argvec.data ());
833177a4
PA
3337 ax_reqs (agent.get ());
3338 ax_print (gdb_stdout, agent.get ());
d3ce09f5
SS
3339
3340 /* It would be nice to call ax_reqs here to gather some general info
3341 about the expression, and then print out the result. */
3342
d3ce09f5
SS
3343 dont_repeat ();
3344}
c5aa993b 3345
c906108c
SS
3346/* Initialization code. */
3347
6c265988 3348void _initialize_ax_gdb ();
c906108c 3349void
6c265988 3350_initialize_ax_gdb ()
c906108c 3351{
c906108c 3352 add_cmd ("agent", class_maintenance, agent_command,
34b536a8
HZ
3353 _("\
3354Translate an expression into remote agent bytecode for tracing.\n\
48c5e7e2 3355Usage: maint agent [-at LOCATION,] EXPRESSION\n\
34b536a8
HZ
3356If -at is given, generate remote agent bytecode for this location.\n\
3357If not, generate remote agent bytecode for current frame pc address."),
782b2b07
SS
3358 &maintenancelist);
3359
3360 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
34b536a8
HZ
3361 _("\
3362Translate an expression into remote agent bytecode for evaluation.\n\
48c5e7e2 3363Usage: maint agent-eval [-at LOCATION,] EXPRESSION\n\
34b536a8
HZ
3364If -at is given, generate remote agent bytecode for this location.\n\
3365If not, generate remote agent bytecode for current frame pc address."),
c906108c 3366 &maintenancelist);
d3ce09f5
SS
3367
3368 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
3369 _("Translate an expression into remote "
3370 "agent bytecode for evaluation and display the bytecodes."),
3371 &maintenancelist);
c906108c 3372}