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