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